In quite a few languages like C# and D, 1f
is be a valid way to declare a float literal, but in C++ the closest you can get by default is 1.f
or 1.0f
. However, this behaviour can be implemented using C++11's user-defined literal operators (even if it involves breaking the "underscores for user defined literals" rule).
The following program (at least in g++ 4.9.2) works as expected, so I'm wondering if there's a good reason why 1f
isn't valid syntax for float literals by default in C++.
#include <iostream>
#include <typeinfo>
constexpr float operator""f(unsigned long long int i){
return static_cast<float>(i);
}
int main(){
auto f1 = 1f;
auto f2 = 1.f;
if(typeid(f1) == typeid(f2))
std::cout << "They're the same" << std::endl;
return 0;
}