سي ++ 20 على الطريق! اجتماع في Rapperswil Yona

في أوائل يونيو ، انتهى اجتماع مجموعة العمل الدولية WG21 بشأن التقييس C ++ في مدينة Rapperswil-Yona.

إليك ما ينتظرك تحت الخفض:
  • العقود والأصدقاء
  • مفاهيم (لا أصدقاء)
  • __has_cpp_attribute (غير مرجح)
  • bit_cast <my_stuff> (some_array)
  • يحتوي على ، shift_left ، shift_right ، ispow2 ، ceil2 ... وخوارزميات قديمة مع صلصة جديدة
  • الذري
  • ما الجديد الذي يمكن كتابته في القوالب ومدى فائدته
  • constexpr الظاهري foo ()
  • التوازي 2 والتفكير والمنفذون TS

سيكون هناك أيضًا مكافأة: تصغير للخبراء:

  • لا يؤثر المدمر الظاهري المعلن من قبل المستخدم على تافهة النوع
  • أين يمكنني وضع علامة تعجب وكيف يمكن أن تكون مفيدة
  • constexpr std :: regex mail_regex (R "((؟: (؟: [^ ​​<> () \ [\].،؛: \ s @ \"] + (؟: \. [^ <> () \ [ \].،؛: \ s @ \ "] +) *) | \". + \ ") @ (؟: (؟: [^ ​​<> () \ [\].،؛: \ s @ \" ] + \.) + [^ <> () \ [\].،؛: \ s @ \ "] {2،}))")


العقود


في C ++ 20 ، قبلوا العقود. لذلك ، سيكون من الممكن قريبًا نسيان استخدام وحدات الماكرو للتأكيدات ، والحصول على أفضل الوثائق خارج الصندوق ، وحتى ملاحظة تعزيز الأداء. من الناحية العملية ، تبدو العقود كما يلي:

std::string get_name_by_login(std::string_view login)
    [[expects: !login.empty() ]]
    [[ensures ret_value: !ret_value.empty() ]]
;

, , . , :

  • ( ) .
  • ( ) . , , .
  • (, doxygen) / .
  • , (, ).

.

21 Fails, . , :

void(const std::contract_violation & e) noexcept {
    std::cerr << "Contract violated in function " << e.function_name() << '\n'
        << std::stacktrace();
}


:

Contract violated in function std::array<T, N>::operator[](size_type) [with T = int; long unsigned int N = 5ul; ]': 
 0# std::array<int, 5ul>::operator[](unsigned long) at /usr/include/c++/array:124
 1# bar(int) at ../example/assert_handler.cpp:17
 2# foo(int) at ../example/assert_handler.cpp:25
 3# main at ../example/assert_handler.cpp:54
 4# 0x00007F991FD69F45 in /lib/x86_64-linux-gnu/libc.so.6
 5# 0x0000000000401139
}

std::stacktrace C++20, design review LEWG, LWG. .


, Ranges TS. compile-time :

template <class F>
    requires Invocable<F>
void my_executor::execute(F f) noexcept {
    lock_guard l{data_mutex_};
    push(std::move(f));
}

proposal.

Feature-test macros


. . unlikely, __has_cpp_attribute(unlikely).

, .

bit_cast


reinterpret_cast – . , . C++ .

,

 my_type my = reinterpret_cast<my_type&>(some_array); 
my_type my = std::bit_cast<my_type>(some_array); 

, some_array my_type , , . , type aliasing.


, C++20:

  • shift_left(it_begin, it_end, unsigned n) – n, *it_begin = std::move(*(it_begin + n)), *(it_begin + 1) = std::move(*(it_begin + n + 1))...
  • shift_right(it_begin, it_end, unsigned n) – , , *(it_begin + n) = std::move(*it_begin), *(it_begin + n + 1) = std::move(*(it_begin + 1))...
  • ispow2(x) – true ,
  • ceil2(x) – ,
  • containsbool contains(const key& v), true

, , std::swap std::swap constexpr. , std::nth_element . 21.

atomic_ref


, - ? atomic_ref<T> ( ).

, , atomic_ref<T>. atomic_ref, atomic_ref, .

, (string_view, atomic_ref), . , .


X, operator<=> :

struct X {
    // ...
    std::strong_equality operator<=>(const X&, const X&) = default;
    // ...
};

:

template <X x>
struct x_as_template_param {
    // ...
};

operator<=>.

constexpr virtual


, , constexpr .

constexpr virtual int foo();, int foo() constexpr, . foo() , constexpr .

, , std::type_info, Boost.TypeIndex, compile-time :

template <class T, class U>
constexpr bool is_same() {
    constexpr bool res = (typeid(T) == typeid(U));
    return res;
}

.

Parallelism 2, Reflection Executors TS


Parallelism 2 , . type traits, simd .

Reflection (TS). , <type_traits>. constexpr constexpr! (. ).

Executors C++20, , , TS. , .

user-declared virtual destructor


, C++20 ( ), , :

struct i_am_trivial {
    int foo;
    char bar;

    virtual ~i_am_trivial() = default;
};

, , . , , , , . , , std::vector<Base>, Base .

constexpr!


, C++20 – constexpr! .

, runtime . C++.

constexpr! . .. constexpr! , ( ) . , . , , Boost.Hana [Boost.]PFR.

: constexpr std::regex


/ . , , .

C++ :

bool is_valid_mail(std::string_view mail) {
    static const std::regex mail_regex(R"((?:(?:[^<>()\[\].,;:\s@\"]+(?:\.[^<>()\[\].,;:\s@\"]+)*)|\".+\")@(?:(?:[^<>()\[\].,;:\s@\"]+\.)+[^<>()\[\].,;:\s@\"]{2,}))");

    return std::regex_match(
        std::cbegin(mail),
        std::cend(mail),
        mail_regex
    );
}

is_valid_mail(). , .

constexpr (constexpr new, is_constexpr_evaluated() .) C++ , constexpr std::regex.

constexpr std::regex is_valid_mail() . , GCC static const, .. GCC-6 constexpr – , GCC .

, constexpr std::regex?

P.S.: C++ Yandex.Taxi Coding Fest. C++17.

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


All Articles