Determining if a C++ type is convertible to another at compile time

Published on
14,714 Points
5 Endorsements
Last Modified:
Community Pick
An expert in cross-platform ANSI C/C++ development, specialising in meta-template programming and low latency scalable architecture design.
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generate a log or trace file for debugging purposes. The relationship of the types may have significance.

This relationship can be determined with a little bit of meta-template trickery along with a special function prototype that takes an ellipsis ... as its formal parameter list.

A little background for those who don't know, the ... ellipsis in C and C++ parlance is a special function parameter that means the function will accept any type and any number of parameters and is often used in C along with var_args to create functions that take variable arguments, functions such as printf() or scanf(), for example.

An important factor to this technique is that, in C++, a function can be overloaded with an ellipsis version and it will be called if and only if no other function of the same name can be found to match the calling parameter list. We take advantage of this by declaring (but not defining) two overloads of with same function name; one that take a reference to the type we're looking to see if we can convert to and the other takes the ... ellipsis.

The trick is to have the ellipsis version return a type that is a different size to that of the more specific function. At compile time the compiler will use static polymorphism to decide which function to call and we can then use the sizeof operator on the function call to get the size of the function's return type that the compiler decided matched the calling parameter. If the types are convertible then the return type size will be that of the specific function taking a reference to the convertible type, otherwise the size will be that of the generic function that has the ellipsis parameter.

Note, neither of these functions actually needs to be defined -- only declared -- because neither of them is actually ever called; there is no runtime cost to this technique

This can all be wrapped up in a simple little template meta-function to simplify usage. Below is a contrived example...

#include <iostream>
// Some types
struct A{};
struct B:A{};
struct C{};
template <typename T1, typename T2>
struct is_convertible
	struct True_ { char x[2]; };
	struct False_ { };
	static True_ helper(T2 const &);
	static False_ helper(...);
	static bool const YES = (
		sizeof(True_) == sizeof(is_convertible::helper(T1()))
template <typename T1, typename T2>
void foo(T1 const & t1, T2 const & t2)
	if(is_convertible<T1, T2>::YES)
		std::cout << "Type t1 is convertible to t2" << std::endl;
		std::cout << "Type t1 is not convertible to t2" << std::endl;
int main(void)
	struct A a;
	struct B b;
	struct C c;

Open in new window


Featured Post

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Join & Write a Comment

The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month