继承构造函数模板时,GCC(6.2.0通过MinGW-w64,6.3通过Ideone.com)似乎忽略了访问修饰符。
#include <iostream>
class Base
{
protected:
template<typename ...Args>
Base(Args ...args)
{
std::cout << "variadic ctor" << std::endl;
}
template<typename T>
Base(T t)
{
std::cout << "template ctor" << std::endl;
}
Base(char const *s)
{
std::cout << s << std::endl;
}
};
class Derived : public Base
{
protected:
using Base::Base;
};
int main(int, char**) noexcept
{
//Base bv{ 0, 1 }; // error: 'Base::Base(Args ...) [with Args = {int, int}]' is protected within this context
//Base bt{ 0 }; // error: 'Base::Base(T) [with T = int]' is protected within this context
//Base bs{ "base" }; // error: 'Base::Base(const char*)' is protected within this context
Derived dv{ 0, 1 };
Derived dt{ 0 };
//Derived ds{ "derived" }; // error: 'Derived::Derived(const char*)' is protected within this context
return 0;
}
这些都不应该是可构造的,但Derived类仍然可以通过模板化构造函数构造。如char const
构造函数所示,只有非模板构造函数继承了正确的访问修饰符。
这是不正确的行为,不是吗?
编辑:This answer在using X::X;
时提供以下相关引文:
如此声明的构造函数具有与X中相应构造函数相同的访问权限。
从12.9 / 4开始,“继承构造函数”。