Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this user-defined literal not work? [duplicate]

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 ?

like image 233
Bérenger Avatar asked May 16 '13 20:05

Bérenger


People also ask

What is a user-defined literal?

A user-defined literal is an expression of any of the following forms an identifier, introduced by a literal operator or a literal operator template declaration (see below). All ud-suffix es introduced by a program must begin with the underscore character _.

What are user-defined literals in C++11?

(C++11) Allows integer, floating-point, character, and string literals to produce objects of user-defined type by defining a user-defined suffix. A user-defined literal is an expression of any of the following forms an identifier, introduced by a literal operator or a literal operator template declaration (see below).

What is the use of literals in Python?

We can define user-defined literals for user-defined types and new form of literals for built-in types. They help to make constants in code more readable.

How are user-defined literal expressions treated in overload sets?

If the overload set includes a raw literal operator, the user-defined literal expression is treated as a function call operator "" X("n")


2 Answers

What is the problem?

The problem is that the Standard forbids it. Per paragraph 13.5.8./3 of the C++11 Standard on user-defined literals:

The declaration of a literal operator shall have a parameter-declaration-clause equivalent to one of the following:

const char*
unsigned long long int
long double
char
wchar_t
char16_t
char32_t
const char*, std::size_t
const wchar_t*, std::size_t
const char16_t*, std::size_t
const char32_t*, std::size_t

Concerning a workaround, I am not sure it is needed, since the following works fine (a double gets implicitly converted to a long double, so you can pass in literals of type double):

struct str {};

str operator"" _X(long double d) {
    return str();
}

int main()
{
    str s = 4.0_X;
}
like image 124
Andy Prowl Avatar answered Oct 05 '22 23:10

Andy Prowl


I think it's to prevent ambiguous overloads. What if you were allowed to define the following overload set

str operator"" _X(long double ld);
str operator"" _X(double d);
str operator"" _X(float f);

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:

str operator"" l_X(long double ld);
str operator""  _X(long double d);
str operator"" f_X(long double f);

Since now you could write

3.0_X  // treated like a double
3.0l_X // treated like a long double
3.0f_X // treated like a float
like image 27
Ben Voigt Avatar answered Oct 06 '22 00:10

Ben Voigt