Written by on May 24, 2020 in C++, Programming with 0 Comments

The type trait is_convertible<From, To> checks if an object of type From can be “implicitly” converted to an object of type To. The expression is_convertible<From, To>::value returns true if implicit conversion is possible, else it returns false. For more details, please check out the reference.

Let us look at three primary cases.

Case-1: Pre-defined Types

Let us start with the simplest case involving pre-defined types. Look at the example code below:

Pre-defined Types

Pre-defined Types

The macro “CHECK” expands to code that applies the type trait and prints a meaningful message. This reduces clutter and makes the code more readable. For convenience, the expected outcome is also shown commented on each line.

The printed output is as follows:

The Output

The Output

I guess there are no surprises here.

Case-2: Classes Involving Hierarchy

This is more interesting and challenging compared to the previous case. We have a bunch of classes that form a hierarchy, including multiple inheritance. Here is the example code.

Complex Class Hierarchy

Complex Class Hierarchy

Let us look at the 6 different conversion attempts. Line 26 checks if class A can be implicitly converted to class F. Since A and F are unrelated classes, A cannot be converted to F (nor vice versa). In line 27, we check if B can be converted to A. This is fine because A is the unambiguous public base class of A. Line 28 fails because there is no implicit conversion from a base class to its derived class.

What about line 29? A is the base class of C, but since the derivation is private, the conversion fails when attempted from “main” (doesn’t have any special privilege). Line 30 tries to convert the derived type E to its public base class A, but this too fails because the conversion is ambiguous (A is included twice along two different paths). In contrast, line 31 is fine because there is only a single shared instance of A in H (due to virtual derivation via all paths). Here is the actual output:

The Output

The Output

Case-3: Explicit Support for Conversion

In this example, we have three classes. See the code below.

Support for Implicit Conversion

Support for Implicit Conversion

Class B defines a constructor that takes an A object as argument. Since this constructor is not declared “explicit”, it acts as an implicit conversion mechanism from A to B. That is why line 24 succeeds.

Line 25 fails because C (unlike B) does not have an implicit constructor taking A argument. 

If you look at class C, you will notice a conversion operator that converts a C object to an A object, and hence supports implicit conversion from C to A. This is the reason why line 26 works. See the corresponding output:

The Output

The Output

The above 3 cases pretty much cover the different scenarios where the type trait is_convertible<From, To> operates. Hope you now have a better understanding of its usage.

You can download the examples from here.

Have a nice wekeend!

Tags: , ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *