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

What Is The Explicit Specifier In Modern C++?

What Is The Explicit Specifier In Modern C++

In modern C++, explicit-qualified conversion functions work in the same context as explicit-qualified constructors and produce diagnostics in the same contexts as constructors do. C++11, C++17, and C++20 standards have improvements in this explicit specifier. This is done in order to avoid situations when the compiler uncomplainingly accepts code that is not semantically correct. In this post, we explain the explicit specifier in modern C++.

What is the explicit specifier in C++?

The explicit specifier is defined with an ‘explicit‘ statement and since C++11, the explicit specifier (keyword) specifies that a constructor or conversion function is explicit, it cannot be used for implicit conversions and copy-initialization Since C++17, the explicit specifier specifies deduction guide is explicit too. Since C++ 20, the explicit specifier can be used with a constant expression, where the function is explicit if and only if that constant expression evaluates to true.

Here is the general syntax:

or since C++20,

Is there a simple example of how to use the explicit specifier in modern C++?

Here is a simple example of how you can use an explicit specifier in a C++ class:

Here is an example with ‘operator’ statement in a Class:

The explicit specifier may only appear within the decl-specifier-seq of the declaration of a constructor or conversion function within its class definition.

How to use the explicit specifier in C++11?

Normally, a class with a single-parameter constructor can be assigned a value that matches the constructor type. This value is automatically (implicitly) converted into an object of the class type to which it is being assigned. You can prevent this kind of implicit conversion from occurring by declaring the constructor of the class with the explicit keyword. Then all objects of that class must be assigned values that are of the class type; all other assignments result in a compiler error.

Objects of the following class can be assigned values that match the constructor type or the class type:

In this example above, assignment statements in that f() function are legal. However, objects of the following class can be assigned values that match the class type only:

Here above, the explicit constructors require the values in the assignment statements of f() function to be converted to the class type to which they are being assigned.

Is there an example of how to use the explicit specifier in modern C++?

Here is the full example of using the explicit specifier in C++.

For more information on these features, see Explicit conversion operators Proposal document.

What Is The Explicit Specifier In Modern C++ 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.

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 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++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++