This question is old, but in the meantime in C++ we got a "locale-independent" atof:
std::from_chars
(with its sibling std::to_chars
), added in c++17, provide locale-independent float scanning (and formatting). They are located in header <charconv>
.
You can read more about them here:
https://en.cppreference.com/w/cpp/utility/from_chars
https://en.cppreference.com/w/cpp/utility/to_chars
I recomment Stephan T. Lavavej wonderful talk about these two tools, here's the link to the part where he talks about using std::from_chars:
https://youtu.be/4P_kbF0EbZM?t=1367
And a short example by me:
#include <charconv>
#include <iostream>
#include <system_error>
int main()
{
char buffer[16] { "123.45678" };
float result;
auto [p, ec] = std::from_chars(std::begin(buffer), std::end(buffer), result);
if(ec == std::errc{})
std::cout << result;
}
Unfortunately, as for today (05.06.2020) only MSVC supports these functions with floating types. Implementing them efficiently turned out to be a big problem.
@edit (27.04.2021) libstdc++ released today with stable GCC 11.1 adds support for floating-type <charconv>
. However, this implementation seems to be not standard-compliant - it needs to copy the text into another buffer and calls strto(f/d/ld)
with default C locale and set Floating Environment, taking error from errno
. In extremely weird cases it can allocate, throw and catch exceptions underneath. You can find the implementation here:
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/src/c%2B%2B17/floating_from_chars.cc#L304