c++ - typedef 声明定义未命名类时链接失败

标签 c++ gcc g++ language-lawyer

C++11 标准的 7.1.3[9] 部分指出:

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).

[示例:

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

——结束示例]

这意味着在下面的示例中,Foo 应该用作未命名类的名称,并在下面的示例中用于链接目的:

//foo1.cpp
typedef class
{
        public: int f();
} Foo;

int Foo::f()
{
        return 5;
}

由于 Foo::f() 是在 foo1.cpp 中定义的,编译器不应该报错。

//foo2.cpp
typedef class
{
    public: int f();
} Foo;

Foo foo;

int main()
{
    return foo.f();
}

但是,GCC 4.8 出现链接错误。我错过了什么吗?

$g++ foo1.cpp foo2.cpp
/tmp/ccMwHawT.o: In function `main':
713Y51.cpp:(.text+0x24): undefined reference to `Foo::f()'
collect2: error: ld returned 1 exit status

最佳答案

我认为你是对的。除了您的问题中已经存在的内容之外,标准没有什么可以添加的。

这是一个 long-standing bug在海湾合作委员会。 clang 同意你的解释并接受你的程序。

关于c++ - typedef 声明定义未命名类时链接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25898127/

相关文章:

gcc - 以 "tree"开头的 gcc 标志有什么特别之处?

c++ - 为什么定义了移动构造函数而隐式删除了赋值运算符?

c++ - "Undefined reference"到定义的构造函数

g++ - Project ERROR : Cannot run compiler 'g++' . 也许你忘了在 gitlab env 中设置环境

c++ - Windows MS VC++ 2005 上的 CRT 堆调试

c++ - 如何检测API Hook?

c++ - 进行测试和变异函数的STL算法

c++ - C++ 中的卷影复制

c++ - 获取 `rsqrtss` 包装器的最少指令

c++ - 为什么 gcc 不能推断数组参数的模板化大小? (C++11)