Consider:
struct str {};
str operator"" _X(long double d) {
return str();
}
This compiles fine with g++ 4.7.2 Wall std=c++11
but now if I give a double :
str operator"" _X(double d) {
return str();
}
I get the following error message: main.cpp|3|error: 'str operator"" _X(double)' has invalid argument list
What is the problem ? Has this something to do with "It is not possible to redefine the meaning of a built-in literal suffix" (Stroustrup FAQ) ? Can you think of a workaround ?
The problem is that the Standard forbids it. Per paragraph 13.5.8./3 of the C++11 Standard on user-defined literals:
Concerning a workaround, I am not sure it is needed, since the following works fine (a
double
gets implicitly converted to along double
, so you can pass in literals of typedouble
):I think it's to prevent ambiguous overloads. What if you were allowed to define the following overload set
Can you give examples of user-defined literals in source code that would map to each of the above? No, there's no way to constrain the literal to a particular floating-point datatype.
What could be useful is this set:
Since now you could write