Templates and Interfaces

P

Paulo Matos

Hi all,

I'm curious if I can say in C++ that a certain template can only be
instantiated (template-wise) by a class implementing a certain
interface (inheriting from a given abstract class).

Regards,

Paulo Matos
 
V

Victor Bazarov

Paulo said:
I'm curious if I can say in C++ that a certain template can only be
instantiated (template-wise) by a class implementing a certain
interface (inheriting from a given abstract class).

There are several ways of doing that. Google for "is_base_of" or
something like that. There are compile-time assertions for that
kind of test.

V
 
K

Kai-Uwe Bux

Paulo said:
I'm curious if I can say in C++ that a certain template can only be
instantiated (template-wise) by a class implementing a certain
interface (inheriting from a given abstract class).

Sure you can say that: nobody is going to stop you. However, be informed,
that if you did, you would be asserting something false: template
instantiation in C++ is a concept totally independent from inheritance (be
it from abstract base classes or from concrete classes).

Maybe there is an underlying problem or confusion. What triggered your
question?


Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Kai-Uwe Bux said:
Sure you can say that: nobody is going to stop you. However, be informed,
that if you did, you would be asserting something false: template
instantiation in C++ is a concept totally independent from inheritance (be
it from abstract base classes or from concrete classes).

Maybe there is an underlying problem or confusion. What triggered your
question?

Oh well, I guess I was confused: reading Victors reply, I realized that I
profoundly misunderstood your question. Sorry about that.

To make up for it, here is what I use to test for public inheritance:


template < typename B, typename D >
class is_subclass {

typedef char (&no) [1];
typedef char (&yes) [2];

struct dummy {
operator D& ( void );
};

static
yes check ( B& );

static
no check ( ... );

public:

static bool const value = sizeof( check( dummy() ) ) == sizeof( yes );

}; // is_subclass


template < typename B, typename D >
class is_a {

typedef char (&no) [1];
typedef char (&yes) [2];

static
D& dummy ( void );

static
yes check ( B& );

static
no check ( ... );

public:

static bool const value = sizeof( check( dummy() ) ) == sizeof( yes );

}; // is_a


struct ABC {};

struct A : public ABC {};

struct X {
static A dummy;
operator A& () {
return dummy;
}
};
A X::dummy;

struct Y {
static ABC dummy;
operator ABC& ( void ) {
return dummy;
}
};

ABC Y::dummy;


#include <iostream>

int main ( void ) {
std::cout
<< is_subclass<ABC,ABC>::value << " "
<< is_subclass<ABC,A>::value << " "
<< is_subclass<ABC,X>::value << " "
<< is_subclass<ABC,Y>::value << "\n"
<< is_a<ABC,ABC>::value << " "
<< is_a<ABC,A>::value << " "
<< is_a<ABC,X>::value << " "
<< is_a<ABC,Y>::value << "\n";
}


Things to note:
a) I consider every class is subclass of itself.
b) is_a<> is more permissive than is_subclass. (The key is that the struct
dummy approach will prevent further user-defined conversions since we
already used one.)
c) These templates are not supposed to handle private inheritance.


Best

Kai-Uwe Bux
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top