Type trait for moveable types?

后端 未结 6 1971
刺人心
刺人心 2020-12-17 14:33

I\'m trying to write a template that behaves one way if T has a move constructor, and another way if T does not. I tried to look for a type trait that could identify this b

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 14:46

    I took Aaron McDaid's last answer and wrapped it in the construct below. The code in his answer didn't work for me, this does with both clang 3.6 and MSVC2013.

    template 
    struct has_move_constructor_alongside_copy {
      typedef char yes[1];
      typedef char no[2];
    
      struct AmbiguousConverter {
        operator T&& ();
        operator const T& ();
      };
    
      template 
      static no& test(decltype( new C( AmbiguousConverter{} )));
    
      template 
      static yes& test(...);
    
      static const bool value = sizeof(test(0)) == sizeof(yes);
    };
    

提交回复
热议问题