Document number:not submitted
Date:2014-03-27
Project:JTC1.22.32 Programming Language C++
Reply-to:Rouslan Korneychuk <rouslank at msn dot com>

A Proposal to Add Base Class Aliases to the Language

I. Table of Contents

II. Introduction

This proposal suggest adding a short-hand notation for creating aliases to base classes within the scope of class.

e.g.:

class A : protected B = C, public D {
public:
    A() : B(3) {}

    void do_something() {
        /* ... */
        B::do_something();
    }
};

which would be approximately equivalent to:

class A : protected C, public D {
protected:
    typedef C B;

public:
    A() : B(3) {}

    void do_something() {
        /* ... */
        B::do_something();
    }
};

The difference being that in the proposed syntax, the name B is specified sooner and thus could be used in the second base-specifier. The name B would always have the same accessibility as the name C.

III. Motivation

Most of the time, a class' base class can be easily referred to by its name due to class name injection, but there are a few cases where this does not work and if the base class is a template, requires either repeating template parameters or a work-around.

Dependent type names

e.g.:

template<typename T,typename Pasta> class monstrous :
    public something<
        monstrous<T,Pasta>,
        allocator<monstrous<T,Pasta>>,
        comparison<monstrous<T,Pasta>,
        and_so_on<Pasta>,
        and_so_forth<T,etc<T>>> {
    /* ... */
};

A very long instantiation such as this is particularly undesirable to have to repeat. The injected name something cannot be used because the definition of its class is not available before monstrous is defined.

Unknown class names

e.g.:

// elsewhere:
template<typename Alloc> using particular_t = typename particular<Alloc>::type;

/* ... */

class A : public particular_t<my_allocator<double>> {
    /* ... */
};

Here again the base class cannot be referred to by particular_t, since that is not its name. Looking at the definition of particular_t it looks like type is its name, but that might not be so. type might actually be a typedef to another name, and in fact, the actual name of the class may vary depending on the template parameter.

Ambiguous names

class A : public B<X>, public B<Y> {
    /* ... */
};

Here the name B is ambiguous.

Alternatives

The first example can be solved like so:

template<typename T,typename Pasta> class monstrous : public something</* ... */> {
    typedef typename monstrous::something base_t;
    /* ... */
};

This, however, will not solve the problem in the other two examples.

A particular template instantiation may be declared in a typedef before being used in a derived class. Disadvantages: this pollutes the namespace with extra names, and doesn't work in the first case. In the first case, one could forward declare monstrous and declare a template alias with the same parameters, but this would only exchange duplicating the base class' arguments with duplicating the parameters of the derived class.

An extra template parameter can be added to the derived class to be used in place of the base class, with a default value of the base class that is desired. e.g.:

template<typename T,typename B=some_base</*...*/>> class some_class : public B {
    /* ... */
};

Disadvantages: This solution is not obvious, doesn't work for the first case (the names B and some_class are not available at that point) and would require turning a plain class into a template if it wasn't one already.

Work is underway to develop a reflection standard for C++, so in the future, given a class A, one will probably be able to refer to—for example— the first direct base class with something like std::base_t<A,0>. This however lacks the informativeness of a meaningful name and is brittle, since adding, removing or reordering base classes would change the numbering.

With the new syntax

The proposed syntax would allow resolving the issues in the examples like so:

// example 1
template<typename T,typename Pasta> class monstrous :
    public base_t = something<
        monstrous<T,Pasta>,
        allocator<monstrous<T,Pasta>>,
        comparison<monstrous<T,Pasta>,
        and_so_on<Pasta>,
        and_so_forth<T,etc<T>>> {
    /* ... */
};

// example 2
class A : public particular = particular_t<my_allocator<double>> {
    /* ... */
};

// example 3
class A : public Bx = B<X>, public By = B<Y> {
    /* ... */
};

IV. Impact On the Standard

The proposed syntax is currently ill-formed and thus will not affect existing code.

V. Design Decisions

The format is chosen for its similarity to the existing alias-declaration.

VI. Technical Specifications

The following changes to the grammar are required:

A.9 Derived classes [gram.derived]

base-specifier:
attribute-specifier-seqopt base-type-specifier base-alias
attribute-specifier-seqopt virtual access-specifieropt base-type-specifier base-alias
attribute-specifier-seqopt access-specifier virtualopt base-type-specifier base-alias
base-alias:
base-type-specifier
identifier = base-type-specifier
base-type-specifier:
class-or-decltype

A typedef-name can be introduced with a base-alias if the base-type-specifier is preceded by an identifier and an equals sign. The semantics are the same as if the name was defined with a typedef in the base class, except it is only introduced into the scope of the derived class and is only available after the base-alias is declared. The aliased name, therefore, has the same accessibility as the inherited base class name.

VII. References

The standards and terms are taken from the working draft N3797.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf