Site icon Learn C++

What Is The Explicit Specifier In Modern C++?

What Is The Explicit Specifier 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 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++.

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:

[crayon-663c418ba85eb378701250/]

or since C++20,

[crayon-663c418ba85f2352363502/]

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:

[crayon-663c418ba85f4380242302/]

Here is an example with ‘operator’ statement in a Class:

[crayon-663c418ba85f5779839378/]

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:

[crayon-663c418ba85f7141091551/]

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:

[crayon-663c418ba85f9844098023/]

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++.

[crayon-663c418ba85fa867680555/]

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.

Exit mobile version