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 :)
I don't have Clang, but Google finds a page listing __has_feature
selectors.
Use __has_feature(cxx_user_literals) to determine if support for user-defined literals is enabled.
I'm having a problem with C++11 user defined literals with Clang 3.1 that comes with XCode 4.5 DP1 install
That's the problem. Clang 3.1 does not come with 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.
As Potatoswatter observes, the right way to test for this feature in Clang is __has_feature(cxx_user_literals)
.
Here's what Clang trunk says about your code:
<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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.