|
Boost : |
From: Joe Gottman (jgottman_at_[hidden])
Date: 2006-07-28 19:29:34
When I compile lexical_cast.hpp using MSVC 03 I get a spurious warning:
warning C4701: local variable 'result' may be used without having been
initialized.
It comes from the following function:
template<typename Target, typename Source>
Target lexical_cast(const Source &arg)
{
typedef typename detail::array_to_pointer_decay<Source>::type
NewSource;
detail::lexical_stream<Target, NewSource> interpreter;
Target result;
if(!(interpreter << arg && interpreter >> result))
throw_exception(bad_lexical_cast(typeid(NewSource),
typeid(Target)));
return result;
}
Of course this code is perfectly safe, as either result will be initialized
or an exception will be thrown. However, it would be nice if you can change
this code to the following:
template<typename Target, typename Source>
Target lexical_cast(const Source &arg)
{
typedef typename detail::array_to_pointer_decay<Source>::type
NewSource;
detail::lexical_stream<Target, NewSource> interpreter;
Target result;
if(!(interpreter << arg && interpreter >> result)) {
throw_exception(bad_lexical_cast(typeid(NewSource),
typeid(Target)));
static Target notused ;
return notused; //We never get here, but some compilers require
it to prevent an uninitialized variable warning
} else {
return result;
}
}
That will remove the warning. There is similar code in boost::array.
Joe Gottman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk