Is using template parameter as base for class legal?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alan

    #1

    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<typena me 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.
  • alan

    #2
    Re: Is using template parameter as base for class legal?

    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<typena me 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<Cl osure, 3>();
    KClosure* kclos = new ClosureArray<KC losure, 5>();

    }

    Comment

    • puzzlecracker

      #3
      Re: Is using template parameter as base for class legal?

      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


      Comment

      • alan

        #4
        Re: Is using template parameter as base for class legal?

        On Jul 29, 2:33 pm, "Alf P. Steinbach" <al...@start.no wrote:
        * 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<typena me 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::reusa ble, 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.

        Comment

        Working...