使用别名引用匿名结构会导致错误
Using alias for reference to anonymous structure results in error
我的理论是 gcc 有一个错误。以下在 clang 和 gcc 中编译:
using type = const struct {}&;
但是现在,当我将其更改为右值引用时,它可以使用 clang 进行编译,但不能使用 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;
// ^
它在 typedef
版本中也失败并出现同样的错误:
typedef const struct {}&& type;
为什么在 gcc 中编译失败?这是标准问题还是错误?
这看起来像一个 gcc
错误,未命名 class 的语法在 9
部分中介绍[class] 我们有以下内容:
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
和以下文字:
A class-specifier whose class-head omits the class-head-name defines an unnamed class.
所以一个 未命名的 class 只是一个 class-specifier 没有名字和 class-specifier 是 type-specifier 和 7.1.3
[dcl.typedef] 说:
The typedef specifier shall not be combined in a declspecifier-
seq with any other kind of specifier except a type-specifier
并且对 未命名 class 没有任何限制,仅在本段中提及它们:
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 ]
我的理论是 gcc 有一个错误。以下在 clang 和 gcc 中编译:
using type = const struct {}&;
但是现在,当我将其更改为右值引用时,它可以使用 clang 进行编译,但不能使用 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;
// ^
它在 typedef
版本中也失败并出现同样的错误:
typedef const struct {}&& type;
为什么在 gcc 中编译失败?这是标准问题还是错误?
这看起来像一个 gcc
错误,未命名 class 的语法在 9
部分中介绍[class] 我们有以下内容:
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
和以下文字:
A class-specifier whose class-head omits the class-head-name defines an unnamed class.
所以一个 未命名的 class 只是一个 class-specifier 没有名字和 class-specifier 是 type-specifier 和 7.1.3
[dcl.typedef] 说:
The typedef specifier shall not be combined in a declspecifier- seq with any other kind of specifier except a type-specifier
并且对 未命名 class 没有任何限制,仅在本段中提及它们:
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 ]