Home DEVELOPER Overview of C++26: Libraries

Overview of C++26: Libraries

0


As already emphasized in my comments on the core language of C++26, I can only give a first impression of the library, since the design freeze of C++26 will not happen until the first quarter of 2025. To cut a long story short, the library does not provide as many powerful features as the core language. The code examples come from the proposals.

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 speaking at specialist conferences. On his blog Modern C++ he discusses his passion C++ in depth.



Nearby tasks std::string And std::string_view Make the use of these features more convenient.

Using functions to_chars Or from_chars It’s been quite complicated so far. You should have come with us res.ec == std::errc{} Check the success of the conversion. Result is reduced with C++26 bool Change.

std::to_string raises some problems:”The choice of floating-point format makes std::to_string of very limited use in practicee” (published proposal P2587R3,

JavaScript Conference: Call for proposals for EnterJS 2025 begins
auto loc = std::locale("uk_UA.UTF-8");
std::locale::global(loc);
std::cout.imbue(loc);
setlocale(LC_ALL, "C");

std::cout << "iostreams:\n";
std::cout << 1234 << "\n";
std::cout << 1234.5 << "\n";

std::cout << "\nto_string:\n";
std::cout << std::to_string(1234) << "\n";
std::cout << std::to_string(1234.5) << "\n";

setlocale(LC_ALL, "uk_UA.UTF-8");

std::cout << "\nto_string (uk_UA.UTF-8 C locale):\n";
std::cout << std::to_string(1234) << "\n";
std::cout << std::to_string(1234.5) << "\n";

The program in the previous listing shows that the output of the floating point overload for iostreams is inconsistent. It takes the decimal point from the global C local.

Thank you for the offer P2495R3 can be a string stream from std::string_view be made. In the following example ""sv an empty string_view-Literally.

// implicitly convertable to string_view
const mystring str;

stringstream s1(""sv);
stringstream s1(str);
s2.str(""sv);

C++26 allows concatenating strings and string sequences.

std::string calculate(std::string_view prefix)
{
  return prefix + get_string(); // NO ERROR
}

In older C++ versions before C++26, there are only data types void, const void And std::nullptr_t valid. If the address of a pointer is to be displayed, it must be in std::string calculate(std::string_view prefix) { return prefix + get_string(); // NO ERROR } should be changed.

double d = 123.456789;
std::format("{}", &d); // ERROR
std::format("{}", static_cast(&d)); // okay
std::format("{}", static_cast(&d)); // okay
std::format("{}", nullptr); // okay

With C++26 the error messages disappear.

// pointerFormat.cpp

#include 
#include 

int main() {
 
     std::cout << '\n';

    double d = 123.456789;

    std::cout << std::format("{}", static_cast(&d)) << '\n';
    std::cout << std::format("{}", static_cast(&d)) << '\n';
    std::cout << std::format("{}", nullptr) << '\n';

    std::cout << '\n';

}

The output of the program then looks like this:



std::format be able to std::filesystem::path-Show objects – such as examples from the proposal P2845R8 clarifies:

auto p1 = std::filesystem::path("/usr/bin");
Ja std::cout << std::format("{}", p1);                           //  /usr/bin


auto p2 = std::filesystem::path("multi\nline");
std::cout << std::format("{}", p2);                          // multi
                                                             // line


auto p3 = std::filesystem::path("multi\nline");
std::cout << std::format("{:?}", p3);                        // "multi\nline"

Thanks for the format string. "{:?}" The last line is an escape sequence "\n" No explanation given.

std::inplace_vector The proposal is as per P0843R8 “A dynamically adjustable vector with a capacity determined at compile time and contiguous embedded memory in which the elements are stored within the vector object itself” (“A dynamically resizable vector with compile-time fixed capacity and contiguous embedded storage in which the elements are stored within the vector object itself,

This container can be used as a direct replacement for std::vector Used. But when should it be done? inplace_vector Or vector used?

A look at the proposal P0843R8 Provides the answer:

  • Memory allocation is not possible, e.g. in embedded environments without free memory, where only the stack and static memory segments are available.
  • Memory allocation shows an unacceptable impact on performance, for example in terms of latency,
  • allocation of objects with complex lifetimes is required in a static memory segment,
  • std::array is not an option, e.g. B. when non-standard manufacturable items need to be stored,
  • within a dynamically adjustable array constexpr-Work Required
  • should be the place of inplace_vector-Within the elements inplace_vector-The object itself lies (for example supporting). memcpy for numbering purposes).

The Range library gets new features: std::ranges::generate_random And std::ranges::concat_view.

Call std::ranges::generate_random(fltArray, g, d) The generator uses g and delivery dTo generate random numbers. The call is equivalent to the following loop:

for(auto& el : fltArray)
el = d(e);

A trend continuing since C++11: more and more functions are being called in C++ constexpr,

  • std::stable_sort, std::stable partition And std::inplace_merge are in C++26 constexpr.This also applies to their counterparts in the Range Library.
  • Proposal P1383R2 says: “This proposal amounts to a (further) liberal sprinkling of Constexpr a snipe as well ,

In my next post I will continue my journey through libraries in C++26.


(Map)

Overview of C++26: Libraries

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version