It\'s possible that someone has already asked about this but googling for \"default\", \"defaulted\", \"explicit\" and so on doesn\'t give good results. But anyway.
Given below excerpt from C++ Programming Language Stroustrup 4th Edition book makes it clear that they are equivalent.
Explicit Defaults
Since the generation of otherwise default operations can be suppressed, there has to be a way of getting back a default. Also, some people prefer to see a complete list of operations in the program text even if that complete list is not needed. For example, we can write:
class gslice {
valarray<size_t> size;
valarray<size_t> stride;
valarray<size_t> d1;
public:
gslice() = default; //<< Explicit default constructor
~gslice() = default;
gslice(const gslice&) = default;
gslice(gslice&&) = default;
gslice& operator=(const gslice&) = default;
gslice& operator=(gslice&&) = default;
// ...
};
This fragment of the implementation of std::gslice (§40.5.6) is equivalent to:
class gslice {
valarray<size_t> siz e;
valarray<size_t> stride;
valarray<size_t> d1;
public:
// ...
};
The purpose of = default
is to make the implicit definition explicit. Any differences between an implicitly defined version and the explicitly defaulted version are limited to some additional possibilities appearing due to the presence of an explicit declaration.
The implicitly declared/defined constructor is always public
, whereas the access control of the explicitly defined defaulted constructor is under your own control.
Defining a defaulted default constructor enables you annotating it with attributes. For example:
$ cat a.cpp
class A
{
public:
[[deprecated]] A() = default;
};
int main()
{
A a;
}
$ g++ -std=c++14 a.cpp
a.cpp: In function ‘int main()’:
a.cpp:9:7: warning: ‘constexpr A::A()’ is deprecated [-Wdeprecated-declarations]
A a;
^
a.cpp:4:20: note: declared here
[[deprecated]] A() = default;
^