说我有以下代码:
template <class ...Args>
DoSomethingWithSubType() { ... }
template <typename T>
void DoSomething() {
DoSomethingWithSubType<typename T::SubType>(T)
}
struct Foo {
typedef Bar SubType;
};
Foo foo;
DoSomething(foo);
注意:
DoSomethingWithSubType
(更改它不是一个选项)。DoSomething
通用,以接受任意数量的args。DoSomething
还需要foo
。DoSomething
时,我不想提供模板参数(例如DoSomething(foo, bar)
,不是DoSomething<Foo::SubType, Foo::SubType>(foo, bar)
像这样重载是一个明显但冗长的选择:
template <typename T>
void DoSomething() {
DoSomethingWithSubType<typename T::SubType>()
}
template <typename T1, typename T2>
void DoSomething() {
DoSomethingWithSubType<typename T1::SubType, typename T2::SubType>()
}
template <typename T1, typename T2, typename T3>
void DoSomething() {
DoSomethingWithSubType<typename T1::SubType, typename T2::SubType, typename T3::SubType>()
}
有没有办法做到这一点,但只写一次?