请考虑以下代码:
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 ;
我看不出歧义。
[proposal for std::bases
std::direct_bases
可能有帮助,但被拒绝了。