📜 ⬆️ ⬇️

One step closer to C ++ 20. Results of the meeting in Toronto

A few weeks ago, a meeting of the C ++ International Standardization Committee took place. On it, people (mostly) have not been exchanged for trifles and have taken several big steps towards C ++ 20.

image

Main news:
')

What does all this mean, how it will simplify writing code, and what else was there - read under the cut.

Concepts


A wonderful thing called Concepts is included in the draft of the future standard C ++ 20. This is a great joy for developers of generic libraries using the SFINAE idiom.

A motivating example for SFINAE fans
Your library has functions `* _fast` and` * _slow`, which accept two template parameters `v` and` data` as input:

  1. `* _fast` - very optimized, but require the following operations to return T & and to be valid:

        v += data;
        v -= data;
        v *= data;
        v /= data;
    
  2. `*_slow` — , .

— `*_optimal`, `*_fast`, :

#include <iostream>

template <class Container, class Data>
void compute_vector_fast(Container& v, const Data& data) {
    std::cout << "fast\n";
    // ...
}

template <class Container, class Data>
void compute_vector_slow(Container& v, const Data& data) {
    std::cout << "slow\n";
    // ...
}

template <class Container, class Data>
void compute_vector_optimal(Container& v, const Data& data) {
    // ??? call `compute_vector_slow(v, data)` or `compute_vector_fast(v, data)` ???
}

, , `std::enable_if_t` .

:

#include <iostream>

template <class T, class Data>
concept bool VectorOperations = requires(T& v, const Data& data) {
    { v += data } -> T&;
    { v -= data } -> T&;
    { v *= data } -> T&;
    { v /= data } -> T&;
};

template <class Container, class Data>
    requires VectorOperations<Container, Data>
void compute_vector_optimal(Container& v, const Data& data) {
    std::cout << "fast\n";
}

template <class Container, class Data>
void compute_vector_optimal(Container& v, const Data& data) {
    std::cout << "slow\n";
}


:


GCC, -fconcepts, , . proposal Concepts.

Ranges TS


Ranges . , C++20.

Ranges `sort(container)` `sort(container.begin(), container.end())`, namespace.

. , , , :

#include <vector>
#include <experimantal/ranges/algorithm>
namespace ranges = std::experimental::ranges;

int main () {
    //  get_some_values_and_delimiter()  ,
    //      42
    std::vector<int> v2 = get_some_values_and_delimiter();

    //    42    ,   :
    auto it = ranges::find(v.begin(), ranges::unreachable{}, 42);
    ranges::sort(++it, v.end());
}

.

SFINAE Ranges , : Sortable, Movable, Copyable, DefaultConstructible, Same…

, . Ranges.

Networking TS


, ( ), C++20. Networking TS ASIO.

:


, . Networking.

Coroutines TS


— « , , ». .
@masterspline
, MS, stackless. . — ( C++, ), , «». stackless , , , -, operatop()(), Duff's device switch() .

, Coroutines TS Networking TS. +100 , 40 :

#include <ctime>
#include <iostream>
#include <string>
#include <experimental/net>

using net = std::experimental::net;
using net::ip::tcp;

std::string make_daytime_string() {
    using namespace std; // For time_t, time and ctime;
    time_t now = time(0);
    return ctime(&now);
}

void start_accept(net::io_context& io_service) {
    tcp::acceptor acceptor{io_service, tcp::endpoint(tcp::v4(), 13)};

    while (1) {
        tcp::socket socket(acceptor.get_io_service());
        auto error = co_await acceptor.async_accept(socket, net::co_future);
        if (error) break;

        std::string message = make_daytime_string();
        auto& [error, bytes] = co_await async_write(
            socket, net::buffer(message), net::co_future
        );
        if (error) break;
    }
}

int main() {
    net::io_context io_service;
    io_service.post([&io_service](){
        try {
            start_accept(io_service);
        } catch (const std::exception& e) {
            std::cerr << e.what() << std::endl;
        }
    });

    io_service.run();
}

: Coroutines TS Networking TS . .

CLANG-6.0, -stdlib=libc++ -fcoroutines-ts, , . Coroutines.


TS. !

?
, cpp ( , ).

, , cpp. , cpp , . ( iostream , 20 — 30 cpp).

! — , . , ( , , ).

, . . , , . . , .

, — . ( inline/force_inline, 100 , — 99 ). , «» cpp.

, (`<windows.h>`, !) , ( std::string , multiple definitions, include guards ). , ( ).

Modules.

, C++20


C++20 bitfields :

struct S {
    unsigned x1:8 = 42;
    unsigned x2:8 { 42 };
};

endianness :

if constexpr (std::endian::native == std::endian::big) {
    // big endian
} else if constexpr (std::endian::native == std::endian::little) {
    // little endian
} else {
    // mixed endian
}

, C:

struct foo { int a; int b; int c; };
foo b{.a = 1, .b = 2};

:

auto bar = []<class... Args>(Args&&... args) {
    return foo(std::forward<Args>(args)...);
};

21


:


, :



, (, , Python):

fmt::format("The answer is {}", 42);

ring_span, boost::circular_buffer, ( view ).

. , « X?» «std::popcount(X)».


21 std::stacktrace ( Boost.Stacktrace), std::shared_library .

C++20, C++17/14/11 C++ — stdcpp.ru. !

? - .

Source: https://habr.com/ru/post/336264/


All Articles