[英]Clang 3.1 C++11 User Defined Literals won't work
我在XCode 4.5 DP1安装随附的Clang 3.1中遇到了C ++ 11用户定义的文字的问题
编译器看起来像支持它们,我可以定义一个新的文字。 我可以直接调用文字函数,但是当我在代码中使用文字时,会出现编译器错误。
Xcode上的自动完成功能甚至在字符串后输入下划线时建议使用我的新文字:D
这是代码:
#include <cstring>
#include <string>
#include <iostream>
std::string operator "" _tostr (const char* p, size_t n);
std::string operator"" _tostr (const char* p, size_t n)
{ return std::string(p); }
int main(void)
{
using namespace std;
// Reports DOES has string literals
#if __has_feature(cxx_variadic_templates)
cout << "Have string literals" << endl;
#else
cout << "Doesn't have string literals" << endl;
#endif
// Compiles and works fine
string x = _tostr("string one",std::strlen("string one"));
cout << x << endl;
// Does not compiler
string y = "Hello"_tostr;
cout << y << endl;
return 0;
}
我收到以下错误:
[GaziMac] ~/development/scram clang++ --stdlib=libstdc++ --std=c++11 test.cpp
test.cpp:22:23: error: expected ';' at end of declaration
string y = "Hello"_tostr;
^
;
1 error generated.
这是clang的版本信息
[GaziMac] ~/development/scram clang++ -v
Apple clang version 4.0 (tags/Apple/clang-421.10.42) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.0.0
Thread model: posix
任何帮助表示感谢:)
我没有Clang,但是Google找到了一个列出 __has_feature
选择器的页面。
使用__has_feature(cxx_user_literals)确定是否启用了对用户定义的文字的支持。
我在XCode 4.5 DP1安装随附的Clang 3.1中遇到了C ++ 11用户定义的文字的问题
那就是问题所在。 Clang 3.1不随XCode 4.5 DP1一起提供。 Apple clang version 4.0 (tags/Apple/clang-421.10.42) (based on LLVM 3.1svn)
是从Clang干线中切下的,介于3.0和3.1之间,然后我用一个可用的部分替换了残缺的部分实现。
正如Potatoswatter观察到的那样,在Clang中测试此功能的正确方法是__has_feature(cxx_user_literals)
。
这是Clang干线关于您的代码的内容:
<stdin>:23:16: error: use of undeclared identifier '_tostr'; did you mean 'strstr'?
string x = _tostr("string one",std::strlen("string one"));
^~~~~~
strstr
/usr/include/string.h:340:14: note: 'strstr' declared here
extern char *strstr (__const char *__haystack, __const char *__needle)
^
...表示不正确的错字更正,但至少是正确的诊断,并且接受了用户定义文字的使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.