C ++ 20に一歩近づいた。 トロントでの会議の結果

数週間前、国際標準化委員会C ++の会議が開催されました。 その上、人々は(ほとんど)些細なことと交換せず、C ++ 20に向けていくつかの大きな一歩を踏み出しました。

画像

速報:


このすべてが何を意味するのか、コードの記述をどのように簡素化するのか、そして他に何があったのか-猫の下で読んでください。

コンセプト


Conceptsと呼ばれる素晴らしいものは、将来のC ++ 20標準のドラフトに含まれています。 これは、SFINAEイディオムを使用する汎用ライブラリの開発者にとって大きな喜びです。

SFINAE愛好家のやる気を起こさせる例
ライブラリには関数 `* _fast`および` * _slow`があり、2つのテンプレートパラメーター `v`および` data`を入力として受け取ります。

  1. `* _fast`-高度に最適化されていますが、T&を返し有効にするために次の操作が必要です。

        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/J336264/


All Articles