1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
// move_if_noexcept example
#include <utility> // std::move_if_noexcept
#include <iostream> // std::cout
// function with lvalue and rvalue reference overloads:
template <class T> void overloaded (T& x) {std::cout << "[lvalue]\n";}
template <class T> void overloaded (T&& x) {std::cout << "[rvalue]\n";}
struct A { // copyable + moveable (noexcept)
A() noexcept {}
A (const A&) noexcept {}
A (A&&) noexcept {}
};
struct B { // copyable + moveable (no noexcept)
B() {}
B (const B&) {}
B (B&&) {}
};
struct C { // moveable only (no noexcept)
C() {}
C (C&&) {}
};
int main () {
std::cout << "A: "; overloaded (std::move_if_noexcept(A()));
std::cout << "B: "; overloaded (std::move_if_noexcept(B()));
std::cout << "C: "; overloaded (std::move_if_noexcept(C()));
return 0;
}
|