Mar 11, 2022

[C++] new syntax parsing

Reference:
https://stackoverflow.com/questions/71380971/why-is-new-int-10-wrong
https://en.cppreference.com/w/cpp/language/new


Doesn't work:
auto p = new int (*)[10]; // error: parsed as (new int) (*[10]) ()

Ok:
typedef int array[10];
auto p = new array *;
Ok:
new (int (*[10])()); // okay: allocates an array of 10 pointers to functions

Reasoning:
Syntax for new without initializer is either
new (type)

or
new type

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.