Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang++ and u16string

Tags:

c++

c++11

clang++

I'm having a hell of a time with this simple line of code and the latest clang++

#include <stdio.h>
#include <string>

using std::u16string;


int main ( int argc, char** argv )
{
    u16string s16 = u"鵝滿是快烙滴好耳痛";

    return EXIT_SUCCESS;
}


Ben-iMac:Desktop Ben$ clang++ -std=c++0x -stdlib=libc++ main.cpp -o main
main.cpp:15:21: error: use of undeclared identifier 'u'
    u16string s16 = u"鵝滿是快烙滴好耳痛"
like image 417
Ben Crowhurst Avatar asked Nov 24 '11 11:11

Ben Crowhurst


2 Answers

The latest released versions of clang, v2.9 from llvm.org or Apple's clang 3.0, do not support Unicode string literals. The latest available version, built from top of trunk source does support Unicode string literals.

The next llvm.org release of clang (i.e., 3.0) will support the Unicode string literal syntax, but does not have support for any source file encoding beyond ASCII. So even with that llvm.org release you won't be able to type in those characters literally in your source and have them converted to a UTF-16 encoded string value. Instead you'll have to use the \u escapes. Again, top of trunk does support UTF-8 source code, but it didn't get put in in time for the llvm.org 3.0 release that is currently under testing. The next release after that (in 6 months or so) should have better support for UTF-8 source code (but not other source encodings).

Edit: The Xcode 4.3 version of clang does have these features.

Edit: And now the 3.1 release from LLVM.org has them

So clang now fully supports the following:

#include <string>

int main() {
    std::u16string a = u"鵝"; // UTF-8 source is transformed into UTF-16 literal
    std::u32string b = U"滿"; // UTF-8 source is transformed into UTF-32 literal
}

It turns out the standard does not actually require much support for char16_t and char32_t in the iostreams library, so you'll probably have to convert to another string type to get much use out of this. At least the ability to convert between these and the more useful std::string is required (though not exactly convenient to set up...).

like image 56
bames53 Avatar answered Oct 18 '22 04:10

bames53


You can test clang for individual C++11 features, e.g.:

http://clang.llvm.org/docs/LanguageExtensions.html#cxx_unicode_literals

and here's a status page:

http://clang.llvm.org/cxx_status.html

like image 21
Howard Hinnant Avatar answered Oct 18 '22 04:10

Howard Hinnant