Compile error due to template: explicit specialization....

  • Thread starter Hendrik Schober
  • Start date
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>>()'.


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
 
R

ranges22

******************************************************************
I am compiling a librarry which has a .h file containing th
following:
******************************************************************

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 lon
&); //[t45]
template<> void from_string(const char Str[], int &); //[t45]
template<> void from_string(const char Str[], unsigned in
&); //[t45]
template<> void from_string(const char Str[], short &); //[t45]
template<> void from_string(const char Str[], unsigned shor
&); //[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[],PGSTD::strin
&Obj) //[t46]
{ Obj = Str; }

template<typename T>
inline void from_string(const PGSTD::string &Str, T &Obj) //[t45]
{ from_string(Str.c_str(), Obj); }

template<> inline void
from_string(const PGSTD::string &Str, PGSTD::string &Obj) //[t46]
{ Obj = Str; }

template<> inline void
from_string(const PGSTD::string &, const signed char &Obj)
{ error_ambiguous_string_conversion(Obj); }
template<> inline void
from_string(const PGSTD::string &, const unsigned char &Obj)
{ error_ambiguous_string_conversion(Obj); }


******************************************************************
I am however getting the following errors at compile time:
******************************************************************
../include\pqxx/util.hxx(111) : error C2912: explicit specialization
'void __cdecl pqxx::from_string(const clas
std::basic_string<char,struct std::char_traits<char>,clas
std::allocator<char> > &,const signed char &)' is not a functio
template
../include\pqxx/util.hxx(110) : see declaration of 'from_string'
../include\pqxx/util.hxx(111) : error C2912: explicit specialization
'void __cdecl pqxx::from_string(const clas
std::basic_string<char,struct std::char_traits<char>,clas
std::allocator<char> > &,const signed char &)' is not a functio
template
../include\pqxx/util.hxx(110) : see declaration of 'from_string'
../include\pqxx/util.hxx(114) : error C2912: explicit specialization
'void __cdecl pqxx::from_string(const clas
std::basic_string<char,struct std::char_traits<char>,clas
std::allocator<char> > &,const unsigned char &)' is not a function
template
../include\pqxx/util.hxx(113) : see declaration of 'from_string'
../include\pqxx/util.hxx(114) : error C2912: explicit specialization
'void __cdecl pqxx::from_string(const clas
std::basic_string<char,struct std::char_traits<char>,clas
std::allocator<char> > &,const unsigned char &)' is not a function
template
../include\pqxx/util.hxx(113) : see declaration of 'from_string'
../include\pqxx/util.hxx(322) : error C2265: '<Unknown>' : reference t
a zero-sized array is illegal
../include\pqxx/util.hxx(488) : fatal error C1506: unrecoverable bloc
scoping error



Can someone tell me how to correct this and what it actuallty relate
to?

Thanx;


-
ranges2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top