Without any user c'tors
You should only implement your own default constructor it it does anything else then the one the compiler would generate.
If you want to give the reader a hint, you can replace the implementation with = default
in C++11.
struct MyObject {
// only members that the compiler initializes fine
std::vector<int> data_;
MyObject() = default;
};
or, if you don't wan to be that verbose (or before C++11):
struct MyObject {
// only members that the compiler initializes fine
std::vector<int> data_;
};
With other user c'tors
The compiler will not generate a default c'tor if you provide any other c'tor. in this case you should only provide a default c'tor if it makes sense, semantically -- not because it's "nice" to have one :-)
Pros and Cons
Explicitly providing an unnecessary default c'tor
(-)
it is bad to have more code then necessary
(o)
except when the gained clarity outweighs the longer source code (= default
)
(-)
a compiler-generated default c'tor will be close to optimal
(-)
if you start providing an unnecessary member ("unnecessary", because it would be generated), you later end up defining all of the auto generated ones, i.e. default-c'tor, destructor, copy, move, assign and move-assign. You really don't want to end up there.
(-)
will you know if you should mark the default c'tor with noexcept
? The compiler often does. Don't waist your brain powers where the compiler can help you.
I really can not see any clear (+)
, but that's just me.