C ++ 20 wird gebündelt, C ++ 23 wird gestartet. Ergebnisse des Treffens in Köln

Neulich fand in Köln eine Sitzung des Internationalen Normungsausschusses C ++ statt. Beim letzten Mal wurde das Einfrieren von Features in C ++ 20 übernommen, daher sollte das Komitee nur Korrekturen bereits akzeptierter Dinge diskutieren und neue Elemente hinzufügen, die bereits in C ++ 23 enthalten sind ...

... aber es war nicht so!



Was haben sie mit std :: flat_map gemacht? Werden die gruseligen Schlüsselwörter co_return, co_await und co_yield erhalten bleiben? Haben Sie es geschafft, std :: format zu beenden? Welche Art von Verträgen wird es in C ++ 20 geben? All dies erwartet Sie unter dem Schnitt.

Arbeitsgruppe Evolution


Montag


Der Tag war voll - wir haben beschlossen, alle Konzepte in snake_case anstelle von CamelCase umzubenennen. Darüber hinaus nahmen sie mit einem großen Stimmenspielraum den Vorschlag P1607 an , der die Syntax und das Verhalten von Verträgen verständlicher macht (aber auch Makros erfordert).

Dienstag


Corutins diskutiert. Alles abgelehnt, einschließlich unseres Vorschlags, co_ aus Schlüsselwörtern für Coroutine zu streichen . Leider.

Mittwoch


Plötzlich stellten wir fest, dass niemand den am Montag in der Praxis genehmigten P1607- Vorschlag genehmigte. Er wurde etwa 30 Minuten lang diskutiert, während die bestehende Vertragsentscheidung seit vielen Jahren präzisiert wurde .

Nach einer langen Diskussion entschieden sie, dass die Verträge im Prinzip nicht für C ++ 20 bereit sind. Und entfernte sie aus dem Standard.

Donnerstag Freitag


Besprochene Dinge für C ++ 23. Die Hauptkräfte konzentrierten sich auf neue Mechanismen bei der Fehlerbehandlung. Es gab sowohl allgemeine Gedanken zum Thema als auch spezifische Vorschläge für einen neuen Auslösespezifizierer .

Arbeitsgruppe Bibliothek


Der Ausschuss hat eine Untergruppe LWG. Jedes Dokument, das der Standardbibliothek Funktionen hinzufügt, sollte in dieser Untergruppe überprüft werden.

Der durchschnittliche Durchsatz von LWG beträgt ~ 30 Dokumente pro Woche. In Köln mussten mehr als 50 Dokumente berücksichtigt werden, von denen etwa die Hälfte äußerst beeindruckende Größen aufwies, zum Beispiel:
* std :: flat_map
* std :: jthread
* Operator <=> für die Standardbibliothek
* Neue Synchronisationsprimitive
+ Papier kam von der EWG, um Konzepte in snake_case umzubenennen.

Welcher der zuvor genehmigten hat es geschafft, auf C ++ 20 zu ziehen?


  • In constexpr ist es jetzt nicht erforderlich, jede Variable mit Null zu initialisieren . Nieder mit int i {}; es lebe int i;
  • require kann jetzt für Konstruktoren und Destruktoren verwendet werden. Dementsprechend kann es jetzt in der Klasse mehrere Destruktoren geben:

    #include <type_traits>
    
    template<typename T>
    struct Optional {
        // ...
        ~Optional() requires(std::is_trivially_destructible_v<T>) = default;
        ~Optional() requires(!std::is_trivially_destructible_v<T>) {
            if (inited_) reinterpret_cast<T&>(data_).~T();
        }
    private:
        bool inited_{false};
        std::aligned_storage_t<sizeof(T), alignof(T)> data_;
    };
    
  • [[nodiscard]] . , :

    template <class F>
    [[nodiscard("Without storing the result the code executes synchronously")]] future async(F&& );
    
    auto test() {
        // ...
    
        // warning: Without storing the result the code executes synchronously
        async([huge_data](){
            std::cerr << huge_data;
        });
    }
    
  • using enum:

    enum class rgba_channel { kRed, kGreen, kBlue, kAlpha};
    
    std::string_view to_string(rgba_channel channel) {
      using enum rgba_channel;
      switch (channel) {
        case kRed:   return "red";
        case kGreen: return "green";
        case kBlue:  return "blue";
        case kAlpha: return "alpha";
      }
    }
    
  • Deduction guides :

    template <typename T>
    struct S {
        T x;
        T y;
    };
    
    S t{'4', '2'}; // Deduces `S<char>`
    
  • __asm constexpr ( !)
  • . :

    void f(int(&)[]); // p.s.:        
    int arr[1];
    
    f(arr);          // OK
    
  • C++20
  • constinit :

    int count_invocations() {
        //   ,   
        //   `counter`  .
        //
        //   -  ,  
        //         
        // `counter`   .
        static constinit std::atomic<int> counter{0};
    
        return ++counter;
    }
    
  • .
  • volatile deprecated. .
  • [[nodiscard]] , :

    struct [[nodiscard]] my_scopeguard { /* ... */ };
    struct my_unique {
        [[nodiscard]] my_unique(int fd) { /* ... */ } //    
        /* ... */
    };
    
    void sample() {
        my_scopeguard(); // warning
        void(my_scopeguard()); // cast  void, warning  
        my_unique(42); // warning
    }
    
  • Class Template Argument Deduction
  • std::vector placement new constexpr
  • <bit> . , / /, — .
  • <format> , chrono locale float:

    constexpr auto birthday = 28d/April/1989;
    string s = format("At {0:%d} of {0:%B}, {0:%Y} someone was born", birthday);
    assert(s == "At 28 of April, 1989 someone was born");
    
  • constexpr bind, invoke, reference_wrapper
  • <numbers>
  • wait notify. conditional_variable:

    #include <atomic>
    
    enum class States {
        kInitial, kProcessing, kPosting,
    };
    
    // !  !
    std::atomic<States> state{States::kInitial};
    
    void state_machine_do_posting() {
        for (;;) {
            States expected_state = States::kProcessing;
    
            //        kProcessing
            state.wait(expected_state);
    
            if (!state.compare_exchange_strong(expected_state, States::kPosting)) {
                continue;
            }
    
            // do something
        }
    }
    
  • snake_case
  • operator != operator ==. , operator <=>
  • std::*stringstream std::basic_stringbuf :

    std::string to_string(const MyType& v) {
        std::string buf;
        constexpr std::size_t kMaxSize = 32;
        buf.reserve(kMaxSize);
    
        //  C++20  ,  C++20 —   
        std::ostringstream oss{std::move(buf)};
        oss << "MyType{" << v << '}';
    
        //  C++20  ,  C++20 — 
        return std::move(oss).str();
    }
    
  • std::jthread:

    #include <thread>
    
    void sample() {
        bool ready = false;
        std::mutex ready_mutex;
        std::condition_variable_any ready_cv; //   `_any`!
    
        std::jthread t([&ready, &ready_mutex, &ready_cv] (std::stop_token st) {
            while (!st.stop_requested()) {
              /* ... */
              {
                std::unique_lock lock{ready_mutex};
    
                //    ready == true,  stop_token.request_stop(),
                //  jthread.request_stop().
                ready_cv.wait_until(lock, [&ready] { return ready; }, st);
              }
              /* ... */
            }
        });
    
        /* ... */
    
        //  `t`  request_stop()       .
    }
    
  • type_traits , .

    antoshkka C++14 CppCon. , . … , .

    , . , type_traits :

    Over dinner at CppCon, Marshall Clow and I discussed a bit of code that relied on two types being layout-compatible. As it happened, the types weren’t layout-compatible after all. I opined that there should be a way to statically assert layout-compatibility, so that the error would be caught at compile time, rather than dinner time. Marshall replied, “Write a proposal.” This is that proposal.
  • std::source_location, , .
  • unordered . .

, std::stacktrace, std::flat_map, std::flat_set C++20 :(

++23


, Boost.Process , /, , , 2d , , JIT C++ .

. std::filesystem::path_view. path_view , .

21


, , , , , , . , , std::format Inf/NaN; jthread; pair, tuple, string, array.

SG1 Concurrency concurrent_unordered_map. visit value , :

concurrent_unordered_map<int, std::string> conc_map;
conc_map.visit(42, [](std::string& value) { // ,  ....
    //    OK.  OK        value.
    std::cerr << value; 

    //    OK.  OK        value.
    value += "Hello"; 
});
.
, :

concurrent_unordered_map<int, std::atomic<int>> conc_map;
conc_map.visit(42, [](std::atomic<int>& value) { // ,  ....
    //  OK
    ++ value; 
});

SG6 Numerics — Numerics TS , wide_integer .


C++ Piter, .

21 ISO 9 ( , ). C++20 C++23.

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


All Articles