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

时间:2012-06-17 08:44:01

标签: c++ c++11 llvm-clang clang++

我在使用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

感激不尽的任何帮助:)

1 个答案:

答案 0 :(得分:4)

我没有Clang,但Google找到了一个页面listing __has_feature选择器。

  

使用__has_feature(cxx_user_literals)确定是否启用了对用户定义文字的支持。