<

Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

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

Published on
13,957 Points
7,457 Views
5 Endorsements
Last Modified:
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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Join & Write a Comment

The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month