是否有比这个更优雅的解决方案(对于一个显而易见的问题)
switch(variableInt)
{
case 0: tmpl<0>::foo(); break;
case 1: tmpl<1>::foo(); break;
// up to, say, 10
}
显然,只能在某个范围内定义。我只发现了与此相关的旧主题,也许是一些不错的C ++ 17或C ++ 20功能可以在这里帮助我们?
类似的东西呢?
#include <utility>
template <std::size_t I>
int foo(int arg) {
return I * arg;
}
template<std::size_t ...Is>
auto get_foo(std::index_sequence<Is...>, int i) {
return *(std::initializer_list<int(*)(int)>{&foo<Is>...}.begin() + i);
}
int f(std::size_t i, int arg) {
return get_foo(std::make_index_sequence<10>(), i)(arg);
}