0

I was wondering if there is any difference between explicitly declaring a trivial constructor and setting it to default or omitting the constructor completely.

Example:

struct someint {
    int n;
    someint() : n(0) {}
};

class example1 {
public:
    someint n;
};

class example2 {
public:
    someint n;
    example2() = default;
};

Is there any difference between example1 and example2? Or is it just a style difference?

wovano
  • 4,543
  • 5
  • 22
  • 49
Flecart
  • 15
  • 1
  • 3
  • A user provided constructor is by definition never [trivial](https://en.cppreference.com/w/cpp/language/default_constructor) even if it does nothing. – Jesper Juhl Aug 29 '22 at 13:11
  • Yes, there are some nuanced differences when it comes to more exotic parts of C++, such as the various rules that define when other default constructors and/or operators get automatically deleted, or not. – Sam Varshavchik Aug 29 '22 at 13:11
  • see here: https://stackoverflow.com/questions/21164641/difference-between-default-and-empty-constructor-with-no-arguments, or here: https://stackoverflow.com/questions/20828907/the-new-syntax-default-in-c11?lq=1 – bavaza Aug 29 '22 at 13:11

0 Answers0