H
Hendrik Schober
ranges22 said:******************************************************************
I am compiling a librarry which has a .h file containing the
following:
******************************************************************
[...]
Can someone tell me how to correct this and what it actuallty relates
to?
Can you create a repro case? This
template< typename T >
void error_ambiguous_string_conversion(T) {}
template<typename T> void from_string(const char Str[], T &Obj);
template<> void from_string(const char Str[], long &); //[t45]
template<> void from_string(const char Str[], unsigned long &); //[t45]
template<> void from_string(const char Str[], int &); //[t45]
template<> void from_string(const char Str[], unsigned int &); //[t45]
template<> void from_string(const char Str[], short &); //[t45]
template<> void from_string(const char Str[], unsigned short &); //[t45]
template<> void from_string(const char Str[], float &); //[t46]
template<> void from_string(const char Str[], double &); //[t46]
template<> void from_string(const char Str[], long double &); //[t46]
template<> void from_string(const char Str[], bool &); //[t76]
template<> inline void from_string(const char Str[],std::string &Obj) //[t46]
{ Obj = Str; }
template<typename T>
inline void from_string(const std::string &Str, T &Obj) //[t45]
{ from_string(Str.c_str(), Obj); }
template<> inline void
from_string(const std::string &Str, std::string &Obj) //[t46]
{ Obj = Str; }
template<> inline void
from_string(const std::string &, const signed char &Obj)
{ error_ambiguous_string_conversion(Obj); }
template<> inline void
from_string(const std::string &, const unsigned char &Obj)
{ error_ambiguous_string_conversion(Obj); }
template< typename T >
T test(const std::string& str)
{
T obj;
from_string(str,obj);
return obj;
}
int main()
{
std::string foo("foo");
test<long >(foo);
test<unsigned long >(foo);
test<int >(foo);
test<unsigned int >(foo);
test<short >(foo);
test<unsigned short >(foo);
test<float >(foo);
test<double >(foo);
test<long double >(foo);
test<bool >(foo);
test<std::string >(foo);
test<signed char >(foo);
test<unsigned char >(foo);
return 0;
}
actually compiles for me without any
problem using VC7.1. Which compiler
version do you use? VC6/7 are known
to be pretty bad with templates. (If
you use one of them, try to use class
template specialization instead of
function template specialization. The
often works where the latter doesn't.)
Finally, have you considered this:
template< typename T >
inline void from_string(const std::string& str, T& obj)
{
std::istringstream iss(str);
iss >> obj;
if( !iss ) throw conversion_error( str, typeid(T).name() );
}
inline void from_string(const std::string& str, std::string& obj)
{
obj = str;
}
This works out of the box with all types
that have a suitable 'operator>>()'.
Thanx
HTH,
Schobi
--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org
"Sometimes compilers are so much more reasonable than people."
Scott Meyers