2

I'm having a problem with C++11 user defined literals with Clang 3.1 that comes with XCode 4.5 DP1 install

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

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

[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 :)

4

2 に答える 2

4

私は Clang を持っていませんが、Google はセレクターをリストするページを見つけます。 __has_feature

__has_feature(cxx_user_literals) を使用して、ユーザー定義リテラルのサポートが有効になっているかどうかを判断します。

于 2012-06-17T11:28:49.460 に答える
1

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)壊れた部分的な実装を動作するものに置き換える前に、3.0 と 3.1 の間の Clang トランクから切り取られました。

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)
             ^

...これは不適切なタイプミスの修正を示唆していますが、少なくとも正しい診断であり、ユーザー定義リテラルの使用は受け入れられます。

于 2012-09-15T07:05:13.707 に答える