Updated:

1 minute read

개요

  • swap 가능 여부 확인


예제

  • 코드
     #include <algorithm>
     #include <cstring>
     #include <iostream>
     #include <string>
     #include <string_view>
     #include <type_traits>
        
     using namespace std;
        
     class A {};
        
     class B {
     	public:
     		B() {}
     		B(const B &b) {}
     };
        
     class C {
     	public:
     		C() {}
     		C(const C &c) noexcept {}
     };
        
     class D {
     	public:
     		D() {}
     		D(const D &d) = delete;
     };
        
     void swap(A &a, B &b) {}
        
     template <typename T> constexpr string_view type_name() {
     	const string s = __PRETTY_FUNCTION__;
     	const int prefixSize = s.find("[with T = ") + strlen("[with T = ");
        
     	return string_view(s.data() + prefixSize, s.find(';') - prefixSize);
     }
        
     int main() {
     	auto func1 = [](auto t) {
     		cout << "is_swappable<" << type_name<decltype(t)>() << ">::value - "
     			 << is_swappable<decltype(t)>::value << endl;
     		cout << "is_nothrow_swappable<" << type_name<decltype(t)>()
     			 << ">::value - " << is_nothrow_swappable<decltype(t)>::value
     			 << endl;
     	};
        
     	auto func2 = [](auto t1, auto t2) {
     		cout << "is_swappable_with<" << type_name<decltype(t1)>() << ", "
     			 << type_name<decltype(t2)>() << ">::value - "
     			 << is_swappable_with<decltype(t1), decltype(t2)>::value << endl;
        
     		cout << "is_nothrow_swappable_with<" << type_name<decltype(t1)>()
     			 << ", " << type_name<decltype(t2)>() << ">::value - "
     			 << is_nothrow_swappable_with<decltype(t1), decltype(t2)>::value
     			 << endl;
     	};
        
     	func1(int());
     	cout << "------" << endl;
     	func1(A());
     	cout << "------" << endl;
     	func1(B());
     	cout << "------" << endl;
     	func1(C());
     	cout << "------" << endl;
     	func1(D());
     	cout << "------" << endl;
     	func2(int(), int());
     	cout << "------" << endl;
     	func2(int(), A());
     	cout << "------" << endl;
     	func2(A(), B());
     	cout << "------" << endl;
     	func2(C(), D());
        
     	return 0;
     }
    
  • 실행 결과
     is_swappable<int>::value - 1
     is_nothrow_swappable<int>::value - 1
     ------
     is_swappable<A>::value - 1
     is_nothrow_swappable<A>::value - 1
     ------
     is_swappable<B>::value - 1
     is_nothrow_swappable<B>::value - 0
     ------
     is_swappable<C>::value - 1
     is_nothrow_swappable<C>::value - 1
     ------
     is_swappable<D>::value - 0
     is_nothrow_swappable<D>::value - 0
     ------
     is_swappable_with<int, int>::value - 0
     is_nothrow_swappable_with<int, int>::value - 0
     ------
     is_swappable_with<int, A>::value - 0
     is_nothrow_swappable_with<int, A>::value - 0
     ------
     is_swappable_with<A, B>::value - 0
     is_nothrow_swappable_with<A, B>::value - 0
     ------
     is_swappable_with<C, D>::value - 0
     is_nothrow_swappable_with<C, D>::value - 0