cat*_*dle 5 c++ templates variadic-templates c++11
这就是我想要做的:
// base case
void f() {}
template <typename T, typename... Ts>
void f() {
// do something with T
f<Ts...>();
}
int main() {
f<int, float, char>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它不编译:
prog.cpp: In instantiation of ‘void f() [with T = char; Ts = {}]’:
prog.cpp:6:5: recursively required from ‘void f() [with T = float; Ts = {char}]’
prog.cpp:6:5: required from ‘void f() [with T = int; Ts = {float, char}]’
prog.cpp:10:25: required from here
prog.cpp:6:5: error: no matching function for call to ‘f()’
prog.cpp:6:5: note: candidate is:
prog.cpp:4:6: note: template<class T, class ... Ts> void f()
prog.cpp:4:6: note: template argument deduction/substitution failed:
prog.cpp:6:5: note: couldn't deduce template parameter ‘T’
Run Code Online (Sandbox Code Playgroud)
此线程显示了解决此问题的方法,但基本案例必须是模板.我真的不喜欢它,因为据我所知,我将不得不重复使用T的代码.有没有办法避免这种情况?
到目前为止,我想出了两个解决方案(http://ideone.com/nPqU0l):
template <typename...> struct types_helper {};
// base case
void f(types_helper<>) {}
template <typename T, typename... Ts>
void f(types_helper<T, Ts...>) {
// do something with T
f(types_helper<Ts...>());
}
int main() {
f(types_helper<int, float, char>());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <type_traits>
struct end_of_list;
template <typename T>
void f() {
static_assert(std::is_same<T, end_of_list>::value, "error");
}
template <typename T1, typename T2, typename... Ts>
void f() {
// do something with T
f<T2, Ts...>();
}
int main() {
f<int, float, char, end_of_list>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来做到这一点.
Cas*_*eri 12
另一种方法是将非模板函数f转换为可变参数模板函数,该函数接受零个或多个模板参数(另一个f需要一个或多个模板参数).然后为避免歧义,SFINAE在参数个数不为零时离开此模板函数.好吧,代码优于1000字:
#include <type_traits>
template <typename... Ts>
typename std::enable_if<sizeof...(Ts) == 0>::type f() {
}
template <typename T, typename... Ts>
void f() {
// do something with T
f<Ts...>();
}
Run Code Online (Sandbox Code Playgroud)