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:
1 2 3 |
operator conversion_type_id |
Here are two important syntax that we should know:
Syntax (since C++11):
1 2 3 |
explicit operator conversion_type_id |
Syntax (since C++20):
1 2 3 |
explicit (expression) operator conversion_type_id |
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++.
1 2 3 4 5 6 7 8 9 10 11 |
class Tx { }; class Ty { public: explicit operator Ty() const ; }; |
Here is how we can use it:
1 2 3 4 5 6 7 8 9 10 11 12 |
void m() { Ty o1; // with cast Ty o2 = (Ty)o1; // ok // without cast Tx o3 = o1; // error: no viable conversion from 'Ty' to 'Tx' } |
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++.
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 |
#include <iostream> class Tx { }; class Ty { public: explicit operator Ty() const ; }; int main() { Ty o1; // with cast Ty o2 = (Ty)o1; // ok // without cast Tx o3 = o1; // error: no viable conversion from 'Ty' to 'Tx' } |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition