From my reading of answers on SO and the cppreference link
The inherited constructors are equivalent to user-defined constructors with an empty body and with a member initializer list consisting of a single nested-name-specifier, which forwards all of its arguments to the base class constructor.
I had concluded that the below classes D
and E
should behave indentically.
#include <string>
#include <utility>
using namespace std;
class B
{
public:
B(string&& a) : a(move(a))
{
}
string a;
};
class D : public B
{
public:
using B::B;
};
class E : public B
{
public:
E(string&& a) : B(a)
{
}
};
string foo()
{
return "bar";
}
int main()
{
D d = foo();//This compiles
E e = foo();//This does not compile
return 0;
}
E e = foo()
rightly fails to compile, since B
's constructor only accepts a string&&
. However, D d = foo()
goes through fine. Why is that?
The compiler used is clang3.5.
EDIT: Also, as explained in this answer, the perfect forwarding idiom isn't a replacement for inheriting constructors. So, what does the body look like exactly?