#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);
}
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.
Comments (0)