📜 ⬆️ ⬇️

Another small step to C ++ 20. Meeting in Albuquerque

From the central part of Canada - to the southwest of the USA! Albuquerque is located in the state of New Mexico:



At the meeting of the international standardization committee C ++, which was held in this city, they took one very big innovation in C ++ 20 and several small ones.
')

operator <=>


The spaceship operator was added to the draft C ++ 20, with which it is possible to determine the relationship of elements in one operation. In simple cases, this means that you can define the spaceship operator and your class will learn to be compared by any means: <,>, <=,> =, == and! =. Example:
Without spaceship operatorWith spaceship operator
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 .


* .

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


All Articles