Boost logo

Boost Users :

From: Agoston Bejo (gusz1_at_[hidden])
Date: 2005-01-06 16:20:28


Hello,
how do I express through template metaprogramming or in a similar way that a
class is convertible to a template
class for some T1?
I.e., the condition: "There exists some T1 such that B is convertible to
A<T1>".
An example program: (Platform: VC++7.1)

---------------------------------------------------

#include <iostream>
#include <boost/utility/enable_if.hpp>

using namespace std;
using namespace boost;

template<typename T>
struct A {};

struct B : public A<int> {};

template<typename T>
struct isA
{
 static const bool value = false;
};

template<typename T>
struct isA< A<T> >
{
 static const bool value = true;
};

// This would give the desired result, but I don't know how it should
// legally be expressed it in C++ (if anyhow):

//template<typename T, typename TEnabler = void>
//struct A { static const bool value = false; };
//
//template<typename T, >
//struct isA< T,
// enable_if<{{T where T is convertible to A<T1> for some T1}}> >
//{
// static const bool value = true;
//};

int _tmain(int argc, _TCHAR* argv[])
{
 cout << isA< A<int> >::value << endl; // 1
 cout << isA< A<double> >::value << endl;// 1
 cout << isA< B >::value << endl; // 0 - not what I want
 cout << isA<int>::value << endl; // 0
 return 0;
}

---------------------------------------------------


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net