<

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

Published on
14,582 Points
8,082 Views
5 Endorsements
Last Modified:
Approved
Community Pick
evilrix
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
{
private:
	struct True_ { char x[2]; };
	struct False_ { };
 
	static True_ helper(T2 const &);
	static False_ helper(...);
 
public:
	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;
	}
	else
	{
		std::cout << "Type t1 is not convertible to t2" << std::endl;
	}
} 
 
int main(void)
{
	struct A a;
	struct B b;
	struct C c;
	
	foo(b,a);
	foo(c,a);
}

Open in new window

5
Comment
Author:evilrix
0 Comments

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Join & Write a Comment

The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month