如果派生类定义了相同的名称,派生类将隐藏基类的重载名称,但我们总是可以使用using-declaration引入该重载集:
template <class BASE>
class A : public BASE
{
public:
using BASE::some_method;
void some_method();
}
Run Code Online (Sandbox Code Playgroud)
但是如果我从可变参数基类引入所有重载集呢?我可以写这样的东西吗?
template <class... BASES>
class A : public BASES...
{
public:
using BASES::some_method...;
void some_method();
}
Run Code Online (Sandbox Code Playgroud)
我考虑过使用一个帮助类:
template <class... BASES>
struct helper;
template <>
struct helper<> {};
template <class OnlyBase>
struct helper<OnlyBase> : OnlyBase
{
using OnlyBase::some_method;
};
template <class Base1, class... OtherBases>
struct helper<Base1, OtherBases> : public Base1, public helper<OtherBases...>
{
using Base1::some_method;
using helper<OtherBases...>::some_method;
};
Run Code Online (Sandbox Code Playgroud)
它确实有效.但它需要大量的输入(当然我可以使用宏,但我尽可能尝试使用c ++的编译时功能),当我想引入更多方法时,我必须在那段代码中做出很多改变.
一个完美的答案是一个简单的语法,但如果没有,我将使用帮助类.
c++ inheritance overloading using-declaration variadic-templates