Hello,
I want to create a simple integer range checker and converter using c++ templates.
The code looks like this:
// D is the "destination" type and S the "source" type
template <class D, class S>
inline D SafeConvert( S value );
template <class S>
inline int SafeConvert<int>( S value ) {
ASSERT( value >= S(INT_MIN) && value <= S(INT_MAX) );
return int(value);
} /// error C2768: 'SafeConvert' : illegal use of explicit template arguments
template <class S>
inline size_t SafeConvert<size_t>( S value ) {
ASSERT( value >= S(0) && value <= S(size_t(-1)) );
return size_t(value);
} /// error C2768: 'SafeConvert' : illegal use of explicit template arguments
// ...
void test() {
size_t v = INT_MAX+1;
int iv = SafeConvert<int>(v);
}
However I have the following come compilation errors:
error C2768: 'SafeConvert' : illegal use of explicit template arguments
My question is how to tell the compiler that I want to specialize only the D class ?
Thanks.