3

In a default constructor's definition, is = default; exactly the same as { }? Are there any situations where they might have different meanings?

Sample pseudocode:

template<typename... Args>
struct S // maybe with base classes
{
    S() = default;    
    // S() {}    - same or different?

    // other stuff...
};
M.M
  • 138,810
  • 21
  • 208
  • 365
  • 2
    This actually depends on where the `= default;` occurs. – T.C. Apr 07 '15 at 03:20
  • In addition to what has already been said, if you have any non-static data members then those members are default-initialized instead of value-initialized where value-initialization can occur. – David G Apr 07 '15 at 03:23
  • @user2864740 good job. I tried SO search with pretty much exactly that and nothing came up :| – M.M Apr 07 '15 at 03:26
  • Note that all the discussion in the dup and the answer below assume that the defaulting occurs on the first declaration. AFAIK there is no difference between `struct S { S(); }; S::S() = default;` and `struct S { S(); }; S::S(){ }`. – T.C. Apr 07 '15 at 03:52

1 Answers1

9

They are different. By S() {}, S is considered to have a user-provided constructor, while S() = default; not. This makes a difference, for example, as to whether S qualifies as an aggregate type. See here.

Lingxi
  • 14,579
  • 2
  • 37
  • 93
  • 1
    Another point is that trivial constructibility requirements cannot be met with a user-provided default constructor. – chris Apr 07 '15 at 03:19