Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perfect forwarding constructor and inheriting constructors

Given this class hierarchy:

#include <iostream>

class Base {
public:
    Base() = default;
    Base(const Base&) { std::cout << "  copy\n"; }
    
    template<typename T>
    Base(T&&) { std::cout << "  T&&\n"; }
};

class Sub : public Base {
public:
    using Base::Base;
};

It is known that this code would print T&&:

// objects
Base varObj;
const Base constObj;
// invoking constructors
Base copy(varObj);              // T&&
Base move(std::move(constObj)); // T&&
Base number(42);                // T&&

Why does using Sub instead of Base "fix" the problem?

// objects
Sub varObj;
const Sub constObj;
// invoking constructors
Sub copy(varObj);              // copy
Sub move(std::move(constObj)); // copy
Sub number(42);                // T&&
like image 567
Kane Avatar asked Aug 28 '21 08:08

Kane


People also ask

Can you inherit constructor?

Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

How do you inherit a constructor in C++?

Constructor in Multiple Inheritance in C++ The main job of the constructor is to allocate memory for class objects. Constructor is automatically called when the object is created. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can derive from several(two or more) base classes.

Do derived classes inherit constructors?

In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.

Can a constructor be inherited by a derived class?

Instead of inheriting constructors by the derived class, it is only allowed to invoke the constructor of base class. In C#, when we are working with the constructor in inheritance there are two different cases arise as follows:

Are perfect-forwarding Constructors accurate-characterization?

perfect-forwarding constructor accurate-characterization Beware of Perfect Forwarding Constructors By Michael ParkJune 07, 2014May 07, 2016 The Problem Suppose we want to write a wrapper class in modern C++. Of course we want to take advantage of move-semantics along with perfect forwarding.

Is perfect forwarding specific to copy constructors?

It’s not specific to perfect forwarding, nor is it specific to copy constructors. Therefore, it shouldn’t be remembered as if it’s a special case. Summary

What are the different cases of constructor in inheritance?

In C#, when we are working with the constructor in inheritance there are two different cases arise as follows: Case 1: In this case, only derived class contains a constructor . So the objects of the derived class are instantiated by that constructor and the objects of the base class are instantiated automatically by the default constructor.


1 Answers

This is CWG2356: the inherited constructor is still a better match for a non-const input (because of the qualification conversion required), but it is discarded by [over.match.funcs]/8. Part of the reason for this is to avoid making any class that inherits constructors implicitly convertible from its base class.

There is still the possibility that the Sub copy constructor (used by both of your initializations) would invoke the constructor template. However, its source parameter is a const Sub&, so its base subobject is a const Base&. That’s an exact match for the Base copy constructor, so it’s selected over the template (which otherwise wins because of qualification conversions or reference kind).

like image 177
Davis Herring Avatar answered Sep 18 '22 11:09

Davis Herring