You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`
if (ec == std::errc{}) { std::cout << n << std::endl; } // 123
652
+
else { /* handle failure */ }
653
+
```
654
+
617
655
## Acknowledgements
618
656
* [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features.
619
657
* [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.
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`
0 commit comments