fork(1) download
  1. #include <memory>
  2. #include <type_traits>
  3.  
  4. struct unused; // forward declaration only
  5.  
  6. template<class Container>
  7. using const_ref_if_copy_constructible = typename std::conditional<
  8. std::is_copy_constructible<typename Container::value_type>::value,
  9. Container const&,
  10. unused>::type;
  11.  
  12. template<typename T>
  13. class Container {
  14. T t;
  15. public:
  16. typedef T value_type;
  17.  
  18. Container() = default;
  19.  
  20. Container(const_ref_if_copy_constructible<Container> other) : t(other.t) {}
  21. Container(Container&& other) : t(std::move(other.t)) {}
  22. };
  23.  
  24. static_assert(std::is_copy_constructible<Container<int>>::value, "Non-Copyable");
  25. static_assert(!std::is_copy_constructible<Container<std::unique_ptr<int>>>::value, "Copyable");
  26.  
  27. Container<std::unique_ptr<int>> makeNonCopyableContainer() {
  28. return Container<std::unique_ptr<int>>();
  29. }
  30.  
  31. int main () {
  32. Container<int> c1;
  33. Container<int> c2(c1);
  34. Container<std::unique_ptr<int>> c3;
  35. // This would generate compile error:
  36. // Container<std::unique_ptr<int>> c4(c3);
  37. Container<std::unique_ptr<int>> c5(makeNonCopyableContainer());
  38. return 0;
  39. }
Success #stdin #stdout 0s 3336KB
stdin
Standard input is empty
stdout
Standard output is empty