78

Does the C++ Standard Library define this function, or do I have to resort to Boost?

I searched the web and couldn't find anything except Boost, but I thought I'd better ask here.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
smallB
  • 16,662
  • 33
  • 107
  • 151

5 Answers5

93

Only partially.

C++11 <string> has std::to_string for the built-in types:

[n3290: 21.5/7]:

string to_string(int val);
string to_string(unsigned val);
string to_string(long val);
string to_string(unsigned long val);
string to_string(long long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);

Returns: Each function returns a string object holding the character representation of the value of its argument that would be generated by calling sprintf(buf, fmt, val) with a format specifier of "%d", "%u", "%ld", "%lu", "%lld", "%llu", "%f", "%f", or "%Lf", respectively, where buf designates an internal character buffer of sufficient size.

There are also the following that go the other way around:

[n3290: 21.5/1, 21.5/4]:

int stoi(const string& str, size_t *idx = 0, int base = 10);
long stol(const string& str, size_t *idx = 0, int base = 10);
unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);
long long stoll(const string& str, size_t *idx = 0, int base = 10);
unsigned long long stoull(const string& str, size_t *idx = 0, int base = 10);
float stof(const string& str, size_t *idx = 0);
double stod(const string& str, size_t *idx = 0);
long double stold(const string& str, size_t *idx = 0);

However, there's nothing generic that you can use (at least not until TR2, maybe!), and nothing at all in C++03.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    Hum, actually there is [`stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) and friends :) – Matthieu M. Nov 09 '11 at 14:01
  • @MatthieuM.: Oh, hey.. so there is! Thanks. – Lightness Races in Orbit Nov 09 '11 at 14:10
  • Stroustrup in his FAQ on c++11 uses it http://www.stroustrup.com/C++11FAQ.html#constexpr in the section on delegating constructors – mcheema Nov 14 '12 at 08:46
  • 3
    @mcheema: http://www.stroustrup.com/C++11FAQ.html#delegating-ctor then. Either he means Boost, or he's using it as an arbitrary abstraction for the purpose of making a point, or he's wrong. – Lightness Races in Orbit Nov 14 '12 at 10:23
  • OK thanks @LightnessRacesinOrbit I just assumed that the FAQ would be written in pure C++11 style given Stroustrup's interest in spreading good coding style practices. – mcheema Nov 19 '12 at 14:19
  • 2
    @mcheema: We can all be interested in spreading good coding style practices, but that doesn't make them any less subjective! – Lightness Races in Orbit Nov 19 '12 at 14:35
  • 3
    @mcheema Remembering Stroustrup's addiction of using bold italic proportional font for code, I don't think one can ever seriously talk about the style of his code. _*joke*_ – polkovnikov.ph Dec 13 '13 at 00:23
  • 1
    @LightnessRacesinOrbit I haven't seen any more rumblings about this making it into a Technical Specification. Did it die T.T – Jonathan Mee Jan 23 '15 at 16:44
  • 1
    @JonathanMee: If anything I'd expect to see it in the Lib Fundamentals TS but there's no evidence of that. Looks to me like it died but I'm not certain. – Lightness Races in Orbit Jan 23 '15 at 16:56
20

No it isn't, even in C++11, but it's proposed for inclusion in Technical Report 2, the next set of std library extensions.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 17
    What's the status of that for C++17, by the way? – einpoklum May 13 '16 at 08:22
  • According to Wikipedia there will be not TR2 because ISO procedures were changed, but Technical Specifications only (https://en.wikipedia.org/wiki/C%2B%2B_Technical_Report_1#Technical_Report_2). – Matias Haeussler Jan 22 '20 at 19:50
11

There's no std::lexical_cast, but you can always do something similar with stringstreams:

template <typename T>
T lexical_cast(const std::string& str)
{
    T var;
    std::istringstream iss;
    iss.str(str);
    iss >> var;
    // deal with any error bits that may have been set on the stream
    return var;
}
luke
  • 36,103
  • 8
  • 58
  • 81
  • 3
    thanks but that would be even slower than boost::lexical_cast – smallB Nov 09 '11 at 13:23
  • 4
    A stream based solution isn't going to be as fast as something like snprintf, but you didn't mention performance concerns in your question. – luke Nov 09 '11 at 13:32
  • I'm kidding now, but when we talk about C++ solutions isn't performance concerns given ;)? Anyway boost also uses streams but they do also specialize them that's why it's very hard to beat them. – smallB Nov 09 '11 at 13:48
  • @smallB: note that `boost::lexical_cast` degenerates to this version when types are unknown. Also, you need not only deal with error bits, but also check that the buffer has been emptied. – Matthieu M. Nov 09 '11 at 13:52
  • 13
    Performance is always a concern, but it is never the only concern. Simplicity, clarity, correctness, and maintainability are also very important. I wouldn't use the above code in a tight loop, but it might be just fine for other situations. A common question in regards to performance on SO is if you've profiled your code, and found that the sacrifice of other concerns are worthwhile :) – luke Nov 09 '11 at 14:01
  • 8
    If you end up using `lexical_cast(string)` this will only return the first word, not what you pass in. (You might use it in a templated function or something, not directly.) Something to look out for. – tmandry Mar 01 '13 at 03:19
  • 2
    This version can be sped up a lot by making `iss` static – M.M Dec 27 '14 at 00:23
  • 2
    @MattMcNabb, maybe but then you've have to reset the ISS's buffer every time, and it wouldn't be thread safe. It was a simple example. Making it fool proof is an exercise for the reader/Boost library ;) – luke Dec 27 '14 at 20:59
  • While the idea is correct, this answer has it backwards. The function declaration should be 'template const std::string lexical_cast(const T& val) { ... } – StarShine Sep 10 '15 at 13:57
6

If you don't want boost then a lightweight library called fmt implements the following:

// Works with all the C++11 features and AFAIK faster then boost or standard c++11
std::string string_num = fmt::format_int(123456789).str(); // or .c_str()

More examples from the official page.

Accessing arguments by position:

format("{0}, {1}, {2}", 'a', 'b', 'c');
// Result: "a, b, c"
format("{}, {}, {}", 'a', 'b', 'c');
// Result: "a, b, c"
format("{2}, {1}, {0}", 'a', 'b', 'c');
// Result: "c, b, a"
format("{0}{1}{0}", "abra", "cad");  // arguments' indices can be repeated
// Result: "abracadabra"

Aligning the text and specifying a width:

format("{:<30}", "left aligned");
// Result: "left aligned                  "
format("{:>30}", "right aligned");
// Result: "                 right aligned"
format("{:^30}", "centered");
// Result: "           centered           "
format("{:*^30}", "centered");  // use '*' as a fill char
// Result: "***********centered***********"

Replacing %+f, %-f, and % f and specifying a sign:

format("{:+f}; {:+f}", 3.14, -3.14);  // show it always
// Result: "+3.140000; -3.140000"
format("{: f}; {: f}", 3.14, -3.14);  // show a space for positive numbers
// Result: " 3.140000; -3.140000"
format("{:-f}; {:-f}", 3.14, -3.14);  // show only the minus -- same as '{:f}; {:f}'
// Result: "3.140000; -3.140000"

Replacing %x and %o and converting the value to different bases:

format("int: {0:d};  hex: {0:x};  oct: {0:o}; bin: {0:b}", 42);
// Result: "int: 42;  hex: 2a;  oct: 52; bin: 101010"
// with 0x or 0 or 0b as prefix:
format("int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}", 42);
// Result: "int: 42;  hex: 0x2a;  oct: 052;  bin: 0b101010"
vitaut
  • 49,672
  • 25
  • 199
  • 336
SLC
  • 2,167
  • 2
  • 28
  • 46
  • 1
    I really like the ``fmt`` library and like to see its usage promoted. ``fmt``, however, will only do the *to string* conversion. (Now it does that better than any other C++ library I have seen.) ``fmt`` does not do the *from string* conversion performed by lexical_cast or std::stoxyz functions. – Phil Sep 15 '16 at 16:16
6

No it's a pure Boost thing only.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621