https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77742
Bug ID: 77742
Summary: Warning about placement new for over-aligned type
Product: gcc
Version: 7.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
CC: jason at gcc dot gnu.org
Target Milestone: ---
#include <new>
struct X
{
alignas(2*__STDCPP_DEFAULT_NEW_ALIGNMENT__) int i;
};
int main()
{
alignas(alignof(X)) char buf[sizeof(X)];
::new((void*)buf) X{1};
}
al.cc: In function ‘int main()’:
al.cc:11:24: warning: ‘new’ of type ‘X’ with extended alignment 32
[-Waligned-new=]
::new((void*)buf) X{1};
^
al.cc:11:24: note: uses ‘void* operator new(std::size_t, void*)’, which does
not have an alignment parameter
al.cc:11:24: note: use ‘-faligned-new’ to enable C++17 over-aligned new support
tmp$ g++17 al.cc -faligned-new
al.cc: In function ‘int main()’:
al.cc:11:24: warning: ‘new’ of type ‘X’ with extended alignment 32
[-Waligned-new=]
::new((void*)buf) X{1};
^
al.cc:11:24: note: uses ‘void* operator new(std::size_t, void*)’, which does
not have an alignment parameter
al.cc:11:24: note: use ‘-faligned-new’ to enable C++17 over-aligned new support
Should this warning only be given for the non-placement forms of operator new?
That means trusting the programmer to have obtained suitably-aligned memory,
but that's always the case even for types with a fundamental alignment.
The code won't even compile without either -faligned-new or C++17, so the final
note is not helpful.
Also, if the new-expression occurs in a system header then you still get all
the notes but not the warning (unless you use -Wsystem-headers).