What's the difference between std::to_string, boost::to_string, and boost::lexical_cast<std::string>?
Asked Answered
E

2

26

What's the purpose of boost::to_string (found in boost/exception/to_string.hpp) and how does it differ from boost::lexical_cast<std::string> and std::to_string?

Esmeraldaesmerelda answered 1/4, 2015 at 19:20 Comment(3)
If I recall the history correctly, boost::to_string preceded std::to_string, as is typically the case. Boost tends to be the playground for stuff before it gets accepted into the standard library. std::to_string is new as of C++11.Roose
@Cyber: I'd suggest you write this as a (partial) answer.Roslyn
@CoryKramer, boost::to_string might be older, but it's not the same as std::to_string and std::to_string was not based on it (they just happen to use the same name).Teodora
A
35

std::to_string, available since C++11, works on fundamental numeric types specifically. It also has a std::to_wstring variant.

It is designed to produce the same results that sprintf would.

You may choose this form to avoid dependencies on external libraries/headers.


The throw-on-failure function boost::lexical_cast<std::string> and its non-throwing cousin boost::conversion::try_lexical_convert work on any type that can be inserted into a std::ostream, including types from other libraries or your own code.

Optimized specializations exist for common types, with the generic form resembling:

template< typename OutType, typename InType >
OutType lexical_cast( const InType & input ) 
{
    // Insert parameter to an iostream
    std::stringstream temp_stream;
    temp_stream << input;

    // Extract output type from the same iostream
    OutType output;
    temp_stream >> output;
    return output;
}

You may choose this form to leverage greater flexibility of input types in generic functions, or to produce a std::string from a type that you know isn't a fundamental numeric type.


boost::to_string isn't directly documented, and seems to be for internal use primarily. Its functionality behaves like lexical_cast<std::string>, not std::to_string.

Ance answered 1/4, 2015 at 19:44 Comment(0)
Y
12

There are more differences: boost::lexical_cast works a bit different when converting double to string. Please consider the following code:

#include <limits>
#include <iostream>

#include "boost/lexical_cast.hpp"

int main()
{
    double maxDouble = std::numeric_limits<double>::max();
    std::string str(std::to_string(maxDouble));

    std::cout << "std::to_string(" << maxDouble << ") == " << str << std::endl;
    std::cout << "boost::lexical_cast<std::string>(" << maxDouble << ") == "
              << boost::lexical_cast<std::string>(maxDouble) << std::endl;

    return 0;
}

Results

$ ./to_string
std::to_string(1.79769e+308) == 179769313486231570814527423731704356798070600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
boost::lexical_cast<std::string>(1.79769e+308) == 1.7976931348623157e+308

As you can see, the boost version uses exponential notation (1.7976931348623157e+308) whereas std::to_string prints every digit, and six decimal places. One may be more useful than another for your purposes. I personally find the boost version more readable.

Yearwood answered 17/1, 2017 at 13:33 Comment(3)
Please do a bit more than just pasting some code. Describe what your code does and how it does it.Aslant
It is simple the output of to_string() and lexical_cast() for the same double value ;-))Yearwood
Those results certainly differ - and that's worth noting. But it's not obvious to me which result is more "right".Ance

© 2022 - 2024 — McMap. All rights reserved.