Constructor Inheritance and Redirection

C ++ 11 allows you to inherit constructors that avoid a lot of patterns, especially with something like a wrapper class. However, it seems that you can already achieve this functionality with the help of variable templates.

class B
{
public:
 B(int){//do something}
 B(int, char){//do something}
};

Using inheriting constructors:

class D : public B
{
public:
 using B::B; 
};

Using variable templates and transitions:

class D : public B
{
public:
 template <typename...Args>
 D(Args&&... args) : B(std::forward<Args>(args)...)
 {
 }
};

( using) - , , ? CWG (N1890 N1898) , :

, , -. "ctor" "", , , . .

+4
3

, . :

struct B {
    B(int* ) { .. }
};

:

D d{0};

, . -, B(int ), 0 int, . !

, , , .

+4

. , 0 int nullvalue. , , 0 , .

, , -, , ( ) , , , . ++ .

+3

( ) "":

struct Base
{
    Base(int) {}
};

struct Derived : public Base
{
    using Base::Base;
};

struct DerivedTemplate : Base
{
    template <typename...Args>
    DerivedTemplate(Args&&... args)
    : Base(std::forward<Args>(args)...)
    {}
};

int main()
{
    // error: no matching function for call to ‘Derived::Derived(int, int)’
    Derived d(1, 2);

    // In instantiation of ‘DerivedTemplate::DerivedTemplate(Args&& ...) [with Args = {int, int}]’:
    // required from here
    // error: no matching function for call to ‘Base::Base(int, int)’
    DerivedTemplate dt(1, 2);
}

, ( )

+2
source

All Articles