As the title says, I'm wondering if there's a way to expose a protected constructor in a derived class (that is, to change access from protected
to public
). Consider the following (contrived) example:
struct A {
int a;
int b;
protected:
A(int, int) {};
void do_something();
};
struct B : A {
B() : A(0, 0) {};
using A::A;
using A::do_something;
};
int main() {
B b(0, 0); // error: 'A::A(int, int)' is protected
//B b{}; // no errors
b.do_something();
}
So one can change the access of protected member functions, but not of constructors? If so, what is the rationale for the restriction?
Workaround: Variadic template with argument forwarding can serve as a workaround that would perform identically.
Speculation for the rationale: by default the constructor of base classes are not "inherited" in the regular sense; the constructor of derived class must construct the base class instance(s) first. When using
is used upon regular member functions, it introduces the functions into the current declarative region thus changing access; on constructors, using
only brings base constructor to the "normal inheritance" level (identical to other member functions without using
). Because only in some cases is constructor inheritance (reuse of base constructors) useful, the standard committee people designated using X::X
syntax for constructor inheritance rather than the stronger namespace teleporting.