Re:Re: passing a const to a template method
Alisdair Meredith [TeamB] wrote:
Quote
I would look at boost type_traits (to be standardised via Library TR1)
#include <boost/type_traits.hpp>
template<typename TValueType>
TValueType ToNumeric( TValueType ValueOnError )
{
std::wistringstream i( MyString.get() );
boost::remove_const< TValueType>::type x;
if( !( i>>x ) )
return ValueOnError;
return x;
}
AlisdairM(TeamB)
OK, now I've shown you the general case, the following should be a
simpler solution without any other library dependencies:
template<typename TValueType>
TValueType ToNumeric( const TValueType ValueOnError ) const
{
std::wistringstream i( MyString.get() );
TValueType x; // << E2304 constant must be initialised
if( !( i>>x ) )
return ValueOnError;
return x;
}
In this case the const is in the parameter declaration, and TValueType
should always deduce as non-const (as const const type makes no sense)
Some might prefer to make that a const reference, to avoid expensive
copies. OTOH, I understand your main use case is going to be primitive
types like int, so that pass-by-reference would probably be *less*
efficient. [And obfuscating code with metaprogramming tricks for
optimal passing is simply not worth it in most cases]
AlisdairM(TeamB)
{smallsort}