Community Pick: Many members of our community have endorsed this article.

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

evilrixEngineering Manager
CERTIFIED EXPERT
An expert in cross-platform ANSI C/C++ development, specialising in meta-template programming and low latency scalable architecture design.
Published:
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
9,917 Views
evilrixEngineering Manager
CERTIFIED EXPERT
An expert in cross-platform ANSI C/C++ development, specialising in meta-template programming and low latency scalable architecture design.

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.