问题
Recently, in the gcc-trunk sources was implemented the "user defined literals". Tell me please, do I understand correctly that I can`t define a "user defined literals" for variadic char template?
template<char... chars>
int operator"" _call() { return sizeof...(chars); }
...
std::cout << "method"_call;
Up.
I don`t understand why this expression is allowed:
template<char... chars>
int operator"" _call() { return sizeof...(chars); }
...
std::cout << 12345566_call;
and this one is disallowed:
template<char... chars>
int operator"" _call() { return sizeof...(chars); }
...
std::cout << method_call;
?
What's the point?
Up. this is because of the ambiguity?
Thanks.
回答1:
method_call
is a valid identifier As is for example some_call
or my_call
. Now imagine how much code would be broken if such identifiers were allowed to be redefined by operator""
.
回答2:
No, it doesn't really make sense. String literals are passed as two arguments to operator""
, and one of them is size, so what you want is:
size_t operator"" _call(const char*, size_t len) {
return len;
}
Standard quote time (2.14.8.5):
5 If L is a user-defined-string-literal, let str be the literal without its ud-suffix and let len be the number of code units in str (i.e., its length excluding the terminating null character). The literal L is treated as a call of the form
operator "" X (str, len)
The variadic template forms are considered only for user-defined-integer-literal (2.14.8.3) and user-defined-floating-literal (2.14.8.4).
As for method_call
, method
is not a literal.
来源:https://stackoverflow.com/questions/8097767/user-defined-literals-for-variadic-char-template