如何对给定的一组模板模板 类 进行编译时递归?
How to do compile-time recursion over a given set of template template classes?
目标: 对给定的 template template classes 集合进行编译时递归以计算给定函数。
问题: 我无法设计 template
参数对于 run_tests
函数应该是什么样子。
MWE: 请考虑以下代码示例 working 对于一组给定的 内置类型 (此示例基于 Whosebug 中的一个答案,到目前为止我找不到它 - 我会在找到它时添加 link):
#include <tuple>
#include <type_traits>
#include <iostream>
#include <vector>
template<class Type>
void test(Type)
{
// Do nothing
std::cout<< "I survived 2020" << std::endl;
}
// Do compile-time recursion over the given types
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type
run_tests
(
const std::tuple<Tp...>& types
)
{}
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), void>::type
run_tests
(
const std::tuple<Tp...>& types
)
{
test(std::get<I>(types));
run_tests<I + 1, Tp...>(types);
}
int main(int argc, char *argv[])
{
const std::tuple<int, double> types // working
// const std::tuple // not working
// <
// std::vector<std::vector<int>>,
// std::vector<std::vector<double>>
// > types
(
std::make_tuple(nullptr, nullptr)
);
run_tests(types);
return 0;
}
尝试次数: 我已经注释掉了上面的注释代码,即:
// const std::tuple<int, double> types // working
const std::tuple // not working
<
std::vector<std::vector<int>>,
std::vector<std::vector<double>>
> types
上面抛出了以下第一个编译错误:
error: no matching function for call to ‘std::tuple<std::vector<std::vector<int,
std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >,
std::vector<std::vector<double, std::allocator<double> >,
std::allocator<std::vector<double, std::allocator<double> > > >
>::tuple(std::tuple<std::nullptr_t, std::nullptr_t>)’
);
您的问题与元组的初始化有关,nullptr
对 std::vector
无效。
您可以使用默认初始化:
const std::tuple<int, double> type1{};
const std::tuple<std::vector<std::vector<int>>,
std::vector<std::vector<double>>> type2{};
目标: 对给定的 template template classes 集合进行编译时递归以计算给定函数。
问题: 我无法设计 template
参数对于 run_tests
函数应该是什么样子。
MWE: 请考虑以下代码示例 working 对于一组给定的 内置类型 (此示例基于 Whosebug 中的一个答案,到目前为止我找不到它 - 我会在找到它时添加 link):
#include <tuple>
#include <type_traits>
#include <iostream>
#include <vector>
template<class Type>
void test(Type)
{
// Do nothing
std::cout<< "I survived 2020" << std::endl;
}
// Do compile-time recursion over the given types
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type
run_tests
(
const std::tuple<Tp...>& types
)
{}
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), void>::type
run_tests
(
const std::tuple<Tp...>& types
)
{
test(std::get<I>(types));
run_tests<I + 1, Tp...>(types);
}
int main(int argc, char *argv[])
{
const std::tuple<int, double> types // working
// const std::tuple // not working
// <
// std::vector<std::vector<int>>,
// std::vector<std::vector<double>>
// > types
(
std::make_tuple(nullptr, nullptr)
);
run_tests(types);
return 0;
}
尝试次数: 我已经注释掉了上面的注释代码,即:
// const std::tuple<int, double> types // working
const std::tuple // not working
<
std::vector<std::vector<int>>,
std::vector<std::vector<double>>
> types
上面抛出了以下第一个编译错误:
error: no matching function for call to ‘std::tuple<std::vector<std::vector<int,
std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >,
std::vector<std::vector<double, std::allocator<double> >,
std::allocator<std::vector<double, std::allocator<double> > > >
>::tuple(std::tuple<std::nullptr_t, std::nullptr_t>)’
);
您的问题与元组的初始化有关,nullptr
对 std::vector
无效。
您可以使用默认初始化:
const std::tuple<int, double> type1{};
const std::tuple<std::vector<std::vector<int>>,
std::vector<std::vector<double>>> type2{};