Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
324 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...