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 to avoid situations when the compiler uncomplainingly accepts code that is not semantically correct. In this post, we answer the question “what is an explicit conversion operator in modern C++?”
What is the explicit conversion operator?
In modern C++, the explicit conversion operator is an operator used with explicit operator
and followed by conversion type id. 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 to avoid situations when the compiler uncomplainingly accepts code that is not semantically correct.
CLANG compilers of C++ Builder and BCC compilers include support for explicit conversion operators, one of the features in the C++11 standard.
We can apply the function specifier explicit in the definition of a user-defined conversion operator. Previously, explicit constructors (including copy constructors) were added to the language in order to prevent unintended conversions being implicitly called by the compiler. Now explicit conversion operators have been added to provide the same control over unintended conversion calls.
Normally we can define an operator in a class or struct as given below:
Here are two important syntax that we should know:
Syntax (since C++11):
Syntax (since C++20):
Conversion functions declared as explicit work in the same contexts as explicit constructors (that is, direct-initialization, explicit type conversion). Explicit conversion operators produce compiler diagnostics in the same contexts (copy-initialization) as explicit constructors do.
Is there a simple example of the explicit conversion operator in C++?
Here is an example of how to use an explicit conversion operator in C++.
Here is how we can use it:
Is there a full example of the explicit conversion operator in C++?
Here is a full example about how to use explicit conversion operator 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.