Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point in defaulting functions in C++11?

Tags:

C++11 adds the ability for telling the compiler to create a default implementation of any of the special member functions. While I can see the value of deleting a function, where's the value of explicitly defaulting a function? Just leave it blank and the compiler will do it anyway.

The only point I can see is that a default constructor is only created when no other constructor exists:

class eg {
public:
    eg(int i);
    eg() = default; 
};

But is that really better than how you do it now?

class eg {
public:
    eg(int i);
    eg() {}
};

Or am I missing a use-case?

like image 850
Motti Avatar asked May 05 '09 08:05

Motti


People also ask

What is the purpose of default keyword in C++11?

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.

What is the point of default constructor C++?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

What does default mean in constructor?

[edit] A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is DefaultConstructible.

What are the default functions provided in C++?

In C++, the compiler automatically generates the default constructor, copy constructor, copy-assignment operator, and destructor for a type if it does not declare its own. These functions are known as the special member functions, and they are what make simple user-defined types in C++ behave like structures do in C.


2 Answers

A defaulted constructor will have a declaration, and that declaration will be subject to the normal access rules. E.g. you can make the default copy constructor protected. Without these new declarations, the default generated members are public.

like image 181
MSalters Avatar answered Oct 07 '22 19:10

MSalters


Those examples from Stroustrup's website might help you understand the point:

defaulted and deleted functions -- control of defaults

The common idiom of "prohibiting copying" can now be expressed directly:

class X {
  // ...

  X& operator=(const X&) = delete;    // Disallow copying
  X(const X&) = delete;
};

Conversely, we can also say explicitly that we want to default copy behavior:

class Y {
  // ...
  Y& operator=(const Y&) = default;   // default copy semantics
  Y(const Y&) = default;

};

Being explicit about the default is obviously redundant, but comments to that effect and (worse) a user explicitly defining copy operations meant to give the default behavior are not uncommon. Leaving it to the compiler to implement the default behavior is simpler, less error-prone, and often leads to better object code. The "default" mechanism can be used for any function that has a default. The "delete" mechanism can be used for any function. For example, we can eliminate an undesired conversion like this:

struct Z {
  // ...

  Z(long long);     // can initialize with an long long
  Z(long) = delete; // but not anything less
};
like image 38
Klaim Avatar answered Oct 07 '22 19:10

Klaim