Joh*_*ohn 5 c++ clang xcode4 c++11
Clang 3.1声称支持用户定义的文字.我可以定义这个:
int operator"" _tryit(long double n) { return int(n); }
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用它时,我收到一个错误:
int m = 5_tryit;
Run Code Online (Sandbox Code Playgroud)
'_tryit'整数常量的后缀无效
5long double在你的情况下不能隐式转换为a .您需要将其更改5.0为使其成为一个long double或显式调用该函数以使隐式转换工作:
int m = 5.0_tryit;
Run Code Online (Sandbox Code Playgroud)
要么
int n = operator"" _tryit(5);
Run Code Online (Sandbox Code Playgroud)
(同时测试clang version 3.1 (trunk) (llvm/trunk 155821))
这个SO问题对规则有很好的解释.
(另外,正如abarnert提到的那样,确保-std=c++11在编译时将标志传递给编译器).