In this answer what I really wanted to do is define a typename
in my template parameters which could be used in the cast and return.
So this:
template <typename T>
typename std::enable_if<sizeof(unsigned char) == sizeof(T), unsigned char>::type caster(T value){ return reinterpret_cast<unsigned char&>(value); }
Would become this:
template <typename T, typename R = std::enable_if<sizeof(unsigned char) == sizeof(T), unsigned char>::type >
R caster(T value){ return reinterpret_cast<R&>(value); }
This works and behaves as desired for a single template specialization, but say that I add another specialization:
template <typename T, typename R = std::enable_if<sizeof(short) == sizeof(T), short>::type>
R caster(T value){ return reinterpret_cast<R&>(value); }
Now I get an error:
error C2995: 'R caster(T)' : function template has already been defined
Is there a way to convince the compiler that only one of these specializations will actually build for any given call?
See Question&Answers more detail:os