簡體   English   中英

Clang 3.1 C ++ 11用戶定義的文字將不起作用

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM