IBM Z and LinuxONE - Languages

Languages

Languages

Broad range of supported development languages and tools allows to transform software delivery practices.

 View Only

Defaulted functions in C++11

By FANG LU posted Tue March 24, 2020 07:39 PM

  

The defaulted functions feature is newly introduced into the C++11 standard. In this article, I will explain this feature and provide some examples on how to use it.

 

Background

C++ has six kinds of special member functions:

 

  • Default constructors
  • Destructors
  • Copy constructors
  • Copy assignment operators
  • Move constructors
  • Move assignment operators


These special member functions are used to create, destroy, initialize, convert, move, and copy class objects.

 

Suppose that you haven't defined a certain special member function (let's take default constructors as an example in this article) for a class; when a constructor is needed, the compiler will implicitly declare a default constructor for this class. For example:


Example 1

class X{

private:

int a;

};

 

X x; // fine, compiler implicitly defined default

// constructor is called


In this example, there is no user-defined default constructor. However, when an object x of class X is created, a default constructor is needed. In this case, the compiler will implicitly declare a default constructor X::X(){ }, which is parameterless and has an empty body.

 

However, if you have defined any constructors but a default constructor for a class, the compiler will not implicitly declare a default constructor when one is needed. For example, the following code fails the compilation because no default constructor exists.


Example 2

class X{

public:

X(int i){

a = i;

}

private:

int a;

};

 

X x; // error, default constructor X::X() doesn't exist

 

To make example 2 work, you have to manually define a default constructor. For example:


Example 3

class X{

public:

X(){}; //manually defined default constructor

X(int i){

a = i;

}

private:

int a;

};

 

X x; // fine

 

Problems

Although example 3 works, it has two drawbacks:

  • You have to manually define the default constructor, which requires more coding effort.
  • The user-defined default constructor is less efficient than the compiler implicitly defined default constructor.

 

To address these two drawbacks, C++11 introduced the defaulted functions feature.

 

Defaulted functions

The C++11 standard introduced defaulted function declaration as a new form of function declaration.

 

To declare a defaulted function, you can append the “=default;” specifier to the end of a function declaration. The compiler will generate the default implementations for defaulted functions. Explicitly defaulted functions can save your effort of defining those functions manually. For example:


Example 4

class X{

public:

X()= default; // defaulted constructor

X(int i){

a = i;

}

private:

int a;

};

 

X x;

 

In example 4, the explicitly defaulted constructor is more efficient than a manually programmed default constructor.

 

A function that is explicitly defaulted must be a special member function and has no default arguments. For example:


Example 5

class X {

public:

int f() = default; // error, f()isn't a special member function

X(int) = default; // error, X(int)isn't a special member function

X(int = 1) = default; // error, X(int=1)has default argument

};

 

Note: Thanks Jia Lei Ma/China/IBM for reviewing this blog.

0 comments
0 views

Permalink