mardi 20 novembre 2018

undefined reference to class static constexpr struct, g++ vs clang

This is my code, a.cpp

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(){ return 0; }

use clang to compile, 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 code 1 (use -v to see 
invocation)

g++ -std=c++11 a.cpp emits no error.

My question is,

  1. who is right on the above code? clang or g++?
  2. why bar2 is wrong while bar1 is correct in clang?

Aucun commentaire:

Enregistrer un commentaire