14

I refer to the following as “multiple re-inheritance”:

  • inheriting a class once directly and one or more times indirectly by inheriting one or more of its descendants
  • inheriting a class indirectly two or more times by inheriting two or more of its descendants

I want to know if it exists and how to unambiguously access embedded subobjects.

1.) [Professional C++, 2nd ed.] states a compilable program can't have a class that directly inherits both its immediate parent and said parent's parent class. Is it true?

Given a GrandParent and Parent, which extends GrandParent, VC12 and g++ allows a GrandChild to directly inherit from both Parent and GrandParent. In VC12 and g++, it’s possible to define these classes as follows:

GrandParent declares an int num data member. Parent declares its own num in addition to inheriting GrandParent's num. GrandChild declares its own num in addition to inheriting Parent's and GrandParent's nums.

VC12 seems to allow unambiguous member access across the board, but g++ only allows it for some cases.

#include <iostream>
using std::cout;
using std::endl;

struct GrandParent { int num; };
struct Parent : GrandParent { int num; };
struct GrandChild : GrandParent, Parent { int num; };

int main()
{
    GrandChild gc;
    gc.num = 2;
    gc.Parent::num = 1;
    gc.Parent::GrandParent::num = 0; // g++ error: ‘GrandParent’ is an ambiguous base of ‘GrandChild’
    gc.GrandParent::num = 5;         // g++ error: ‘GrandParent’ is an ambiguous base of ‘GrandChild’

                                                 // --VC12 output; g++ output--
    cout << gc.num << endl;                      // 2 ; 2
    cout << gc.Parent::num << endl;              // 1 ; 1
    cout << gc.Parent::GrandParent::num << endl; // 0 ; N/A due to above error
    cout << gc.GrandParent::num << endl;         // 5 ; N/A due to above error
}

2.) Why is (a) gc.Parent::GrandParent::num ambiguous in g++ when (b) gc.Parent::num isn't? (a) uniquely describes its location on the inheritance tree. gc only has 1 Parent subobject, which only has 1 GrandParent subobject, which only has 1 num. For (b), gc has one Parent, which has its own num but also a GrandParent subobject with another num.

3.) For gc.GrandParent::num, it seems VC12 looks into gc's immediate GrandParent base subobject for the latter's num. I’m guessing the reason it is unambiguous is that it’s a name lookup qualified by gc, so the entity to the right of . is looked for first in gc's scope, and the most immediate GrandParent to gc's scope is the directly inherited one, not the indirectly inherited one via Parent. Am I wrong?

4.) Why is gc.GrandParent::num ambiguous to g++ when gc.Parent::num isn't? If one is ambiguous, then shouldn't both be equally ambiguous? For the prior, gc has two GrandParents; and for the latter, Parent has 2 nums.


Gregoire, Marc R. et al. Professional C++, 2nd ed. Indianapolis, IN: Wiley Pubishing, 2011. p. 241. Print.

6
  • 6
    "[Professional C++, 2nd ed.]† states a compilable program can't have a class that directly inherits both its immediate parent and said parent's parent class. Is it true?" No, that's wrong. The C++ Standard even contains an example and states there that it is well-formed, see [class.mi]/3 class D : public A, public L { void f(); /∗ ... ∗/ }; // well-formed, where class A : public L { /∗ ... ∗/ };
    – dyp
    Commented Dec 20, 2013 at 0:00
  • 1
    In the same paragraph, a note says: "A class can be an indirect base class more than once and can be a direct and an indirect base class. There are limited things that can be done with such a class. The non-static data members and member functions of the direct base class cannot be referred to in the scope of the derived class." (emphasis mine).
    – dyp
    Commented Dec 20, 2013 at 0:20
  • I think the reason why those accesses are ambiguous is that GrandChild::Parent::GrandParent is equivalent to GrandChild::GrandParent in terms of name lookup. Similarly, struct A { int x; }; struct B : A {}; struct C : A {}; struct D : B, C {}; D d; d.B::A::x = 42; fails.
    – dyp
    Commented Dec 20, 2013 at 0:26
  • "how to unambiguously access embedded subobjects" You could do that via something like static_cast<Parent&>(gc).GrandParent::num = 42;
    – dyp
    Commented Dec 20, 2013 at 0:32
  • +1 Interesting question. Adding to the mix of compilers, Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn), reports the same ambiguity errors as gcc.
    – WhozCraig
    Commented Dec 20, 2013 at 0:33

2 Answers 2

3

The common term for this is the diamond pattern (or diamond problem).

It is not an error per se, but as noted in the comments here, any attempt to access a direct base that is reduplicated elsewhere in the hierarchy will result in an ambiguity error.

One workaround would be to make the base indirect. The new inheriting constructors feature in C++11 allows perfect wrappers:

template< typename base, typename tag >
struct disambiguated_base : base
    { using base::base; };

Given an unused tag type, this generates a new class derived from, and functionally identical to, the given base. The tag type may be an incomplete class denoted by an elaborated-type-specifier:

struct GrandChild : Parent,
    disambiguated_base< GrandParent, class grandchild_grandparent_tag > {

    typedef disambiguated_base< GrandParent, grandchild_grandparent_tag >
        my_direct_grandparent;

    int num;
};

Now GrandChild can use my_direct_grandparent:: to disambiguate member accesses.

5
  • +1. Ingenious. But, I wanted to access GrandChild’s Parent’s GrandParent, too. It’d be cumbersome to come up with another name like grandchilds_parents_grandparent for the elaborated-type-specifier, especially if the diamond gets more generations. Can it be indexed instead to allow GrandChild.disambiguator<0>:: and GrandChild.disambiguator<1>::? i.e., can disambiguated_base’s type tag instead be a template template parameter, whose template parameter is an int N? How would the incomplete class template foo<N> be denoted in the elaborated-type-specifier? Thanks.
    – CodeBricks
    Commented Dec 20, 2013 at 9:09
  • @CodeBricks Yeah, the tag can be anything, including a non-type, if you declare it that way. An incomplete specialization Foo<N> is called simply Foo<N>, but you do need to declare template< int N > class Foo;. You don't need disambiguation except to a direct base, though. Parent::GrandParent:: works fine alongside my_direct_grandparent::. Commented Dec 20, 2013 at 9:14
  • *Meant to say disambiguator<N> instead of foo<N>. gc.Parent::GrandParent:: gives g++ error. GrandParent is a direct base class of GrandChild. If I do template< typename base, template <int N> class tag > struct disambiguated_base… then how do I make the corresponding changes in the GrandChild's inheritance statement for an incomplete, yet index-able, class template? struct GrandChild : Parent, disambiguated_base< GrandParent, class template<int N> class grandchild_grandparent_tag > ?
    – CodeBricks
    Commented Dec 20, 2013 at 9:29
  • You don't need to change any declarations, except to add the template forward declaration I mentioned in the last comment. Then use it as struct GrandChild : Parent, disambiguated_base< GrandParent, disambiguator< 3 > > {. The tag can be any typename, such as int, string, class this_is_a_unique_name, or some_template< 42 >. Commented Dec 20, 2013 at 9:35
  • @CodeBricks Automatically generating a tag is beyond the scope of this question, and may not be possible at all. Better to open a new question. Commented Dec 20, 2013 at 10:15
1

I’m adding to the accepted answer. It states a derived class cannot access a direct base class if the derived class also indirectly inherits base. Its solution makes the base class indirect by wrapping it with a template whose second type argument is tag. This ensures base is indirect to the derived class provided the derived class extends the wrapped base with a unique tag. The below example will use a non-type tag.

If the diamond-like problem were generalized to contain more generations in the form of:

  • ith class inherits from base and (i – 1)th,
  • (i – 1)th inherits from base and (i – 2)th,
  • …, and
  • 2nd inherits from base,

then it's an improvised container, where each element is stored in each uniquely tagged base. In that case, tag-making should be automated. One way is to consolidate all derived classes via a non-type template. Its non-type parameter N can specify the number of recursive inheritance iterations. By making tag a non-type parameter, the value of the parameter determining the number of subclasses can be uniquely related to that of the one tagging each subobject type. For example, tag = 10 corresponds to N = 10, which refers to the 10th generation on the hierarchy:

// disambiguated_wrapper.h

struct int_wrapper {
    int num;
};

template < typename base, unsigned int tag >
struct disambiguated_wrapper : base {
    using base::base;
};

// improvised_container.h

#include "disambiguated_wrapper.h"

template <unsigned int N>
struct improvised_container : 
    protected disambiguated_wrapper<int_wrapper, N>, 
    protected improvised_container<N - 1> {

    unsigned int size() const { return N; }

    int& at(const unsigned int index) {
        if (index >= N) throw "out of range";
        else return (index == N - 1) ?
            this->disambiguated_wrapper<int_wrapper, N>::num :
            this->helper(index);
    }
protected:
    int& helper(const unsigned int index) {
        return (index == N - 1) ?
            this->disambiguated_wrapper<int_wrapper, N>::num :
            this->improvised_container<N - 1>::helper(index);
    }
};
#include "specializations.h"

// specializations.h

template <>
struct improvised_container<0> {
    improvised_container() = delete;
}; // ^ prohibits 0-length container

template <>
struct improvised_container<1> : 
    protected disambiguated_wrapper<int_wrapper, 1> {

    unsigned int size() const { return 1; }

    int& at(const unsigned int index) {
        if (index != 0) throw "out of range";
        else return this->disambiguated_wrapper<int_wrapper, 1>::num;
    }
protected:
    int& helper(const unsigned int index) {
        if (index != 0) throw "out of range";
        else return this->disambiguated_wrapper<int_wrapper, 1>::num;
    }
};

// main.cpp

#include "improvised_container.h"
#include <iostream>
int main() {
    improvised_container<10> my_container;
    for (unsigned int i = 0; i < my_container.size(); ++i) {
        my_container.at(i) = i;
        std::cout << my_container.at(i) << ",";
    }   // ^ Output: "0,1,2,3,4,5,6,7,8,9,"
}

Element access at cannot decrement the index to recursively call itself, because index isn’t a compile-time constant. But N is. So, at calls helper, which recursively calls the (i – 1)th version of itself in the (i – 1)th subobject, decrementing N until it equals index – 1, with each call moving one scope deeper, and finally returning the element of the target scope. It checks against index – 1 and not index, because the 0th improvised_container specialization's ctor is deleted. at compensates for the off-by-one.

improvised_container uses protected inheritance to prevent client code from accessing the at and size methods of its base subobjects. The subobject’s size is less than the enclosing object’s.

This works in g++ 4.8. The inheriting constructor using base::base causes errors in VC12, but it can be omitted because the element type is int.

5
  • Ah… I haven't totally grokked this yet, but it looks like you're implementing something like a std::tuple. Commented Dec 21, 2013 at 3:02
  • @Potatoswatter, I just added an explanation at the bottom. Honestly, I don't know std::tuple's implementation.
    – CodeBricks
    Commented Dec 21, 2013 at 3:43
  • Various implementations are possible including a chain of bases. The critical feature is that it returns a subobject given an index. Although your getter works at runtime, I think it could be adjusted to add a compile-time variant as well. Commented Dec 21, 2013 at 4:02
  • I don't understand that last comment… I'm not suggesting or criticising anything, just mentioning the similarity of implementation details to tuples. Commented Dec 22, 2013 at 1:36
  • Oh, I didn't know you were interested in a compile time variant. Since std::tuple is already generally tuned to whatever compiler you're using, it's better not to reimplement it. Chain of bases isn't particularly efficient anyway. I have no idea what your use case is, I'm just mentioning similarities and answering questions in a vacuum, no context applied. Commented Dec 22, 2013 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.