• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Trouble with std::basic_ostringstream and libc++
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trouble with std::basic_ostringstream and libc++


  • Subject: Re: Trouble with std::basic_ostringstream and libc++
  • From: Howard Hinnant <email@hidden>
  • Date: Mon, 09 Jul 2012 11:49:33 -0400

On Jul 9, 2012, at 11:33 AM, Jesper Papmehl-Dufay wrote:

> 9 jul 2012 kl. 16.58 skrev Howard Hinnant:
>
>> On Jul 9, 2012, at 10:15 AM, Jesper Papmehl-Dufay wrote:
>>
>>> I am having trouble getting code that uses std::basic_ostringstream to work when building with libc++. The same code works without any problems when building using libstdc++. (In both cases I’m building with Xcode 4.3.3, Apple LLVM compiler 3.1, and C++ Language Dialect set to C++11.)
>>>
>>> For example, the following program fails to compile:
>>>
>>> #include <sstream>
>>>
>>> int main(int argc, const char* argv[]) {
>>>  std::basic_ostringstream<int> myStream;
>>>  return 0;
>>> }
>>>
>>> with the error messages listed at the end of this mail.
>>>
>>> If I change std::basic_ostringstream<int> to std::ostringstream, it builds with our any problems (though I actually need a specific character type, so I cannot use ostringstream in my real program).
>>> And if I change the standard C++ library to libstdc++, it also builds without any problems.
>>>
>>> Does anyone have any idea what I might be doing wrong? Or is this simply a bug in libc++?
>>
>> The stream classes are only defined to work with char and wchar_t because Table 81 defines only ctype<char> and ctype<wchar_t>.
>>
>> Will std::basic_ostringstream<wchar_t> myStream work for you?
>
> Thanks for the reply!
> We have a cross-platform code base, and need a consistent and deterministic bit size and encoding (utf-16) for our strings. So wchar_t/wstring isn’t really an option for us (since it is, as far as I know, pretty undefined both when it comes to size and character encoding).
> Anyway, thanks for the clear reply, I guess we’ll have to find some other way (other than basic_ostringstream) to solve this.

C++11 has support for UTF-16 characters, and UTF-16 to UTF-8 conversions.

The character type is char16_t.  Unfortunately char16_t isn't directly supported by the stream classes either.  But there is a convenience converter in <locale> named wstring_convert.  You can use that to convert UTF-16 strings to UTF-8 strings and then stream those UTF-8 strings to the char-based streams.  It would look something like this:

#include <sstream>
#include <string>
#include <codecvt>
#include <locale>
#include <iostream>

int
main()
{
    // Specify a UTF-16 to UTF-8 converter type
    typedef std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> Converter;
    // Create a UTF-16 string
    std::u16string text(u"Time = 16\u00b5s");
    // Instantiate a converter
    Converter conv;
    std::ostringstream myStream;
    // Stream your UTF-16 to an ostringstream by first converting it to UTF-8
    myStream << conv.to_bytes(text);
    std::cout << myStream.str() << '\n';
    // Or just go straight to std::cout from the converter
    std::cout << conv.to_bytes(text) << '\n';
}

Time = 16µs
Time = 16µs

Hth.

Howard


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Trouble with std::basic_ostringstream and libc++
      • From: Jesper Papmehl-Dufay <email@hidden>
References: 
 >Trouble with std::basic_ostringstream and libc++ (From: Jesper Papmehl-Dufay <email@hidden>)
 >Re: Trouble with std::basic_ostringstream and libc++ (From: Howard Hinnant <email@hidden>)
 >Re: Trouble with std::basic_ostringstream and libc++ (From: Jesper Papmehl-Dufay <email@hidden>)

  • Prev by Date: Re: Trouble with std::basic_ostringstream and libc++
  • Next by Date: Re: Trouble with std::basic_ostringstream and libc++
  • Previous by thread: Re: Trouble with std::basic_ostringstream and libc++
  • Next by thread: Re: Trouble with std::basic_ostringstream and libc++
  • Index(es):
    • Date
    • Thread