c++ - boost 单元测试 mpl 列表的笛卡尔积

标签 c++ templates boost

假设我有两个 mpl 列表,例如

typedef boost::mpl::list<int, double> inner_types;
typedef boost::mpl::list<std::vector, std::set> outer_types;

我希望能够在 boost 单元测试中迭代这些列表的笛卡尔积并从每个组合构造一个对象......如下所示:

BOOST_TEST_CASE_TEMPLATE(mytest, InnerType, inner_types, OuterType, outer_types) {
    OuterType<InnerType> obj;

    BOOST_CHECK(ns::foo(obj));
}

但在使用 _TEMPLATE 作为 boost 测试用例时,您似乎只能有一个 mpl 列表。有办法实现我的目的吗?

最佳答案

您可以使用类似于thisBoost 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 .

关于c++ - boost 单元测试 mpl 列表的笛卡尔积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67894815/

相关文章:

c++ - 在 VS2005 中为我的 C++ 应用程序设置管理员权限

c++ - OpenMP 虚假共享

c++ - 这个 A<B>::c ("d") 结构是什么意思?用模板命名空间?

c++ - 如何在可变参数模板中获取嵌套参数包?

c++ - 如何 boost::bind 以通用引用作为参数的模板成员函数

c++ - 编译错误 - boost::numeric::ublas::coordinate_matrix

c++ - 这如何解决访问冲突问题?

c++ - getIntegerv() 和 std::cout 的奇怪行为

c++ - Boost的lexical_cast从double到string的精度

c++ - 使用 cygwin : can't find configure file 安装 C++ boost