B is_convertible to A<T> for some T

A

Agoston Bejo

Hello,
I've got a template related problem. 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>

using namespace std;

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>
//struct isA< {{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;
}



---------------------------------------------------
 
M

msalters

Agoston said:
Hello,
I've got a template related problem. 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>".

You can't. You need every contructor of every A<T> instantiation,
which is an infinite set. How else would you know that
A<void volatile* const*>::A is specialized to take a B?

Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top