fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <type_traits>
  4.  
  5. struct move_not_copy { move_not_copy(move_not_copy &&); };
  6.  
  7. template<typename T>
  8. struct has_move_constructor {
  9. struct helper : public move_not_copy, public T {
  10. };
  11. constexpr static bool value = std::is_constructible<helper, typename std::add_rvalue_reference<helper>::type> ::value;
  12. constexpr operator bool () const { return value; }
  13. };
  14.  
  15. struct T0 {
  16. void operator=(T0&&) {}
  17. };
  18. struct Copy {
  19. Copy( const Copy& );
  20. };
  21. struct MoveOnly {
  22. MoveOnly( MoveOnly&& );
  23. };
  24. struct Both {
  25. Both( const Both& );
  26. Both( Both&& );
  27. };
  28. struct CopyWithDeletedMove {
  29. CopyWithDeletedMove( const CopyWithDeletedMove& );
  30. CopyWithDeletedMove( CopyWithDeletedMove&& ) = delete;
  31. };
  32.  
  33. template<typename T>
  34. void provide_a_report_impl(const char *type_name) {
  35. std::cout << std::setw(20) << type_name
  36. << " is_copy_constructible " << std::is_constructible<T, typename std::add_lvalue_reference<T>::type>()
  37. << " is_move_constructible " << std::is_constructible<T, typename std::add_rvalue_reference<T>::type>()
  38. << " has_move_constructor " << has_move_constructor<T>::value
  39. << std::endl;
  40. };
  41. #define REPORT_ON_ISHAS_MOVE_CONSTRUCTOR(T) provide_a_report_impl<T>(#T)
  42.  
  43. int main()
  44. {
  45. REPORT_ON_ISHAS_MOVE_CONSTRUCTOR(Copy);
  46. REPORT_ON_ISHAS_MOVE_CONSTRUCTOR(MoveOnly);
  47. REPORT_ON_ISHAS_MOVE_CONSTRUCTOR(Both);
  48. REPORT_ON_ISHAS_MOVE_CONSTRUCTOR(CopyWithDeletedMove);
  49. }
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
                Copy  is_copy_constructible 1  is_move_constructible 1  has_move_constructor 1
            MoveOnly  is_copy_constructible 0  is_move_constructible 1  has_move_constructor 1
                Both  is_copy_constructible 1  is_move_constructible 1  has_move_constructor 1
 CopyWithDeletedMove  is_copy_constructible 1  is_move_constructible 0  has_move_constructor 0