throwable .vs. non throwable?
I have a template I use for converting types:
template<typename T, typename F > T StrmConvert( const F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >> to;
return to;
}
template<typename F> std::string StrmConvert( const F from )
{
return StrmConvert<std::string>( from );
}
As you can see, it doesn't throw. If the conversion can not take place,
then it simply returns a default constructed type, which is fine for most
cases I use it. However, there may be a case where I want to throw on error.
I know I could simply copy this template and give it a new name, such as
StrmConvertThrow but I don't like that solution. What I would like to be
able to do (which may not be possible) is something like:
int i = 10;
std::string x;
x = StrmConvert( i ):throwable;
or such. I don't think that's right, well, okay, I know it's not right. I
do know that there is a throw( int ) type keyword for functions. I tried to
duplicate StrmConvert like this:
template<typename T, typename F > T StrmConvert( const F from ) throw(
int )
but got compile time errors.
console5.cpp(17) : warning C4290: C++ exception specification ignored except
to indicate a function is not __declspec(nothrow)
console5.cpp(24) : error C2382: 'StrmConvert' : redefinition; different
exception specifications
console5.cpp(3) : see declaration of 'StrmConvert'
so apparently C++ doesn't distinguish between function signatures by throw
or not. Although I seem to recall something about there being a new, and a
new throwable.
Am I barking up the wrong tree, or is there a way to do what I want?