473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is using template parameter as base for class legal?

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[i];
}
virtual int const& operator[](size_t i) const{
return dat[i];
}
}

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.
Jul 29 '08 #1
3 1576
alan wrote:
>
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[i];
}
virtual int const& operator[](size_t i) const{
return dat[i];
}
}
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>();

}

Jul 29 '08 #2
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
Jul 29 '08 #3
On Jul 29, 2:33*pm, "Alf P. Steinbach" <al...@start.nowrote:
* 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[i];
* * }
* * virtual int const& operator[](size_t i) const{
* * * * *return dat[i];
* * }
}

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 collectionof 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, suchas
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[i] = 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.
>
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++

That's a very broad question.

It's valid to derive from a class specified as a template parameter, yes.
and is
portable to a reasonably large number of non-g++ compilers.

Yes, it's the basis of several template programming idioms.
Thanks.
Jul 29 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave | last post by:
Hello all, I hope the context to my problem shown below is adequate but not overwhelming. I tried to keep it minimal. At the line indicated (in comment form) in the source below, the...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
0
by: Arne Claus | last post by:
I've got a tricky problem here. I try to create an object-hierarchie, based on a template base class like template <class T> class Node { addChild(Node<T> *) =0; // the Node-Container wil be...
5
by: Hartmut Sbosny | last post by:
Hello NG, I have a ordinary class `Base' with two functions `void foo()' and `void any()': class Base { public: Base() {} void foo() {} void any() {}
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
11
by: Niels Dekker - no reply address | last post by:
The following attempt to pass my template "Base" as a template template argument was rejected by Microsoft VC++ 8.0 (2005), while it still works on VC++ 7.1 (2003). Is it correct C++? And is...
3
by: valoh | last post by:
Hi, is this legal c++ code? template <typename BaseTstruct A { BaseT& this_() { return *static_cast<BaseT*>(this); } template <typename Tvoid Foo() { this_().Bar<T>(); } template...
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.