Recently, I wanted to use from_chars
from c++17.
Looked at http://en.cppreference.com/w/cpp/utility/from_chars and found that code on this page:
#include <iostream>
#include <charconv>
#include <array>
int main()
{
std::array<char, 10> str{"42"};
int result;
std::from_chars(str.data(), str.data()+str.size(), result);
std::cout << result;
}
cannot be compiled by any of compilers. I tried on page http://en.cppreference.com/w/cpp/utility/from_chars and on godbolt with different compilers but all of them returned the same error:
<source>:2:10: fatal error: 'charconv' file not found
#include <charconv>
^~~~~~~~~~
1 error generated.
Compiler returned: 1
Can anybody help me with this, please?
(I tried clang 6.0, gcc 7.3 and msvc 19 but all of them returned error that 'charconv' not found)
C++17 implementation status You can use Clang in C++17 mode with the -std=c++17 option (use -std=c++1z in Clang 4 and earlier).
The Clang tool is a front end compiler that is used to compile programming languages such as C++, C, Objective C++ and Objective C into machine code. Clang is also used as a compiler for frameworks like OpenMP, OpenCL, RenderScript, CUDA and HIP.
GCC and C99 allow an array's size to be determined at run time. This extension is not permitted in standard C++. However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs. If you would prefer not to use this extension, you can disable it with -Werror=vla.
According to GCC's libstdc++ status page, this header is only supported starting from GCC-8.1. You could either avoid using this header by detecting it:
#if __has_include(<charconv>)
#include <charconv>
#else
/* implement some fallback */
#endif
Or just update your compiler. This godbolt example confirms that the header is present in GCC-8.1.
If you use Clang, remember that Clang uses GCC's stdlibc++
by default. You will either need to update your GCC or switch to Clang's libc++
by:
clang++ -std=c++17 -stdlib=libc++ main.cpp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With