Re: How do I 'resolve' static members of a template class?
On Thursday, April 3, 2014 4:20:45 PM UTC-5, Wouter van Ooijen wrote:
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
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 a=
nd 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 inst=
antiate 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 der=
ived 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_col=
lection;
template<class Key, class T, class Cmp>
typename boost::mutex Registrar<Key, T, Cmp>::m_mutex;