Is the C++ syntax: T foo; valid?

The following code compiles and run with Clang (tested on 13, 14, and current git head), but not with GCC.

struct foo { int field<0, 1, int, 3>;
};

But I do not understand what it is declaring: what is this field ?

int field<0, 1, int, 3>;

I can put whatever I want in the field<> template (if it is even a template?), e.g. field<0, 1, int, 3> compiles and run. But I cannot access it afterwards.

9

2 Answers

Assuming field isn't a template that has been declared, the program is ill-formed.

But I do not understand what it is declaring: what is this field ?

Clang AST says:

`-CXXRecordDecl 0xdb6f20 <test.cpp:1:1, line:3:1> line:1:8 struct foo definition `-FieldDecl 0xdb7168 <line:2:3> col:7 'int'

Clang AST for a program with int field;:

`-CXXRecordDecl 0x168af90 <test2.cpp:1:1, line:3:1> line:1:8 struct foo definition `-FieldDecl 0x168b150 <line:2:3, col:7> col:7 field 'int'

So, it looks like Clang thinks that an int field is being declared, but the name of the field is empty. This seems to be corroborated by being able to initialise this "unnamed" field:

foo f{0}; // compiles in Clang

The first Clang version to have this bug seems to be 9:

Without a declaration of field, this isn’t even valid syntax: the < can’t begin a template argument list, and expressions aren’t allowed there in a member-declaration. (With a suitable declaration, it could be an invalid declaration with two types and no variables.) Definitely diagnosable, and definitely a Clang bug.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like