I have the following :
Header file:
class CU
{
const char *u;
public:
constexpr CU (const char *u) :u(u) {}
constexpr const char *c_str () const { return u; } ;
constexpr operator const char * () const { return u; } ;
};
void test2 (const CU & r) ;
class S
{
public:
static constexpr CU u = CU("foo"); // linker error
/*
when I use the u in the normal constructor in a separate file I get a linker error
*/
S();
};
Source file :
#include "test.hpp"
#include <iostream>
void test2 (const CU & r)
{
std::cerr << r;
}
S::S()
{
test2(S::u);
}
int main () {
S a;
}
Output from the compiler :
g++ -std=gnu++11 test.cpp test.hpp
/tmp/cceBFmJM.o: In function S::S():
test.cpp:(.text+0x35): undefined reference to S::u
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'test' failed
make: *** [test] Error 1
See the bugzilla report here : https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67155