Conditional expression syntax expression error in specified type specifier inside new expression

Consider the following C ++ code:

struct A {};
struct A* b = (1 == 1) ? new struct A : new struct A;

MSVC accepts this, but GCC and Clang do not. I would like to understand what is right according to the standard and why.

The error that Clang gives is:

test.cpp:2:37: error: redefinition of 'A'
struct A* b = (1 == 1) ? new struct A : new struct A;
                                    ^
test.cpp:1:8: note: previous definition is here
struct A {};
       ^
test.cpp:2:37: error: '(anonymous struct at test.cpp:2:37)' cannot be defined in a type specifier
struct A* b = (1 == 1) ? new struct A : new struct A;
                                    ^
test.cpp:2:41: error: expected class name
struct A* b = (1 == 1) ? new struct A : new struct A;
                                        ^
test.cpp:2:53: error: expected '{' after base class list
struct A* b = (1 == 1) ? new struct A : new struct A;
                                                    ^
test.cpp:2:53: error: expected ':'
struct A* b = (1 == 1) ? new struct A : new struct A;
                                                    ^
                                                    :
test.cpp:2:24: note: to match this '?'
struct A* b = (1 == 1) ? new struct A : new struct A;
                       ^
test.cpp:2:53: error: expected expression
struct A* b = (1 == 1) ? new struct A : new struct A;
                                                    ^

This tells me that clang tries to parse markers struct A :as a class specifier when the colon enters the base sentence, and then takes help when this parsing fails.

, . , , -seq, . - -. , - .

+4
2

, .

- : 2015 , , , .

, -, DR 2141, - - , ( ).

Clang DR 2141 "".

, :

, : , -? , , .

+2

++ struct , class, public: private:. , struct A {}; , , A, struct:

A* b = (1 == 1) ? new A : new A;

OP, struct A new. new . cppreference.com:

new int + 1 // syntactically okay, '+1' offsets the pointer returned by 'new int'
new int * 1 // error, type (int *) is assumed and 1 doesn't make sense in '(new (int *)) 1'

, class A, , , (class A : public base_class), , error: redefinition of 'A'.

: , :

1 ? new (struct A) : new struct A;

, struct A , , , .

0

Source: https://habr.com/ru/post/1687692/


All Articles