468,110 Members | 1,595 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

How to declare static members of a template class

I tried this code:

#include <iostream>
#include <string>

template<typename T>
struct enum_properties
{
static const long max;
static const std::string name;
};

enum MyEnum { a,b,c };

int main()
{
std::cout << "My enum is called " << enum_properties<MyEnum>::name
<< " and its max value is " << enum_properties<MyEnum>::max
<< std::endl;
}

const long enum_properties<MyEnum>::max (2);
const std::string enum_properties<MyEnum>::name ("MyEnum");

but it gets a parse error on the last 2 lines.

I found out that to fix the problem, I have to add in the
following lines, AFTER the declaration of MyEnum
but BEFORE the first usage of enum_properties<>::name :

template<>
struct enum_properties<MyEnum>
{
static const long max;
static const std::string name;
};

This seems like senseless duplication to me, I have to
repeat all this code for every different type that I
want to be a template parameter. Also, it can be difficult
in a large project to ensure that this code block occurs
after the enum declaration but before the first instantiation
of it, eg. if there is code in header files that refers to it.

Is there any better solution?

Jul 23 '05 #1
1 2149
Old Wolf wrote:
I tried this code:

#include <iostream>
#include <string>

template<typename T>
struct enum_properties
{
static const long max;
static const std::string name;
};

enum MyEnum { a,b,c };

int main()
{
std::cout << "My enum is called " << enum_properties<MyEnum>::name
<< " and its max value is " << enum_properties<MyEnum>::max
<< std::endl;
}

const long enum_properties<MyEnum>::max (2);
const std::string enum_properties<MyEnum>::name ("MyEnum");

but it gets a parse error on the last 2 lines.
The 'enum_properties<MyEnum>' class hasn't been defined, that's probably
the reason why definitions of those static members fail.

I found out that to fix the problem, I have to add in the
following lines, AFTER the declaration of MyEnum
but BEFORE the first usage of enum_properties<>::name :

template<>
struct enum_properties<MyEnum>
{
static const long max;
static const std::string name;
};
That's called "a specialization".
This seems like senseless duplication to me, I have to
repeat all this code for every different type that I
want to be a template parameter. Also, it can be difficult
in a large project to ensure that this code block occurs
after the enum declaration but before the first instantiation
of it, eg. if there is code in header files that refers to it.

Is there any better solution?


Static data members need to be defined. (Have you not heard of that
before?) If your 'max' is always the same, then after your class,
before the 'MyEnum' declaration, write

template<class T> const long enum_properties<T>::max(2); // ...

If they are not the same, you may explicitly _specify_ only those
members that you need. The syntax is similar to the specialisation:

template<> const long enum_properties<MyEnum>::max(2);

and those will be the definitions for the static members of your
_implicitly_ specialised enum_properties<MyEnum> class.

V
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alibek | last post: by
7 posts views Thread by Drew McCormack | last post: by
13 posts views Thread by Adam H. Peterson | last post: by
9 posts views Thread by Rennie deGraaf | last post: by
5 posts views Thread by mast2as | last post: by
8 posts views Thread by John | last post: by
10 posts views Thread by =?Utf-8?B?Y2FybG0=?= | last post: by
17 posts views Thread by Juha Nieminen | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.