What does "default" mean after a class' function declaration?

Paul Manta picture Paul Manta · Jun 28, 2011 · Viewed 95.8k times · Source

I've seen default used next to function declarations in a class. What does it do?

class C {
  C(const C&) = default;
  C(C&&) = default;
  C& operator=(const C&) & = default;
  C& operator=(C&&) & = default;
  virtual ~C() { }
};

Answer

Peter Alexander picture Peter Alexander · Jun 28, 2011

It's a new C++11 feature.

It means that you want to use the compiler-generated version of that function, so you don't need to specify a body.

You can also use = delete to specify that you don't want the compiler to generate that function automatically.

With the introduction of move constructors and move assignment operators, the rules for when automatic versions of constructors, destructors and assignment operators are generated has become quite complex. Using = default and = delete makes things easier as you don't need to remember the rules: you just say what you want to happen.