Skip to content

Commit 137f8ac

Browse files
Add string conversion using std::to_chars and std::from_chars.
1 parent c0e0f37 commit 137f8ac

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

CPP17.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ C++17 includes the following new library features:
3737
- [prefix sum algorithms](#prefix-sum-algorithms)
3838
- [gcd and lcm](#gcd-and-lcm)
3939
- [std::not_fn](#stdnot_fn)
40+
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
4041

4142
## C++17 Language Features
4243

@@ -614,6 +615,43 @@ std::copy_if(std::cbegin(v), std::cend(v), ostream_it, is_even); // 0 2 4
614615
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, std::not_fn(is_even)); // 1 3
615616
```
616617
618+
### String conversion to/from numbers
619+
Convert integrals and floats to a string or vice-versa. Conversions are non-throwing, do not allocate, and are more secure than the equivalents from the C standard library.
620+
621+
Users are responsible for allocating enough storage required for `std::to_chars`, or the function will fail by setting the error code object in its return value.
622+
623+
These functions allow you to optionally pass a base (defaults to base-10) or a format specifier for floating type input.
624+
625+
* `std::to_chars` returns a (non-const) char pointer which is one-past-the-end of the string that the function wrote to inside the given buffer, and an error code object.
626+
* `std::from_chars` returns a const char pointer which on success is equal to the end pointer passed to the function, and an error code object.
627+
628+
Both error code objects returned from these functions are equal to the default-initialized error code object on success.
629+
630+
Convert the number `123` to a `std::string`:
631+
```c++
632+
const int n = 123;
633+
634+
// Can use any container, string, array, etc.
635+
std::string str;
636+
str.resize(3); // hold enough storage for each digit of `n`
637+
638+
const auto [ ptr, ec ] = std::to_chars(str.data(), str.data() + str.size(), n);
639+
640+
if (ec == std::errc{}) { std::cout << str << std::endl; } // 123
641+
else { /* handle failure */ }
642+
```
643+
644+
Convert from a `std::string` with value `"123"` to an integer:
645+
```c++
646+
const std::string str{ "123" };
647+
int n;
648+
649+
const auto [ ptr, ec ] = std::from_chars(str.data(), str.data() + str.size(), n);
650+
651+
if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
652+
else { /* handle failure */ }
653+
```
654+
617655
## Acknowledgements
618656
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
619657
* [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics.

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ C++17 includes the following new library features:
6767
- [prefix sum algorithms](#prefix-sum-algorithms)
6868
- [gcd and lcm](#gcd-and-lcm)
6969
- [std::not_fn](#stdnot_fn)
70+
- [string conversion to/from numbers](#string-conversion-tofrom-numbers)
7071

7172
C++14 includes the following new language features:
7273
- [binary literals](#binary-literals)
@@ -1278,6 +1279,43 @@ std::copy_if(std::cbegin(v), std::cend(v), ostream_it, is_even); // 0 2 4
12781279
std::copy_if(std::cbegin(v), std::cend(v), ostream_it, std::not_fn(is_even)); // 1 3
12791280
```
12801281
1282+
### String conversion to/from numbers
1283+
Convert integrals and floats to a string or vice-versa. Conversions are non-throwing, do not allocate, and are more secure than the equivalents from the C standard library.
1284+
1285+
Users are responsible for allocating enough storage required for `std::to_chars`, or the function will fail by setting the error code object in its return value.
1286+
1287+
These functions allow you to optionally pass a base (defaults to base-10) or a format specifier for floating type input.
1288+
1289+
* `std::to_chars` returns a (non-const) char pointer which is one-past-the-end of the string that the function wrote to inside the given buffer, and an error code object.
1290+
* `std::from_chars` returns a const char pointer which on success is equal to the end pointer passed to the function, and an error code object.
1291+
1292+
Both error code objects returned from these functions are equal to the default-initialized error code object on success.
1293+
1294+
Convert the number `123` to a `std::string`:
1295+
```c++
1296+
const int n = 123;
1297+
1298+
// Can use any container, string, array, etc.
1299+
std::string str;
1300+
str.resize(3); // hold enough storage for each digit of `n`
1301+
1302+
const auto [ ptr, ec ] = std::to_chars(str.data(), str.data() + str.size(), n);
1303+
1304+
if (ec == std::errc{}) { std::cout << str << std::endl; } // 123
1305+
else { /* handle failure */ }
1306+
```
1307+
1308+
Convert from a `std::string` with value `"123"` to an integer:
1309+
```c++
1310+
const std::string str{ "123" };
1311+
int n;
1312+
1313+
const auto [ ptr, ec ] = std::from_chars(str.data(), str.data() + str.size(), n);
1314+
1315+
if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
1316+
else { /* handle failure */ }
1317+
```
1318+
12811319
## C++14 Language Features
12821320
12831321
### Binary literals

0 commit comments

Comments
 (0)