473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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


************************************************** ****************
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
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 17 '05 #1
1 1960
ranges22 <ra*************@mail.codecomments.com> wrote:
************************************************** ****************
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

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
1
by: JohnPantango | last post by:
Can anyone give me a simple work around to fix this compiler error on VC7.1 .....cpp(23) : error C2912: explicit specialization; 'bool Equal<_F32>(const _F32::type,const _F32::type)' is not a...
4
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
2
by: Nick | last post by:
I'm learning C++ and ran into a compile error using Visual C++ 2005 Express on the following example program (located at http://www.cplusplus.com/doc/tutorial/templates.html): // template...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
7
by: (2b|!2b)==? | last post by:
I have a class template. Each of the instantiations implements a method in the class template differently, so I (need?) to use template speciaization. My question is this, when writing the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.