【问题标题】:Expose the most direct base class template name暴露最直接的基类模板名称
【发布时间】:2020-06-02 19:06:58
【问题描述】:

考虑以下代码:

template <class...> struct base {};
template <class... T> struct intermediate: base<void, T...> {};
template <class... T> struct derived: base<T...>, intermediate<T...> {};

using type1 = derived<int>::intermediate::base; // works
using type2 = derived<int>::base; // ambiguous

有没有办法让它工作起来没有歧义,这样derived&lt;int&gt;::base 就意味着层次结构中最“直接”的基类(在这个例子中是base&lt;int&gt;)。欢迎使用模板元编程。

【问题讨论】:

  • 你能换成struct derived: wrapped&lt;base&lt;T...&gt;&gt;, intermediate&lt;T...&gt; {};吗?
  • 是的。只要我能输入derived&lt;int&gt;::basederived&lt;int&gt;::intermediate::base,我就可以改变层次结构
  • 可以derived&lt;int&gt;::wrapped::base, derived&lt;int&gt;::intermediate::base... :/

标签: c++ templates inheritance c++17 multiple-inheritance


【解决方案1】:

如果合适,您可以在derived 中添加别名,以便您可以从外部使用它们:

template <class... Ts> struct derived: base<Ts...>, intermediate<Ts...>
{
    using DirectBase = base<Ts...>;
    using IndirectBase = typename intermediate<Ts...>::base;
};

然后

using type1 = derived<int>::IndirectBase ;
using type2 = derived<int>::DirectBase ;

我没有办法消除歧义。

proposal for std::bases std::direct_bases 可能会有所帮助,但被拒绝了。

【讨论】:

  • 很遗憾,我想保持相同的名称(在我的实际情况下,base 可能是一个模板模板,我想保持相同,不管它是什么)
  • 你不能做using base = base&lt;Ts...&gt;; 也不能:-/
  • 通常间接基类是基类的基类,而不是“共同父类”
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 2016-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多