• Advertisement
Sign in to follow this  

Template function specialization

This topic is 4574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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<class T> std::string lexical_cast<std::string>(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

Share this post


Link to post
Share on other sites
> 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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement