Under certain conditions, I'd like to SFINAE away the copy constructor and copy assignment operator of a class template. But if I do so, a default copy constructor and a default assignment operator are generated. The SFINAE is done based on tags I pass as class template parameters. The problem is, that SFINAE only works on templates and a copy constructor/assignment operator can't be a template. Does there exist a workaround?
Copy constructor (and assignment) should be defined when ever the implicitly generated one violates any class invariant. It should be defined as deleted when it cannot be written in a way that wouldn't have undesirable or surprising behaviour.
Copy Constructor is called when an object is either passed by value, returned by value, or explicitly copied. If there is no copy constructor, c++ creates a default copy constructor which makes a shallow copy. If the object has no pointers to dynamically allocated memory then shallow copy will do.
The copy constructor and copy-assignment operator are public but deleted. It is a compile-time error to define or call a deleted function. The intent is clear to anyone who understands =default and =delete . You don't have to understand the rules for automatic generation of special member functions.
In C++, a Copy Constructor may be called for the following cases: 1) When an object of the class is returned by value. 2) When an object of the class is passed (to a function) by value as an argument. 3) When an object is constructed based on another object of the same class.
This solution uses a base class that is conditionally not copyable (by explicitely marking the copy constructor and copy assignment operator as deleted).
template <bool>
struct NoCopy;
template <>
struct NoCopy<true>
{
// C++11 and later: marking as deleted. Pre-C++11, make the copy stuff private.
NoCopy(const NoCopy&) = delete;
NoCopy& operator=(const NoCopy&) = delete;
protected:
~NoCopy() = default; // prevent delete from pointer-to-parent
};
template <>
struct NoCopy<false>
{
// Copies allowed in this case
protected:
~NoCopy() = default; // prevent delete from pointer-to-parent
};
Example usage:
template <typename Number>
struct Foo : NoCopy<std::is_integral<Number>::value>
{
Foo() : NoCopy<std::is_integral<Number>::value>{}
{
}
};
int main()
{
Foo<double> a;
auto b = a; // compiles fine
Foo<int> f;
auto g = f; // fails!
}
Note: the destructor of NoCopy
is declared protected
to avoid virtual inheritance (Thanks for the hint, @Yakk).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With