0

I was happy for the standard library to get a to_string function, but now I'm in WTF mode. First of all why isn't this templated, second of all if it is not a template on return value then why in the world they don't have to_u16string() and to_u32string() functions.

I know that there is boost lexical cast, but I'm curious to know if there is a standard way to get what I want without manually writing these functions?

EDIT: to make matters worse boost 1.46 also dislikes u16string :(

boost::lexical_cast<u16string>(22.44);

terminate called after throwing an instance of 'boost::exception_detail::clone_impl

' what(): bad lexical cast: source type value could not be interpreted as target

vitaut
  • 49,672
  • 25
  • 199
  • 336
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • Possible duplicate of http://stackoverflow.com/questions/7232710/c0x-convert-between-string-u16string-u32string – Jim Jeffries Dec 01 '11 at 13:04
  • There's no `to_string` in the STL. – Lightness Races in Orbit Dec 02 '11 at 01:48
  • 2
    While I fully agree with Tomalak on the whining, I'm not sure what is not constructive about this question compared to others. There have been plenty of "why does language X do thing Y?" questions. Most of them are not closed. Yes, this question is *very* badly stated. But at it's core, it's really just asking about `std::to_string` and its ilk. – Nicol Bolas Dec 02 '11 at 03:33
  • also it asks why boost also fails to do the conversion, but I think it was answered... C++ cant do it so I guess boost cant do it easily... – NoSenseEtAl Dec 02 '11 at 09:20
  • also what if cpp gets unicode support. then I guess template writers gonna be pretty pissed that there is no std::tostring<*string_type*> I guess they can always add to_basic_string<*string_type*> though... it just looks like terrible design decision to me... – NoSenseEtAl Dec 03 '11 at 18:57
  • 1
    Ranting aside, this is a perfectly valid question, voting to reopen. – vitaut Apr 08 '18 at 22:14
  • 1
    You can easily solve this issue btw with two lines of code: `wstring_convert, char32_t> utf32conv; auto to_u32string = [&] (const auto& value) -> u32string {return utf32conv.from_bytes(to_string(value));};` – xamid Sep 23 '19 at 11:28

1 Answers1

5

Relax and take a deep breath before you collapse.

It all makes perfect sense. to_string and to_wstring use sprintf/wsprintf. There is nothing in the language that crosses the bridge between agnostic narrow/wide encoding and the UTF encodings, so there simply does not exist a standard library facility to produce UTF-encoded strings. The new UTF-facilities allow you to store UTF-encoded strings and transcode among them, but there is no way to encode a system-specific encoding to a UTF-encoding without the help of an extra library.

How would those "20 lines of code" look like that you propose? Include floating point numbers, please.

(Here are my standard question links on the subject: #1, #2, #3)

Community
  • 1
  • 1
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • So u are telling me that there is no way to convert numbers(float or int) in standard cpp to unicode strings? – NoSenseEtAl Dec 01 '11 at 13:14
  • 1
    @NoSenseEtAl: Correct. You will need an external library, such as `iconv()`. Then again, you're the one who boasted that you *could* do it in 20 lines, so I'm still curious how you propose to do that. – Kerrek SB Dec 01 '11 at 13:15
  • ok, what about doing simple to_wstring and then casting the that to u16string... I mean I really dont get why converting NUMBER to unicode string is not possible without external lib. – NoSenseEtAl Dec 01 '11 at 13:18
  • 2
    Read my linked posts. Wide strings have nothing to do with Unicode. They simply don't. And again, if you think you can do it, please do, and post! (I admit that it ought to be easy for integers.) – Kerrek SB Dec 01 '11 at 13:19
  • so u are saying that . , and - are hard parts ? – NoSenseEtAl Dec 01 '11 at 13:30
  • @Kerrek SB Not all that easy if one wishes to convert between non-ASCII Unicode numerals (say, [U+0660](http://www.fileformat.info/info/unicode/char/660/index.htm) - [U+0669](http://www.fileformat.info/info/unicode/char/669/index.htm)) and integers... perhaps I want too much, but Java can convert those characters to correct digits individually. – Cubbi Dec 01 '11 at 21:15
  • @NoSense - The standard doesn't say how a `wstring` is encoded. It could just as well be equivalent to a `u32string`. Or something else. – Bo Persson Dec 01 '11 at 22:15
  • 1
    @Cubbi: You're entirely correct, it's not trivial even for integers - it's not terribly *hard* for integers, though, if you permit `to_string()` as an intermediate step. – Kerrek SB Dec 01 '11 at 23:39
  • OK, I'm totally lost... what prohibits one to use u'-', u'.', u'0',u'1', u'2'... u'9' to do to_u16string – NoSenseEtAl Dec 02 '11 at 09:05
  • @NoSenseEtAl: yes, it should be straight-forward for integers. I had thought for a moment that `sprintf` is locale-aware, but it isn't in the default mode (`%i` and `%d`). To get floating point numbers, you'd essentially have to copy the `sprintf` code and make all the necessary adjustments. A `uNNsprintf` would be the most obvious first step, so this should be requested of the C library first of all. – Kerrek SB Dec 02 '11 at 11:32