How do I 'resolve' static members of a template class?

C

cpisztest

When I derive from this base class template, I get unresolved external symbol errors and I agree with the compiler. If it wasn't a template class, I'd define them in the .cpp and could do so because the types were known.

How do you handle this with templates?

I need one collection and mutex per class type that has derived from MyClass.

// MyClass.hxx

template<class Key, class T, class Cmp = std::less<Key> >
class MyClass
{
private:
static std::map<Key, T, Cmp> m_collection;
static boost::mutex m_mutex;
};
 
W

Wouter van Ooijen

template<class Key, class T, class Cmp = std::less<Key> >
class MyClass
{
private:
static std::map<Key, T, Cmp> m_collection;
static boost::mutex m_mutex;
};

add this, can be in the .hpp file:

template<class Key, class T, class Cmp >
std::map< Key, T, Cmp> MyClass< Key, T, Cmp >::m_collection;

template<class Key, class T, class Cmp >
boost::mutex MyClass< Key, T, Cmp >::m_mutex;

The idea is that
- you must declare your statics
- they are templatized, just like your class
- they are part of the instatized class

Wouter van Ooijen
 
C

cpisztest

add this, can be in the .hpp file:



template<class Key, class T, class Cmp >

std::map< Key, T, Cmp> MyClass< Key, T, Cmp >::m_collection;



template<class Key, class T, class Cmp >

boost::mutex MyClass< Key, T, Cmp >::m_mutex;



The idea is that

- you must declare your statics

- they are templatized, just like your class

- they are part of the instatized class



Wouter van Ooijen

I tried that and it gave me a compiler error about name is a dependent type..
I ended up doing this, which I pretty much just copied from google searches.. It would be good if I understood it, if someone can explain the meaning and use of typename here vs without.

Also, is it safe to define this in a header? I always did it in a .cpp, butwith a template class, you really don't have a .cpp until to actually instantiate it, aka write my derived classes and then it would seem awkward to define the base class statics in the derived class' .cpp files for each derived class.

So I stuck this in the tempate's header:

//-----------------------------------------------------------------------------
// inside MyClass.hxx
//
// Static members
template<class Key, class T, class Cmp>
typename MyClass<Key, T, Cmp>::CollectionType Registrar<Key, T, Cmp>::m_collection;

template<class Key, class T, class Cmp>
typename boost::mutex Registrar<Key, T, Cmp>::m_mutex;
 
C

cpisztest

I tried that and it gave me a compiler error about name is a dependent type.

I ended up doing this, which I pretty much just copied from google searches. It would be good if I understood it, if someone can explain the meaningand use of typename here vs without.



Also, is it safe to define this in a header? I always did it in a .cpp, but with a template class, you really don't have a .cpp until to actually instantiate it, aka write my derived classes and then it would seem awkward to define the base class statics in the derived class' .cpp files for each derived class.



So I stuck this in the tempate's header:



//-----------------------------------------------------------------------------

// inside MyClass.hxx

//

// Static members

template<class Key, class T, class Cmp>

typename MyClass<Key, T, Cmp>::CollectionType Registrar<Key, T, Cmp>::m_collection;



template<class Key, class T, class Cmp>

typename boost::mutex Registrar<Key, T, Cmp>::m_mutex;


Whoops. I was trying to replace the original class name with "MyClass" for discussion online. I missed a few spots.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top