简体   繁体   English

Clang 3.1 C ++ 11用户定义的文字将不起作用

[英]Clang 3.1 C++11 User Defined Literals won't work

I'm having a problem with C++11 user defined literals with Clang 3.1 that comes with XCode 4.5 DP1 install 我在XCode 4.5 DP1安装随附的Clang 3.1中遇到了C ++ 11用户定义的文字的问题

The compiler looks like it supports them and I can define a new literal. 编译器看起来像支持它们,我可以定义一个新的文字。 I can call the literal function directly but when I use the literal in my code I get a compiler error. 我可以直接调用文字函数,但是当我在代码中使用文字时,会出现编译器错误。

Auto complete on Xcode even suggest my new literal when typing an underscore after a string :D Xcode上的自动完成功能甚至在字符串后输入下划线时建议使用我的新文字:D

Here is the code: 这是代码:

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

I get the below error: 我收到以下错误:

[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.

This is the version information for clang 这是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

Any help gratefully received :) 任何帮助表示感谢:)

I don't have Clang, but Google finds a page listing __has_feature selectors. 我没有Clang,但是Google找到了一个列出 __has_feature选择器的页面。

Use __has_feature(cxx_user_literals) to determine if support for user-defined literals is enabled. 使用__has_feature(cxx_user_literals)确定是否启用了对用户定义的文字的支持。

I'm having a problem with C++11 user defined literals with Clang 3.1 that comes with XCode 4.5 DP1 install 我在XCode 4.5 DP1安装随附的Clang 3.1中遇到了C ++ 11用户定义的文字的问题

That's the problem. 那就是问题所在。 Clang 3.1 does not come with XCode 4.5 DP1. Clang 3.1不随XCode 4.5 DP1一起提供。 Apple clang version 4.0 (tags/Apple/clang-421.10.42) (based on LLVM 3.1svn) was a cut from Clang trunk between 3.0 and 3.1, before I replaced the broken partial implementation with a working one. Apple clang version 4.0 (tags/Apple/clang-421.10.42) (based on LLVM 3.1svn)是从Clang干线中切下的,介于3.0和3.1之间,然后我用一个可用的部分替换了残缺的部分实现。

As Potatoswatter observes, the right way to test for this feature in Clang is __has_feature(cxx_user_literals) . 正如Potatoswatter观察到的那样,在Clang中测试此功能的正确方法是__has_feature(cxx_user_literals)

Here's what Clang trunk says about your code: 这是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)
             ^

... which has suggested an inappropriate typo correction, but at least it's a correct diagnostic, and your uses of user-defined literals are accepted. ...表示不正确的错字更正,但至少是正确的诊断,并且接受了用户定义文字的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM