Skip to contentSkip to conversation

no number->string conversion in stdc++?

DISCUSSION Shriramana Sharma 2 participants 2 messages Jun 29, 2007 – Jul 1, 2007

lore
Share & tools
lore
#0 lore raw

Hello.

I learnt today to my dismay that there is in fact no number to string conversion function in the standard C++ library? I can't do simple natural things like:

std :: string a ( 2 ) ;
std :: string b ( 2.5 ) ;

without errors at compilation talking about "no matching function for call".

I find it difficult to believe that no-one thought this was necessary enough to be included in the C++ standard basic_string class.

The three ways out of this problem I got to know:

1. stringstream from std, resulting in somewhat unweildy operations
2. lexical_cast from boost, resulting in dependency on huge boost lib
3. QString from Qt, resulting in depdency on huge Qt lib

Of course stdc++ is also a huge lib, but thought I can expect stdc++ to be there on more machines than boost or Qt.

So shall I go with std::string and std::stringstream, std::string and boost::lexical_cast or simply Qt's QString? QString seems most appealing, but I would listen to your better judgment.

Shriramana Sharma.

#1 lore raw
Quoted 6 lines · Shriramana Sharma

I learnt today to my dismay that there is in fact no number to string conversion function in the standard C++ library? I can't do simple natural things like:

std :: string a ( 2 ) ;
std :: string b ( 2.5 ) ;

These conversions are neither simple nor natural.

Look at the printf() documentation, and note how much of it applies to formatting numeric values. Base, precision, field width, leading zeroes/spaces, exponential notation. And that's aside from locale issues (e.g. decimal separator), which aren't described there.

Quoted 6 lines

The three ways out of this problem I got to know:

1. stringstream from std, resulting in somewhat unweildy operations
2. lexical_cast from boost, resulting in dependency on huge boost lib
3. QString from Qt, resulting in depdency on huge Qt lib

Quoted 6 lines

Of course stdc++ is also a huge lib, but thought I can expect stdc++ to be there on more machines than boost or Qt.

So shall I go with std::string and std::stringstream, std::string and boost::lexical_cast or simply Qt's QString? QString seems most appealing, but I would listen to your better judgment.

If the std::ostream formatting operators are sufficient, then use std::ostringstream, otherwise write your own.

Don't use substantial external libraries just for a few utility functions.

Signature · 1 line
Glynn Clements <[email protected]>