相关疑难解决方法(0)

类定义静态constexpr结构的未定义引用,g ++ vs clang

这是我的代码,a.cp​​p

struct int2
{
    int x, y;
};
struct Foo{
    static constexpr int bar1 = 1;
    static constexpr int2 bar2 = {1, 2};
};
int foo1(){
    return Foo::bar1; // this is ok for both clang++ and g++
}
int2 foo2(){
    return Foo::bar2; // undefined reference to `Foo::bar2' in clang++
}
int main(){ std::cout << foo2().x << std::endl; return 0; }
Run Code Online (Sandbox Code Playgroud)

用clang编译, clang++ -std=c++11 a.cpp

/tmp/a-0dba90.o: In function `foo2()':
a.cpp:(.text+0x18): undefined reference to `Foo::bar2'
clang-7: error: linker command failed with exit …
Run Code Online (Sandbox Code Playgroud)

c++ one-definition-rule c++11

5
推荐指数
1
解决办法
200
查看次数

标签 统计

c++ ×1

c++11 ×1

one-definition-rule ×1