从 C++ 中的私有模板类继承构造函数,
解释了using Base::Base;
在内部Derived
我们将Base
注入的类型Base
作为Base::Base
作用域Derived
而不是实际Base
的 c-tor(s)。我很感兴趣,whys
而且hows
比上面提到的问题更深入。
在下面的代码片段中,有 3 个模板类MyVector...
继承自 std::vector。我很好奇为什么using Super::vector;
还不足以让事情为private
和protected
案例工作?它不应该将vector
с-tor带入 'public
的MyVector...
范围,从而改变对它的访问权限,就像其他成员(如at
and )一样push_back
?如果我们尝试using Super::vector;
将构造函数与. 即使它是一种类型,为什么语言不像它那样公开访问它?C2923
Super::vector
IsAType
using Super::iterator;
#include <vector>
template< class T >
struct IsAType {};
template< class T >
class MyVectorPublic : public std::vector< T >
{
using Super = std::vector< T >;
public:
using Super::vector;
using Super::at; // function
using Super::push_back; // function
using Super::iterator; // type
// IsAType< Super::vector > t; //
};
template< class T >
class MyVectorProtected : protected std::vector< T >
{
using Super = std::vector< T >;
public:
using Super::vector;
using stdvec = typename Super::vector;
using Super::at; // function
using Super::push_back; // function
using Super::iterator; // type
// IsAType< Super::vector > t; // C2923
};
template< class T >
class MyVectorPrivate : private std::vector< T >
{
using Super = std::vector< T >;
public:
using Super::vector;
using stdvec = typename Super::vector;
using Super::at; // function
using Super::push_back; // function
using Super::iterator; // type
// IsAType< Super::vector > t; // C2923
};
int main()
{
auto vecPub = MyVectorPublic< int >::vector();
//auto vecProt = MyVectorProtected< int >::vector(); // C2247
//auto vecPriv = MyVectorPrivate< int >::vector(); // C2247
auto vecProt = MyVectorProtected< int >::stdvec();
auto vecPriv = MyVectorPrivate< int >::stdvec();
auto itPub = MyVectorPublic< int >::iterator();
auto itProt = MyVectorProtected< int >::iterator();
auto itPriv = MyVectorPrivate< int >::iterator();
}
错误 C2247:“std::vector<int,std::allocator>”不可访问,因为“MyVectorProtected”使用“protected”从“std::vector<int,std::allocator>”继承
错误 C2247:“std::vector<int,std::allocator>”不可访问,因为“MyVectorPrivate”使用“private”从“std::vector<int,std::allocator>”继承
错误 C2923:“IsAType”:“std::vector<int,std::allocator>::{ctor}”不是参数“T”的有效模板类型参数
更新: 所以看起来真正的原因是当我们谈论构造函数时,标准忽略了使用声明访问修饰符:http: //eel.is/c++draft/namespace.udecl#14 http://eel.is/c ++draft/namespace.udecl#16 如果我错了,请纠正我。