您可以使用类似于this 的Boost MP11 将合并两个列表 合并为一个笛卡尔积,如下所示
#include <boost/mp11.hpp>
template <template <typename...> typename... F>
using mp_list_q = boost::mp11::mp_list<boost::mp11::mp_quote<F>...>;
using outer_types = mp_list_q<std::vector, std::set>;
using inner_types = boost::mp11::mp_list<int, double>;
using TypeList = boost::mp11::mp_product<boost::mp11::mp_invoke_q,
outer_types,
inner_types>;
在这个例子中TypeList corresponds to:
boost::mp11::mp_list<std::vector<int>, std::vector<double>, std::set<int>, std::set<double>>
这可以用于类型化单元测试,如下所示:
BOOST_TEST_CASE_TEMPLATE_FUNCTION(foo_test, T) {
BOOST_TEST(ns::foo(T{}) == true);
}
boost::unit_test::test_suite* init_unit_test_suite(int, char* []) {
boost::unit_test::framework::master_test_suite().add(BOOST_TEST_CASE_TEMPLATE(foo_test, TypeList));
return EXIT_SUCCESS;
}
Try it here!
如果由于某种原因您不能使用 Boost MP11,您可以使用可变参数模板为自己编写一个手动解决方案,以创建类似于 the one I have written here 的别名 TypeList。