The following single-file testcase fails to link:
#include
#include
#include <iostream>
template<typename Type>
class singleton {
public:
typedef boost::recursive_mutex mutex_type;
typedef Type * handle;
private:
typedef mutex_type::scoped_lock lock_type;
static mutex_type mutex;
static handle inst;
public:
static Type &instance(void) {
lock_type lock(mutex);
if (!inst) {
inst = new Type;
}
return(*inst);
};
};
class tester {
public:
void test(void) { std::cerr << "Test" << std::endl; };
};
template<> singleton<tester>::mutex_type singleton<tester>::mutex;
template<> singleton<tester>::handle singleton<tester>::inst = 0;
int main(void)
{
singleton<tester>::instance().test();
return(0);
}
thread.o: In function
`singleton<tester>::instance()':/home/greened/projects/apsim/current/apsim/msim/test/thread.cc:21:
undefined reference to `singleton<tester>::mutex'
collect2: ld returned 1 exit status
I was getting undefined references to "inst" before I added the
initializer. Is this a new adherance to the standard by g++ 4.0.2?
I didn't used to have to add the initializer to get it to link.
This is troublesome with boost::recursive_mutex because it is
noncopyable. How do I specify an initializer for it? Is it
just not possible to have a mutex static member?
-Dave