CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Inheriting one variadic class from another

  1. #1
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Inheriting one variadic class from another

    Let's say I have a variadic base class with a pure-virtual function per type:

    Code:
    template <typename ... Types>
    class Base;
    
    template <typename T, typename ... Types>
    class Base<T,Types...>: public Base<Types...>
    {
    public:
        using Base<Types...>::doSomething;
        // Stuff relating to T specifically
        virtual void doSomething(const T& arg) = 0;
    };
    
    template <typename T>
    class Base<T>
    {
    public:
        void doSomething(const T &arg);
    };
    Now, I'd like to inherit from it using another variadic class, which provides implementations of doSomething(), but I run into trouble --- where do I indicate it derives from Base?
    Code:
    template <typename ... Types>
    class Derived;
    
    template <typename T, typename ... Types>
    class Derived<T,Types...>: public Derived<Types...>
    // I can't indicate it derives from Base<T,Types...> here, I don't want the diamond problem and I otherwise
    // don't need virtual inheritance.
    {
    public:
        using Derived<Types...>::doSomething;
        // Stuff relating to T specifically
        void doSomething(const T& arg)
        {
            std::cout << arg << std::endl;
        }
    };
    
    template <typename T>
    class Base<T> // I can't indicate it derives from Base here, I don't have all the types available anymore!
    {
    public:
        void doSomething(const T& arg)
        {
            std::cout << arg << std::endl;
        }
    };
    I see two possible approaches. First, virtual inheritance *might* get me what I want, but I don't know how bad a performance hit that might be.

    Second, I could do some magic where the full set of types is captured in a tuple at the lowest level and continually passed up, then re-expanded in the base case to indicate Base inheritance. However, I'm not sure if that can be done in an unambigious manner; I can't have two variadic packs at once (Types... and the tuple contents), and I'm not sure if there's a way to use enable_if to check if an arbitrary template type is any kind of tuple.

    Any suggestions?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Inheriting one variadic class from another

    I may have generalized my example code too much....in my case, the parameter pack is a list of values, not types. This is good in that it allows me to pass an additional type as the first argument, capturing the complete set of values without ambiguity; however, it's bad in that if I do that, I *can't* pretend the type isn't a part of the class signature from the user perspective.

    What I'd like to do is do the additional type trick on a helper class. However, I need my main class to inherit those pure virtual functions, and I don't believe it's possible for a class with two bases to let one base provide implementations for another base's pure virtuals. (Correct me if I'm wrong.)

    Virtual inheritance may be the only way to go here.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Inheriting one variadic class from another

    I believe I've worked it out. I did need a helper class to do the majority of the logic and keep the extra type hidden from the main interface.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)