【问题标题】:C++ inherit template class with its own in-class structC++ 继承具有自己的类内结构的模板类
【发布时间】:2020-11-16 22:02:17
【问题描述】:
template <typename Foo>
class A{

};

class B : A<B::Bar>{
    struct Bar{

    };
};

我想知道我怎么能做这样的事情,或者这是将 struct Bar 移到 B 类之外的唯一方法。

【问题讨论】:

  • 如果你坚持使用B::Bar,你可以使用class B : A&lt;X&gt; { using Bar = X; }(当然你需要提供X的实际定义)

标签: c++ class templates inheritance


【解决方案1】:

你根本做不到!

您不能使用未知类型作为模板参数,也不能转发嵌套类类型。两者加在一起就不可能做你喜欢做的事情。

关于转发嵌套类声明,请参见此处:forward declaration of nested class

已经有人提议在 C++ 中添加这样的东西,但我认为它不是标准的一部分,即使在 C++20 中也没有。 Forward declarations of nested classes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2020-12-14
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    相关资源
    最近更新 更多