In addition to major improvements to reflection and contracts, the upcoming C++26 standard brings several small but useful additions. Today it’s all about strings and stuff string_view
,
Advertisement
Rainer Grimm has been working as a software architect, team and training manager for many years. He enjoys writing articles on the programming languages ​​C++, Python, and Haskell, but also frequently speaks at expert conferences. On his blog Modern C++ he discusses his passion C++ in depth.
First: what is A? string_view
,
std::string_view
A std::string_view
is a non-owned reference to a string. It represents a view of a string. This string can be a C++ string or a C string. Typically, C++17 provides four types of synonyms for built-in character types.

std::string_view std::basic_string_view
std::wstring_view std::basic_string_view
std::u16string_view std::basic_string_view
std::u32string_view std::basic_string_view
A question remains: why do we need it? std::string_view
Why did Google, LLVM and Bloomberg already implement String Views? The answer is simple: it is resource efficient, a std::string_view
to copy. One std::string_view
Only two pieces of information are needed: the pointer to the string and its length. As you might suspect, they exist std::string_view
And its three siblings deal primarily with read operations with the interface std::string
consequences. Mainly because they use new methods remove_prefix
And remove_suffix
Receive.
Test for success or failure ofcharconv
> work
Work std::to_chars
And std::from_chars
It was difficult to test: if(res.ec == std::errc{})
,
Here is a simplified program from cpppreference.com,
// charconv.cpp
#include
#include
#include
#include
#include
template
void show_to_chars(T value) {
const size_t buf_size = 5;
char buf(buf_size);
std::to_chars_result result = std::to_chars(buf, buf + buf_size, value);
if (result.ec != std::errc{})
std::cout << std::make_error_code(result.ec).message() << '\n';
else{
std::string_view str(buf, result.ptr - buf);
std::cout << std::quoted(str) << '\n';
}
}
int main() {
show_to_chars(42);
show_to_chars(1234567);
}
Program includes headerscharconv
>For character conversion,iomanip
>For input/output manipulators,string_view
>for the treatment of string_views
Andsystem_error
>For error handling.
function template show_to_chars
takes any kind of value T
And tries to convert it to a string. Inside the function, a buffer of size 5 is declared to store the resulting string. Celebration std::to_chars
is then called to perform the conversion and bring the result into a std::to_chars_result
-object name result
To save.
The result object contains a pointer to the end of the converted string and an error code. If the error code is not the same std::errc{}
an error message is displayed indicating an error in the conversion. std::make_error_code(result.ec).message()
Output to console. otherwise a std::string_view
An object is created to represent the converted string and is created using a sequence std::quoted
Output to the console to make sure it appears within the quotation marks.
In main
-function becomes function show_to_chars
Called twice: with first value 42
and then with the value 1234567
first call converts the value 42
Successfully converts and prints it into a string. However, the second call tries to return the value 1234567
The buffer size to convert exceeds 5, resulting in an error message printed on the console.
Finally the output of the program comes:
Thanks to C++26, the function exists std::to_chars
return a boolean and function show_to_chars
Can be simplified:
template
void show_to_chars(T value) {
std::array str;
if (auto result = std::to_chars(str.data(), str.data() + str.size(), value)) {
std::string_view strView(str.data(), result.ptr);
std::cout << std::quoted(strView) << '\n';
}
else
std::cout << std::make_error_code(result.ec).message() << '\n';
}
inside the function there is a std::array
Characters declared with a fixed size of 5 to hold the resulting string. Celebration std::to_chars
Then the conversion is called. This function tries to convert a numeric value to a string and put it into an array. str
To save. Celebration std::to_chars
enters std::to_chars_result
Object that contains a pointer to the end of the converted string and an error code.
function uses one if
-Instructions for obtaining results std::to_chars
-Call to check. If the conversion is successful, the result object is implicitly converted. true
is converted, and the function continues to print the converted string. For this purpose a std::string_view
-object strView
Generates string from beginning of str
– table up to result.ptr
The indicator shows. std::cout
-stream is used to output this string view to the console, where std::quoted
This ensures that the output is displayed within the quotation marks.
If the conversion fails, the result object is implicitly converted. false
is converted and the function returns an error message instead. You get an error message when you make a call std::make_error_code(result.ec).message()
Which converts the error code into a human-readable string.
C++26 has additional functions for processing string
Sand string_view
S:
- concatenate string stream with
std::string_view
- Concatenation of strings and string_views
- arithmetic overload of
std::to_string
and use ofstd::format
I have already introduced them in my previous post: “Overview of C++26: Libraries”.
What will happen next?
My next post reveals that there is much more to the C++26 library.
(rme)
