I want to store integers for given types which should be used during compilation and during runtime.
Up to now I have the following:
template<typename T>
struct my_int { enum { result = -1 }; };
And I specialize for each type:
template<> struct my_int<a_type> { enum { result = 5 }; };
And I can check during compile time (of course the check here would be against another compile time constant):
static_assert(my_int<a_type>::result == 5, "Bla");
Problem: This works well, as long as the specialization is in the same namespace. But that is an inconvenience I want to get rid of. So I want to be able to use it in every namespace:
namespace foo {
template<> struct my_int<a_type> { enum { result = 5 }; };
}
namespace bar {
template<> struct my_int<b_type> { enum { result = 7 }; };
}
Any ideas how I could do this?
C++11 and boost is ok for my situation, if really needed.
Update: Seems I gave to little information. The types are mainly enum classes. If you're really interested you can see the real implementation here, http://www.codeduce.com/extra/enum_tools, download the zip and in the header line 33, 34.
template<> struct my_int<foo::a_type> { enum { result = 5 }; };::my_intor::otherNS::my_interror: class template specialization of 'my_int' must originally be declared in namespace 'otherNS'.error: global qualification of class name is invalid before '{' token