问题描述
我有一个 A 类,其中包含一个方法 open 的多个重载(全部使用可变参数模板):
class A {
template<typename... Args>
bool open(int,Args...);
template<typename... Args>
bool open(int,string,char,Args...);
}
然后我有一个类B,它有一个成员A,并且必须支持这个开放函数的所有重载
class B {
A a;
template<typename... Args>
bool open(int i,Args... args) {
// do stuff
return a.open(i,args...);
}
template<typename... Args>
bool open(int i,string s,Args... args) {
// do the same stuff
return a.open(i,s,char c,c,args...);
}
}
但我想避免在 B 类上创建每个重载的“副本”,因为它们采用与 A 相同的参数。我想知道是否可以创建一个通用的打开 方法在 B 转发其参数,并且编译器可以在调用 b.open() 时自动调用正确的重载,如下所示:
template<typename... Args>
bool open(Args&& ... args) {
// do stuff
return a.open(std::forward<Args>(args)...);
}
然后,如果需要,可以为每个重载创建一个模板特化。 (仍然比每个重载的完整方法要好得多)。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)