C ++ 20へのもう1つの小さなステップ。 アルバカーキでの会議

カナダの中央部から米国南西部へ! アルバカーキはニューメキシコ州にあります。







この都市で開催されたC ++標準化国際委員会の会議で、彼らはC ++ 20の非常に大きなイノベーションといくつかの小さなイノベーションを採用しました。



演算子<=>



宇宙船演算子がC ++ 20ドラフトに追加され、1回の操作で要素の比率を決定できます。 単純な場合、これは、宇宙船演算子を定義できることを意味し、クラスは<、>、<=、> =、==および!=のいずれかの方法で比較することを学びます。 例:

宇宙船オペレーターなし 宇宙船オペレーターと
struct point3d {
    int x;
    int y;
    int z;
};

using p_ref = const point3d&;

bool operator==(p_ref a, p_ref b) {
    return a.x == b.x
        && a.y == b.y
        && a.z == b.z
    ;
}
bool operator< (p_ref a, p_ref b) {
    return a.x < b.x
        || (a.x == b.x && a.y < b.y)
        || (a.x == b.x && a.y == b.y
            && a.z < b.z)
    ;
}

bool operator!=(p_ref a, p_ref b) {
    return !(a == b);
}
bool operator<=(p_ref a, p_ref b) {
    return !(b < a);
}
bool operator> (p_ref a, p_ref b) {
    return b < a;
}
bool operator>=(p_ref a, p_ref b) {
    return !(a < b);
}

      
      





struct point3d {
    int x;
    int y;
    int z;

    auto operator<=>(const point3d&)
        const = default;
};

      
      



:



class weak_equality;
class strong_equality;
class partial_ordering;
class weak_ordering;
class strong_ordering;
      
      





, , - :



#include <compare> // weak_ordering, is_neq

struct point3d {
    int x;
    int y;
    int z;

    std::weak_ordering operator<=>(const point3d& p) const {
        using std::abs;
        if (auto cmp = abs(z) <=> abs(p.z); std::is_neq(cmp)) return cmp;
        if (auto cmp = abs(x) <=> abs(p.x); std::is_neq(cmp)) return cmp;
        return abs(y) <=> abs(p.y);
    }
};
      
      





. spaceship *.



, :



lexicographical_compare_3way(InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2);
      
      





, , .



osyncstream



std::cout/std::cerr :



#include <iostream>
#include <thread>
#include <string_view>

void say_hello(std::string_view username) {
    std::cerr << "Hello " << username;
}

void beware_of(std::string_view something) {
    std::cerr << something << " is dangerous";
}

int main() {
    std::thread t1([]{
        say_hello("Rachel");
    });

    std::thread t2([]{
        beware_of("darkness");
    });

    std::cerr << '\n';
 
    t2.join();
    t1.join();

    /* Possible output:
            Hello darkness
            Rachel is dangerous
    */
}

      
      





, C++20 , :



#include <iostream>
#include <thread>
#include <string_view>

void say_hello(std::string_view username) {
    std::osyncstream{std::cerr} << "Hello " << username;
}

void beware_of(std::string_view something) {
    std::osyncstream(std::cerr) << something << " is dangerous";
}

int main() {
    std::thread t1([]{
        say_hello("Rachel");
    });

    std::thread t2([]{
        beware_of("darkness");
    });

    std::cerr << '\n';
 
    t2.join();
    t1.join();
}

      
      





*.



21



, , :





, :









Modules TS Networking TS.





C++20? C++17, 14 11? C++? : stdcpp.ru. !



? - .



27 21, . — . , C++ User Group .




* .



All Articles