How to declare static members of a template class

O

Old Wolf

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?
 
V

Victor Bazarov

Old said:
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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top