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

What Is An Explicit Conversion Operator In Modern C++?

What Is An Explicit Conversion Operator 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 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.

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

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++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?