Search code examples
c++c++11gccusing-declaration

Using alias for reference to anonymous structure results in error


My theory is that gcc has a bug. The following compiles in both clang and gcc:

using type = const struct {}&;

But now when I change it to an rvalue reference it compiles with clang but not with gcc:

using type = const struct {}&&;

// main.cpp:8:17: error: expected ';' after struct definition
//  typedef struct {}&& type;
//                  ^
// main.cpp:8:17: error: missing type-name in typedef-declaration
// main.cpp:8:22: error: expected constructor, destructor, or type conversion before ';' token
//  typedef const struct {}&& type;
//                                ^

It fails with the typedef version as well with the same error:

typedef const struct {}&& type;

Why does this fail to compile in gcc? Is this an issue with the standard or a bug?


Solution

  • This looks like a gcc bug, the grammar for an unnamed class is covered in section 9 [class] and we have the following:

    class-specifier:
        class-head { member-specificationopt}
    class-head:
        class-key attribute-specifier-seqopt class-head-name class-virt-specifieropt base-clauseopt
        class-key attribute-specifier-seqopt base-clauseopt
    

    and the following text:

    A class-specifier whose class-head omits the class-head-name defines an unnamed class.

    so an unnamed class is simply a class-specifier without a name and class-specifier is a type-specifier and section 7.1.3 [dcl.typedef] says:

    The typedef specifier shall not be combined in a declspecifier- seq with any other kind of specifier except a type-specifier

    and does not have any restrictions with respect to unnamed class and only refers to them in this paragraph:

    If the typedef declaration defines an unnamed class (or enum), the first typedef-name declared by the declaration to be that class type (or enum type) is used to denote the class type (or enum type) for linkage purposes only (3.5). [ Example:

    typedef struct { } *ps, S; // S is the class name for linkage purposes
    

    —end example ]