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).
source share