Without spaceship operator | With spaceship operator |
---|---|
|
|
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);
}
};
lexicographical_compare_3way(InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2);
#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
*/
}
#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();
}
for (T thing = f(); auto& x : thing.items())
using greater_t = decltype([](auto x, auto y) { return x > y; });
std::map<std::string, int, greater_t> map;
constexpr greater_t comparator{}; // OK
template <class T> struct atomic<shared_ptr<T>>;
template <class T> struct atomic<weak_ptr<T>>;
Source: https://habr.com/ru/post/342354/
All Articles