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++.
Table of Contents
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.
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.