I originally posted this as a question only about destructors, but now I'm adding consideration of the default constructor. Here's the original question:
If I want to give my class a destructor that is virtual, but is otherwise the same as what the compiler would generate, I can use
=default
:class Widget { public: virtual ~Widget() = default; };
But it seems that I can get the same effect with less typing using an empty definition:
class Widget { public: virtual ~Widget() {} };
Is there any way in which these two definitions behave differently?
Based on the replies posted for this question, the situation for the default constructor seems similar. Given that there is almost no difference in meaning between "=default
" and "{}
" for destructors, is there similarly almost no difference in meaning between these options for default constructors? That is, assuming I want to create a type where the objects of that type will be both created and destroyed, why would I want to say
Widget() = default;
instead of
Widget() {}
?
I apologize if extending this question after its original posting is violating some SO rules. Posting an almost-identical question for default constructors struck me as the less desirable option.
= default
is more explicit imo, and is consistent with the support for it with constructors. – chrisstd::has_trivial_destructor<Widget>::value
istrue
for the first, butfalse
for the second. What the implications of that are I don't know either. :) – GManNickG