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

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…

// Some types
struct A{};
struct B:A{};
struct C{};

template 
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 
void foo(T1 const & t1, T2 const & t2)
{
   if(is_convertible::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);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s