Is using template parameter as base for class legal?

A

alan

Hello all, I'm wondering if it's valid for a templated class to use a
template parameter as a base class.

Basically I have two abstract types, one of which is derived from the
other, and I have an implementation for those types. Note however
that their implementations are practically the same, only the two
abstract types are different only in the bit that is derived from the
other:

class Closure {
public:
virtual int& operator[](size_t i) =0;
virtual int const& operator[](size_t i) const =0;
};

class KClosure: public Closure {
public:
bool reusable;
void banreuse(){
reusable = 0;
}
KClosure() : reusable(1), Closure() {}
//KClosure doesn't add any virtual functions,
//but does add a few non-virtuals
};

Now what I tried in g++ is:

template<typename T, size_t N>
class ClosureArray : public T{
int dat[N];
public:
ClosureArray<T,N>() : T() {}
virtual int& operator[](size_t i){
return dat;
}
virtual int const& operator[](size_t i) const{
return dat;
}
}

g++ compiled the above in what seems to be what I expected it to work
(although I haven't done a very comprehensive test).

What I'd like to know is whether this is legal standard C++ and is
portable to a reasonably large number of non-g++ compilers.
 
A

alan

alan said:
class Closure {
public:
virtual int& operator[](size_t i) =0;
virtual int const& operator[](size_t i) const =0;
};

class KClosure: public Closure {
public:
bool reusable;
void banreuse(){
reusable = 0;
}
KClosure() : reusable(1), Closure() {}
//KClosure doesn't add any virtual functions,
//but does add a few non-virtuals
};


template<typename T, size_t N>
class ClosureArray : public T{
int dat[N];
public:
ClosureArray<T,N>() : T() {}
virtual int& operator[](size_t i){
return dat;
}
virtual int const& operator[](size_t i) const{
return dat;
}
}


Err, anyway, the use case I tried was like this:

int main(void){
Closure* clos = new ClosureArray<Closure, 3>();
KClosure* kclos = new ClosureArray<KClosure, 5>();

}
 
P

puzzlecracker

Consider changing the interface of Closure so that it doesn't provide direct
access to implementation details (as it is it forces an int or collection of int
member on the implementation).

I don't see a reasonable way to change that, it looks perfectly fine
to me... alf, what do you have in mind, just curious?

Thanks
 
A

alan

* alan:


Hello all, I'm wondering if it's valid for a templated class to use a
template parameter as a base class.
Basically I have two abstract types, one of which is derived from the
other, and I have an implementation for those types.  Note however
that their implementations are practically the same, only the two
abstract types are different only in the bit that is derived from the
other:
class Closure {
public:
    virtual int& operator[](size_t i) =0;
    virtual int const& operator[](size_t i) const =0;
};
class KClosure: public Closure {
public:
    bool reusable;
    void banreuse(){
       reusable = 0;
    }
    KClosure() : reusable(1), Closure() {}
//KClosure doesn't add any virtual functions,
//but does add a few non-virtuals
};
Now what I tried in g++ is:
template<typename T, size_t N>
class ClosureArray : public T{
    int dat[N];
public:
    ClosureArray<T,N>() : T() {}
    virtual int& operator[](size_t i){
         return dat;
    }
    virtual int const& operator[](size_t i) const{
         return dat;
    }
}


Consider changing the interface of Closure so that it doesn't provide direct
access to implementation details (as it is it forces an int or collection of int
member on the implementation).

Also consider using the keyword 'true' instead of numerical '1'.

Also consider ways to avoid dynamically changing "modes" of objects, such as
KClosure::reusable, and anyway, not exposing such as public data members.


This is just sample code for what I was trying to explain; the array
entries are really Generic*, and Generic is the ultimate base class
for my hierarchy of types (which includes Closure as a derived class)
and they should really refer to each other using Generic*'s.
In an interface class a reference in a result type generally says "exposes
implementation, ungood".

clos = something; seems shorter and more understandable to me than
clos.setElem(i, something);

For that matter, Closure is pretty much defined as a flat array of
pointers to other objects in my system.
That's a very broad question.

It's valid to derive from a class specified as a template parameter, yes.


Yes, it's the basis of several template programming idioms.

Thanks.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top