C++C++11C++14C++17C++20Learn C++

What Is Defaulted Function or Method In Modern C++ Now?

What Is Defaulted Function or Method In Modern C++ Now

Modern C++ is really amazing with a lot of great features of programming. One of the features of Modern C++ is a Defaulted Function or in another term Defaulted Method of classes that is a function that contains =default; in its prototype. Defaulted functions are a feature of C++11 and above. In this post, we explain what is a defaulted function or method in modern C++.

What is defaulted function or method in Modern C++?

A defaulted function (actually defaulted method, they are functions in classes) is a function that contains =default; in its prototype. This construction indicates that the function’s default definition should be used. Defaulted functions are a C++11 specific feature.

If you have a method (including construction method) and you want to make it defaulted method, just add ‘=default;’ specifier to the end of this method declaration to declare that method as an explicitly defaulted method (or explicitly defaulted function).

Is there a simple example of a defaulted function or method in modern C++?

Here is an example to demonstrates defaulted function: 

This will allow the compiler generate the default implementations for explicitly defaulted methods which are more efficient than manually programmed method implementations. 

Here is another example class.

What is defaulted function or method in Modern C++?

For example, if we have a parameterized constructor, we can use the ‘=default;’ specifier in order to create a default method. Because the compiler will not create a default constructor without this.

For more information on defaulted functions, see Defaulted functions Proposal document.

Here are more details about the related topic of a trivial default constructor in C++.

What Is Defaulted Function or Method In Modern C++ Now? C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap