Brackets around placing a new statement for arrays

Playing with placing new for arrays, I came up (by chance / error) with the following code:

#include <new> struct X{}; int main() { char buf[256]; std::size_t n = 10; X* p = new (buf) (X[n]); // incorrect way, parenthesis by mistake //X* p = new (buf) X[n]; // correct way } 

The third line in main is incorrect, although it compiles. There should not be any parentheses. clang ++ spits out

warning: when the type is in parentheses, the array cannot have dynamic size

while gcc6 outputs

warning: ISO C ++ prohibits a variable-length array [-Wvla] X * p = new (buf) (X [n]);

warning: a non-constant array of a new length should be specified without parentheses around an identifier of the type [-Wvla] X * p = new (buf) (X [n]);

then the internal compiler error (ICE) crashes in tree_to_uhwi, on tree.h: 4044. The internal compiler error only appears in gcc> = 6.

My question is: how exactly is a line marked as โ€œincorrectโ€ parsed / interpreted, why is it โ€œwrongโ€ to have these parentheses? *

* For ICE, Iโ€™ll fill in the error anyway.

EDIT 1 I just realized that ICE / warning have nothing to do with user-defined types, so the same behavior is observed for int instead of struct X

EDIT 2 gcc6 error is filled here . ICE does not appear in gcc5 or earlier (only warnings that are correct appear).

+5
source share
1 answer

With parentheses, the type to be entered comes from the type identifier, in this case X[n] . This is a variable length array that is not standard behavior. Without parentheses, the input type is the identifier of the new type, an array of X

+4
source

All Articles