【问题标题】:Avoid nested for-loops when searching parameter space搜索参数空间时避免嵌套 for 循环
【发布时间】:2014-01-17 13:12:24
【问题描述】:

在编写单元测试时,我经常想调用带有参数组合的函数。例如,我有一个函数声明为

void tester_func(int p1, double p2, std::string const& p3);

以及一些选定的参数

std::vector<int> vec_p1 = { 1, 2, 666 };
std::vector<double> vec_p2 = { 3.14159, 0.0001 };
std::vector<std::string> vec_p3 = { "Method_Smart", "Method_Silly" };

我现在做的只是

for(auto const& p1 : vec_p1)
    for(auto const& p2 : vec_p2)
        for(auto const& p3 : vec_p3)
            tester_func(p1, p2, p3);

但是,Sean Parent 建议避免显式循环并改用std:: 算法。在上述情况下,如何遵循这一建议?有什么成语吗?编写可变参数模板的最简洁方法是什么? 没有 C++11 功能的最佳方法是什么?

【问题讨论】:

  • 答案可能取决于您使用的单元测试框架。
  • 提升单元测试。 tester_func 自动记录所有错误。唯一重要的是为所有参数组合调用该函数。
  • 在您链接到的视频中,他明确指出,只要循环体是单个语句/赋值,有时(节省打字)使用范围 for 循环是可以的。我觉得你在这里做的没问题。
  • C++ 标准库似乎缺少一种算法,该算法可以生成/迭代多个容器的笛卡尔积。这种算法的实现可以在 stackoverlow 上找到,例如stackoverflow.com/a/13841673/2128694
  • 查看 AWESOME cppitertools 软件包中的产品模板:github.com/ryanhaining/cppitertools

标签: c++ c++11 for-loop idioms


【解决方案1】:

@Oberon 在 cmets 中提供了对非常好的解决方案的参考。

但我认为这个问题有很多不同的解决方案。这是我的解决方案:

#include <tuple>
#include <type_traits>

template <class TestFunction, class... Containers, class... Types>
typename std::enable_if<sizeof...(Containers) == sizeof...(Types)>::type
TestNextLevel
    (
        TestFunction testFunction,
        const std::tuple<Containers...>& containersTuple,
        const Types&... parameters
    )
{
    testFunction(parameters...);
}

template <class TestFunction, class... Containers, class... Types>
typename std::enable_if<(sizeof...(Containers) > sizeof...(Types))>::type
TestNextLevel
    (
        TestFunction testFunction,
        const std::tuple<Containers...>& containersTuple,
        const Types&... parameters
    )
{
    for (const auto& element : std::get<sizeof...(Types)>(containersTuple))
    {
        TestNextLevel(testFunction, containersTuple, parameters..., element);
    }
}

template <class TestFunction, class... Containers>
void TestAllCases
    (
        TestFunction testFunction,
        const Containers&... containers
    )
{
    TestNextLevel
        (
            testFunction,
            std::tuple<const Containers&...>(containers...)
        );
}

使用示例:

TestAllCases(tester_func, vec_p1, vec_p2, vec_p3);

【讨论】:

  • 很明显,这比原代码更容易理解 :)
猜你喜欢
  • 2012-06-25
  • 2017-08-27
  • 2017-11-11
  • 2020-05-21
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 2015-01-24
相关资源
最近更新 更多