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:
1 2 3 |
explicit <declaration> |
or since C++20,
1 2 3 |
explicit ( expression ); |
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:
1 2 3 4 5 6 7 8 9 |
class myclass { public: explicit myclass(const myclass&) // Compatible with C++17 (Error in C++20) { }; }; |
Here is an example with ‘operator’ statement in a Class:
1 2 3 4 5 6 7 |
class myclass { public: explicit operator int (); // Compatible with C++17 (Error in C++20) }; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Tx { public: Tx(int); Tx(const char*, int = 0); }; void f(Tx arg) { Tx a = 1; Tx B = "LearnCPlusPlus.org"; a = 2; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Tx { public: explicit Tx(int); explicit Tx(const char*, int = 0); }; void f(Tx arg) { Tx a = Tx(1); Tx b = Tx("LearnCPlusPlus.org",0); a = Tx(2); } |
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++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> class myclass { public: explicit myclass (int) { } explicit myclass (int, int) { } explicit operator bool() const { return true; } }; int main() { // myclass b1 = 1; // Error myclass b2(2); // Correct myclass b3 {4, 5}; // Correct // myclass b4 = {4, 5}; // Error myclass b5 = (b3); // Correct //if (b2) return 0; // Correct // bool nb1 = b2; // Error bool nb2 = static_cast<bool>(b2); // Correct return 0; } |
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.