-

   rss_rss_hh_full

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 1

:


++20.

, 28 2017 . 22:47 +
antoshkka 22:47

++20.

    C++. ( ) ++20.

    image

    :

    • Concepts C++20!
    • Ranges, Networking Coroutines/: TS.
    • : TS .

    , .

    Concepts


    Concepts ++20. , SFINAE.

    SFINAE
    `*_fast` `*_slow`, `v` `data`:

    1. `*_fast` , , T& :

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

    `*_optimal`, `*_fast`, :

    #include 
    
    template 
    void compute_vector_fast(Container& v, const Data& data) {
        std::cout << "fast\n";
        // ...
    }
    
    template 
    void compute_vector_slow(Container& v, const Data& data) {
        std::cout << "slow\n";
        // ...
    }
    
    template 
    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 
    
    template 
    concept bool VectorOperations = requires(T& v, const Data& data) {
        { v += data } -> T&;
        { v -= data } -> T&;
        { v *= data } -> T&;
        { v /= data } -> T&;
    };
    
    template 
        requires VectorOperations
    void compute_vector_optimal(Container& v, const Data& data) {
        std::cout << "fast\n";
    }
    
    template 
    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 
    #include ranges/algorithm>
    namespace ranges = std::experimental::ranges;
    
    int main () {
        //  get_some_values_and_delimiter()  ,
        //      42
        std::vector 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 TS move-only callback. ASIO BOOST_ASIO_DISABLE_HANDLER_TYPE_REQUIREMENTS. unique_ptr, callback.
    • constexpr (, ip::address).
    • : -callback allocator_type allocator_type get_allocator(); associated_allocator , get_allocator().

    , . Networking.

    Coroutines TS


    , , . .

    , Coroutines TS Networking TS. +100 , 40 :

    #include 
    #include 
    #include 
    #include 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.

    , (``, !) , ( 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 = [](Args&&... args) {
        return foo(std::forward(args)...);
    };
    

    21


    :

    • P0652R0 . , , . .
    • P0539R1 integers, ( ) . ( , ), .
    • P0639R0 `constexpr_allocator` `constexpr_string + constexpr_vector`. , . .
    • P0415R0 constexpr std::complex. LWG ( constexpr ). C++20.

      constexpr?
      ++ . constexpr- , constexpr , .

      , constexpr-: [1], [2].

    , :

    • P0457R0 starts_with ends_with . . , , . proposal, .
    • P0458R0 contains(key) member [unordered_]map/set/multimap/multiset. , LWG C++20.


    , (, , 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. !

    ? - .
    Original source: habrahabr.ru (comments, light).

    https://habrahabr.ru/post/336264/


    : [1] []
     

    :
    : 

    : ( )

    :

      URL