I am currently porting a bunch of code that was previously compiled using Visual Studio 2008. This code has this scheme:
template <typename T>
T convert( const char * s )
{
std::istringstream is( s );
T ret;
is >> ret;
return ret;
}
template <typename T, typename T2>
T convert( T2 * s )
{
return convert<T>( static_cast<const char*>( s ));
}
template <typename T, typename T2>
T convert( T2 s )
{
return T( s );
}
template <>
inline int convert<int>( const char * s )
{
return (int)atoi( s );
}
As a rule, there are many specializations of a template function with different types of return values, which are called as follows:
int i = convert<int>( szInt );
The problem is that these template specializations lead to "Ambiguous template specialization." If it were something other than the return type that distinguished these specialized functions, I could just use overloads, but that is not an option.
How can I solve this problem without changing all the places that are called by the conversion functions?
, .
, , , - , , *.
GCC , , .
2
cpp, .
" ", .
- , .
#include <iostream>
#include <sstream>
template <typename T>
T convert( const char * s )
{
std::istringstream is( s );
T ret;
is >> ret;
return ret;
}
template <typename T, typename T2>
T convert( T2 * s )
{
return convert<T>( static_cast<const char*>( s ));
}
template <typename T, typename T2>
T convert( T2 s )
{
return T( s );
}
template <>
inline float convert<float>( const char * s )
{
return (float)atof( s );
}
int main( int argc, const char * sz[] )
{
return 0;
}