为什么 typedef 类型不能用于声明其父类的 ctors?

Why can't a typedef type be used to declare its parent class' ctors?

本文关键字:父类 ctors 声明 typedef 类型 不能 用于 为什么      更新时间:2024-05-10
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进行编译。

正如其他人所评论的,您的代码在最新的GCC和MSVC上编译时没有问题。

你的问题似乎发生在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;
};