#include <memory>
#include <type_traits>
struct unused; // forward declaration only
template<class Container>
using const_ref_if_copy_constructible = typename std::conditional<
std::is_copy_constructible<typename Container::value_type>::value,
Container const&,
unused>::type;
template<typename T>
class Container {
T t;
public:
typedef T value_type;
Container() = default;
Container(const_ref_if_copy_constructible<Container> other) : t(other.t) {}
Container(Container&& other) : t(std::move(other.t)) {}
};
static_assert(std::is_copy_constructible<Container<int>>::value, "Non-Copyable");
static_assert(!std::is_copy_constructible<Container<std::unique_ptr<int>>>::value, "Copyable");
Container<std::unique_ptr<int>> makeNonCopyableContainer() {
return Container<std::unique_ptr<int>>();
}
int main () {
Container<int> c1;
Container<int> c2(c1);
Container<std::unique_ptr<int>> c3;
// This would generate compile error:
// Container<std::unique_ptr<int>> c4(c3);
Container<std::unique_ptr<int>> c5(makeNonCopyableContainer());
return 0;
}
I2luY2x1ZGUgPG1lbW9yeT4KI2luY2x1ZGUgPHR5cGVfdHJhaXRzPgoKc3RydWN0IHVudXNlZDsgIC8vIGZvcndhcmQgZGVjbGFyYXRpb24gb25seQoKdGVtcGxhdGU8Y2xhc3MgQ29udGFpbmVyPgp1c2luZyBjb25zdF9yZWZfaWZfY29weV9jb25zdHJ1Y3RpYmxlID0gdHlwZW5hbWUgc3RkOjpjb25kaXRpb25hbDwKICAgICAgICBzdGQ6OmlzX2NvcHlfY29uc3RydWN0aWJsZTx0eXBlbmFtZSBDb250YWluZXI6OnZhbHVlX3R5cGU+Ojp2YWx1ZSwKICAgICAgICBDb250YWluZXIgY29uc3QmLAogICAgICAgIHVudXNlZD46OnR5cGU7CiAgICAgICAgCnRlbXBsYXRlPHR5cGVuYW1lIFQ+CmNsYXNzIENvbnRhaW5lciB7CiAgICBUIHQ7CnB1YmxpYzoKCXR5cGVkZWYgVCB2YWx1ZV90eXBlOwoJCiAgICBDb250YWluZXIoKSA9IGRlZmF1bHQ7CgoJQ29udGFpbmVyKGNvbnN0X3JlZl9pZl9jb3B5X2NvbnN0cnVjdGlibGU8Q29udGFpb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