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