This question has come up before, in particular here Should we generally use float literals for floats instead of the simpler double literals? but I was wondering if there were any better suggested solutions now we are in C++14 land and things like user defined literals and brace initialization exist.
The problem can be expressed as how to write a floating literal in a template function for floating point types
template <typename T> T foo( T x )
{
static_assert( std::is_floating_point<T>::value, "" );
T y = x * 101.0;
return( y );
}
So the question boils down to how do we write "101.0", as it stands it's a double so what happens if I call foo using
float a = 42.0f;
float b = foo( a );
If I write "101.0f" in foo what happens if I call foo with a double, note 101.0 and 101.0f are not necessarily the same.
Also how do I guarantee that no extra code is produced to cast values?
Other suggestions include writing things like "static_cast( 101.0 )", "T( 101.0 )" or other hideous things!