Suppose I\'m writing a class template C that holds a T value, so C can be copyable only if T is copyabl
If you want to conditionally disable your copy constructor, you definitely want it to participate in overload resolution - because you want it to be a loud compile error if you try to copy it.
And to do that, all you need is static_assert:
template
class C {
public:
C(const C& rhs) {
static_assert(some_requirement_on::value,
"copying not supported for T");
}
};
This will allow copy construction only if some_requirement_on is true, and if it's false, you can still use the rest of the class... just not copy construction. And if you do, you'll get a compile error pointing to this line.
Here's a simple example:
template
struct Foo
{
Foo() { }
Foo(const Foo& ) {
static_assert(std::is_integral::value, "");
}
void print() {
std::cout << "Hi" << std::endl;
}
};
int main() {
Foo f;
Foo g(f); // OK, satisfies our condition
g.print(); // prints Hi
Foo h;
//Foo j(h); // this line will not compile
h.print(); // prints Hi
}