【问题标题】:Using alias for reference to anonymous structure results in error使用别名引用匿名结构会导致错误
【发布时间】:2015-05-21 22:35:47
【问题描述】:

我的理论是 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 中编译失败?这是标准的问题还是错误?

【问题讨论】:

    标签: c++ c++11 gcc using-declaration


    【解决方案1】:

    这看起来像 gcc 错误,未命名类的语法在 9[class] 部分中介绍,我们有以下内容: p>

    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
    

    以及以下文字:

    类头省略类头名的类说明符定义了一个未命名的类。

    所以未命名的类只是一个没有名称的类说明符,而类说明符是一个类型说明符 和部分7.1.3 [dcl.typedef] 说:

    typedef 说明符不应组合在 declspecifier 中- seq 与除类型说明符之外的任何其他类型的说明符

    并且对未命名的类没有任何限制,仅在本段中引用它们:

    如果 typedef 声明定义了一个未命名的类(或枚举),则 由声明声明为该类类型的第一个 typedef-name (或枚举类型)用于表示类类型(或枚举类型) 仅用于链接目的 (3.5)。 [ 例子:

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

    ——结束示例]

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 2021-08-02
      • 2021-09-17
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      相关资源
      最近更新 更多