Reputation: 2354
What are the pro and cons of declaring always defaulted constructors for each non user-defined constructor?
Consider a class with a user-defined constuctor which does not need other user-defined constructors, it would be:
class Foo
{
public:
Foo() { // user-defined declaration }
Foo(const Foo&) = default;
Foo(Foo&&) noexcept = default;
~Foo() = default;
Foo& operator=(const Foo&) = default;
Foo& operator=(Foo&&) = default;
}
There are other actual advantages/disadvantages of doing that?
Upvotes: 1
Views: 1040
Reputation: 234685
There are no advantages. It merely demonstrates to the reader of your code that you don't know how the C++ compiler works. If you enforce this as a policy, you are also vulnerable to changes in future C++ standards.
An exception is using
virtual ~Foo() = default;
in a base class, where we introduce polymorphism which can help memory management.
Another class of exceptions are cases where you might want to change the access specifier for a default
able function:
protected:
Foo foo() = default;
The explicit example I give here can be useful if you only want to use a class via inheritance, but don't want to make it a polymorphic type.
Upvotes: 5
Reputation: 76519
Several disadvantages of the top of my head:
std::unique_ptr
member of a class. You can mitigate this by either including the class definition (leading to an eventual overall increase in (re)build time for non-trivial projects, especially when the not forward declared class changes often). You can mitigate this by moving the = default
to a definition in a source file.There are probably some more, rising in subtility level beyond these.
Upvotes: 5