I'm working with a library that unfortunately uses boost::lexical_cast to convert from a double to a string.
I need to be able to definitively mirror that behavior on my side, but I was hopping to do so without propagating boost.
Could I be guaranteed identical behavior using to_string, sprintf, or some other function contained within the standard?
The boost code ends up here:
You're in luck since the precision is USUALLY compile-time constant (unless boost configures
BOOST_LCAST_NO_COMPILE_TIME_PRECISION).Simplifying a bit and allowing for conforming, modern standard libraries:
Mimicking Boost Lexicalcast
Regression Tests
To compare the results and check the assumptions:
Live On Coliru
Prints
Note that
mimickedandboost::lexical_cast<std::string>(double)result in exactly the same output each time.