为什么 typedef 类型不能用于声明其父类的 ctors?
Why can't a typedef type be used to declare its parent class' ctors?
template<typename>
struct A
{
int n;
A(bool)
{}
};
template<typename>
struct B
{
struct C : A<B>
{
using Base = A<B>;
using A<B>::A; // ok
using Base::n; // ok
// error: dependent using declaration resolved to type without 'typename'
using Base::A;
};
C get() const
{
return C(true);
}
};
int main()
{
auto b = B<int>();
b.get();
}
代码中描述了错误。
为什么typedef类型不能用来声明其父类的actor
类似的行为早些时候被报道为可能的Clang bug:[bug 23107]模板上的构造函数继承无法正常工作。
Richard Smith对此报告的评论:
C++委员会已经讨论过这个案例,并不打算使用该语法有效。请改用
using myBase::myBase;
来声明继承构造函数。
因此,应该编写using Base::Base;
而不是using Base::A;
。修复后,您的代码将使用Clang进行编译。
你的问题似乎发生在Clang身上。
该标准与析构函数的命名问题类似(来源(:
在形式为的合格id中
[…]类型名称::~类型名称
第二个类型名称是在与第一个类型名称相同的范围中查找的。
struct C {
typedef int I;
};
typedef int I1, I2;
extern int* p;
extern int* q;
p->C::I::~I(); // I is looked up in the scope of C
q->I1::~I2(); // I2 is looked up in the scope of the postfix-expression
struct A {
~A();
};
typedef A AB;
int main() {
AB* p;
p->AB::~AB(); // explicitly calls the destructor for A
}
但我找不到任何与构造函数相关的明确内容。我认为行为应该是一样的,但只有更有经验的人才能证实。
有趣的是,如果你让A
类不是一个模板,它也适用于Clang:
struct A
{
A(bool) {}
};
template<typename>
struct B
{
struct C : A
{
using Base = A;
using Base::A;
};
//...
也许那是Clang虫?
可以做的一件事是使用Base
的构造函数:Base::Base
:
struct C : A<B>
{
using Base = A<B>;
using Base::Base;
};
相关文章:
- 从类继承时,继承的类是否会通过父类重新定义继承的变量
- 从父类方法返回子类对象
- c++, 在子类中,如何在没有对象的情况下访问父类的方法?
- 将父类对象强制转换为子类的问题
- 在运行时选择父类的实现
- 如何在C++子类中访问父类的私有变量
- 将父类的子类的数据复制到具有相同父类的另一个类
- 父类的私有函数会导致对具有相同名称和相似参数的子类中的公共函数的不明确调用
- C++ 将子类的对象添加到父类的向量中
- 两个父类的构造函数的序列
- 为什么我的子类不继承父类的字符串?
- C++调用使用重写函数的父类函数
- 在父类中公开受保护的构造函数
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 如何使父类不重复已经执行的祖父方法
- 从模板化父类中的派生内部类访问受保护的成员变量
- Arduino在子类中使用父类方法
- 使用 Clang LibTooling 扫描C++在模板化父类中调用本地类的源
- 从父类的向量访问子类函数,而无需向下转换
- 将派生类的构造函数声明为父类的友元