I have the same problem like this:
"undefined reference" for static specification of a field template
but the workaround that they use will not work for me.
I have a CRTP class with static data elements, one of which is std :: mutex. Unfortunately, the GCC linker (4.8.2) gives me the "undefined reference" error for this mutex. Klang (3.4) does not. Is there a workaround? The initial question (linked above) called the copy constructor on the static data member, which caused GCC to issue a character, but since my data member is std :: mutex, this is not an option - the copy constructor is deleted, and there are no argument constructors there. Have I just been with you?
I don't believe the problem is with std :: mutex, I think the problem is with how GCC handles static data members in template classes that rely on default constructors.
Thanks for any help!
Here's a thin version of my problem: test.hh
#include <mutex>
template < class T >
class CRTP_class {
public:
T * ptr_;
static std::mutex mutex_;
static int clearly_a_problem_with_mutex_;
};
class Foo : public CRTP_class< Foo >
{
public:
void set_bar( int setting );
int bar_;
};
test.cc
#include <test.hh>
template<> std::mutex CRTP_class< Foo >::mutex_;
template<> int CRTP_class< Foo >::clearly_a_problem_with_mutex_( 0 );
void Foo::set_bar( int setting ) {
std::lock_guard< std::mutex > locker( mutex_ );
++clearly_a_problem_with_mutex_;
bar_ = setting;
}
main.cc
#include <test.hh>
int main() {
Foo foo;
foo.set_bar( 5 );
}
:
g++ -std=c++0x main.cc test.cc -I.
/tmp/cclyxUfC.o: In function `Foo::set_bar(int)':
test.cc:(.text+0x86): undefined reference to `CRTP_class<Foo>::mutex_'
collect2: error: ld returned 1 exit status
(Edit 1: , , " " - , .cc .hh - , , , . , , # , , )
(Edit 2: Ooops! .)