Skip to main content
gamedev.net
🔒 Locked

Template function specialization

Started by TrueTom Oct 11, 2005 at 2:44 PM 3 replies 2.4k views
Original Post
TrueTom
TrueTom
Hi, I can't solve this problem:

template<class R, class T> R lexical_cast(const T& Value) 
{
	std::stringstream Stream;
	Stream << Value;

	R Result;
	Stream >> Result;

	return Result;
}


template<std::string, class T> std::string lexical_cast(const T& Value) 
{
	std::stringstream Stream;
	Stream << Value;

	return Stream.str();
}

...

// Problem: Always the first function is called...

... = lexical_cast<std::string>(SomeValue);




Does someone know how to solve this? Regards Tom
SiCrane
SiCrane
You could just use the boost::lexical_cast implementation.

The problem here is that partial template specialization of non-member template functions isn't happy. If you want to implement your own version of lexical cast, you can dispatch the function call to a templated class that you can then partially specialize for std::string.
Enigma
Enigma
The first problem is that that's not how you specialize a template. The second is that to do as you desire would require the ability to partially specialize a function template. Unfortunately the current C++ standard only allows partial specialization of member templates.

If function template partial specialization was allowed the syntax would be (assuming the same syntax as partial specialization for member templates):
template<class R, class T> R lexical_cast(const T& Value) {	std::stringstream Stream;	Stream << Value;	R Result;	Stream >> Result;	return Result;}template<font color="red"><class T></font> std::string lexical_cast<font color="red"><std::string></font>(const T& Value) {	std::stringstream Stream;	Stream << Value;	return Stream.str();}

However, since such a thing is not allowed it's a bit of a moot point. You only alternatives (that I can think of) would be to provide a lexical_cast_string or make lexical cast a member function of some object).

Enigma
TrueTom
TrueTom
> If you want to implement your own version of lexical cast, you can dispatch the
> function call to a templated class that you can then partially specialize for
> std::string.

Yes, the boost version seems to do something like this...


> The first problem is that that's not how you specialize a template.

That's true. After the compiler has thrown an error at the supposed syntax I just played around a bit...


> Unfortunately the current C++ standard only allows partial specialization of
> member templates.

Thats also true. It's normally not necessary since you can still overload the function (though not in this case)...


> You only alternatives (that I can think of) would be to provide a
> lexical_cast_string

Well, I guess I will use this, not to elegant but reasonable...


Thanks for your help!

TrueTom
snk_kid
snk_kid
You can simulate partial specializations of function templates, read about type traits and compile-time tag/type dispatch.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.