公开最直接的基类模板名称

Expose the most direct base class template name

本文关键字:基类      更新时间:2023-10-16

考虑以下代码:

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<int>::base表示层次结构中最"直接"的基类(在本例中为base<int>(。欢迎使用模板元编程。

如果合适,您可以在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 ;

我看不出消除歧义的方法。

有人提出std::basesstd::direct_bases可能会有所帮助,但遭到拒绝。