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.

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.

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 graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?