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