What Are The Elementary String Conversions That Come With C++ 17?

In addition to many beneficial features of C++ 17, there are elementary string conversions introduced in that specification. The std::to_chars() and std::from_chars() are defined in header to do conversions between numeric values to strings or strings to numeric values without considering locale-specific conversions. In this post, we explain the std::to_chars() and std::from_chars() that come with C++17.

What Are The Elementary String Conversions That Come With C++ 17 ?

What is std::to_chars()?

The std::to_chars() is defined in header and is used to convert numeric values into a character string within the given valid range.

The std::to_chars() is designed to copy the numeric value into a string buffer, with a given specific format, with the only overhead of making sure that the buffer is big enough. You don’t need to consider locale-specific conversions.

Here is the syntax of std::to_chars in C++ 17.

Here is a simple example.

In floating point number conversions, std::chars_format types can be used (i.e. std::chars_format::fixed, std::chars_format::scientific, std::chars_format::general,…)

What is std::from_chars()?

The std::from_chars() is defined in the header and used to convert the string data in a given range to a value (string to int, string to float operations) if no string characters match the pattern or if the obtained value is not representable in a given type of value, then the value has remains unchanged.

The std::from_chars() is a lightweight parser that does not need to create dynamic allocation, and you don’t need to consider locale-specific conversions.

Here is the syntax of std::from_chars in C++ 17.

Here is a simple example.

Is there a full example to elementary string conversions that comes with C++ 17?

Here is a full example about std::to_chars() and std::from_chars() in C++ 17.

Here is the output.

Note that here ec is error check parameter and can be used to check error by if( ec = std::erc() ) { ... } or other error check members can be used, i.e. if (ec == std::errc::invalid_argument) { ... }. The benefits of these conversion methods are compatible with the locale-specific conversions.

For more details about this feature in C++17 standard, please see these papers; P0067R5P0682R1DR

What Are The Elementary String Conversions That Come With C++ 17

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.