Why isn\'t 0f treated as a floating point literal in C++?
#include
using namespace std;
int main(){
cout << 0f <&l
If there was an explicitly stated reason for this design decision, it would be in the C99 "Rationale" document (C++ copied all this stuff verbatim from C without reconsidering it). But there isn't. This is everything that's said about the 'f' suffix:
§6.4.4.2 Floating constants
Consistent with existing practice, a floating-point constant is defined to have type
double. Since C89 allows expressions that contain onlyfloatoperands to be performed infloatarithmetic rather thandouble, a method of expressing explicitfloatconstants is desirable. Thelong doubletype raises similar issues.The
FandLsuffixes have been added to convey type information with floating constants, much like theLsuffix does for long integers. The default type of floating constants remains double for compatibility with prior practice. Lower-casefandlare also allowed as suffixes.
There is an implied reason, though. Note the wording: "the ... suffixes have been added to convey type information with floating constants." The authors of the standard were thinking of numeric constants as already being unambiguously either integer or floating point by the time you get to the suffix. The suffix is only for extra specificity within the category, it can't flip a number from one category to another. This is backed up by the actual grammar (C99 §6.4.4) which first defines numeric constants as being either integer-constants or floating-constants, and then defines separate classes of suffixes for each.