gpt4 book ai didi

c++ - 为什么不能使用 typedef 类型来声明其父类的构造函数?

转载 作者:行者123 更新时间:2023-12-02 03:30:04 27 4
gpt4 key购买 nike

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 类型来声明其父类的构造函数?

最佳答案

类似的行为早在 reported作为一个可能的 Clang bug:[Bug 23107] 模板上的构造函数继承无法正常工作

理查德·史密斯对此报告的评论:

The C++ committee have discussed this case and did not intend for that syntax to be valid. Use using myBase::myBase; instead to declare an inheriting constructor.

因此,您应该编写 using Base::Base; 而不是 using Base::A;。完成此修复后,您的代码将使用 Clang 进行编译。

关于c++ - 为什么不能使用 typedef 类型来声明其父类的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61605600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com