Search code examples
c++classconstructorusinglanguage-lawyer

Constructor nominated by using declaration


I'm trying to declare a member name which is constructor of base class as the following:

#include <iostream>

class A{ };

class B: public A
{
    using A::A; //error: ‘A::A’ names constructor
};

int main()
{
}

Where is it specified that constructor cannot be accepted by using declaration? I'm looking for a corresponding quote from the Standard.


Solution

  • Where is it specified that constructor cannot be accepted by using declaration?

    Nowhere, because it can. See 12 Special Member Functions:

    12.9 Inheriting constructors [class.inhctor]

    A using-declaration (7.3.3) that names a constructor implicitly declares a set of inheriting constructors. The candidate set of inherited constructors from the class X named in the using-declaration consists of actual constructors and notional constructors that result from the transformation of defaulted parameters as follows:

    — all non-template constructors of X, and

    — for each non-template constructor of X that has at least one parameter with a default argument, the set of constructors that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list, and

    — all constructor templates of X, and

    — for each constructor template of X that has at least one parameter with a default argument, the set of constructor templates that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list.

    ....

    Here's an example:

    struct A
    { 
      explicit A(int) {}    
    };
    
    struct B: A
    {
        using A::A;
    };
    
    int main()
    {
        B b{42};
    }