【问题标题】:For-loop with conditional call of a variadic amount of functions带有条件调用可变数量函数的 For 循环
【发布时间】:2021-12-22 03:50:14
【问题描述】:

我希望根据循环中的某些条件触发任意数量的函数,而不是执行递归调用。 如何转换 method0、method1 以调用可变参数 typename ... methods?

编辑: 由于您无法返回可变数量的类型,因此我稍微修改了示例。

template <typename method0, typename method1, typename ... Args>
void loop(Args ... args)
{
    for (int i = 0; i < iter_max; i++)
    {
        if (method0::condition(args))
        {
            method0::call_modify(args);
        }
        if (method1::condition(args))
        {
            method1::call_modify(args);
        }
        //...
    }
}

template<typename ... Args>
struct method0
{
    static bool condition(Args& ... args)
    {
        //Trigger condition
    }

    static void call_modify(Args& ... args)
    {
        //Perform action on args
    }
};

【问题讨论】:

  • methodX 是具有静态方法condition 和call 的类?
  • args = ... ?你真的想用第 n 个参数调用 methodN::call 吗?
  • 您应该提供method0 和method1 的外观。

标签: c++ templates variadic-templates non-type-template-parameter


【解决方案1】:

你可以这样做:

#include <iostream>

template<typename ... Args>
struct method0
{
    static bool condition(Args& ... args)
    {
        std::cout << "condition \n";
        return true;
    }

    static void call(Args& ... args)
    {
        std::cout << "call\n";
    }
};


template <typename... Methods, typename ... Args>
void loop(Args ... args)
{        
    auto apply = []<typename m>(auto&... a){
        if (m::condition(a...)) m::call(a...); 
    };
    (apply.template operator()<Methods>(args...), ...);
}

int main() {
    loop<method0<int,int>,method0<int,int>>(1,2);
}

Output:

condition 
call
condition 
call

也许loop 在这种情况下也应该引用。而且我省略了循环,添加它应该是微不足道的。


这是非可变condition 和call 的稍微简单的情况的答案的旧版本:

struct A {
    static bool condition(int x) { 
        std::cout << "A::condition " << x << "\n";
        return true;
    }
    static int call(int) { 
        std::cout << "A::call\n";
        return 42;
    }
};
struct B {
    static bool condition(int x) { 
        std::cout << "B::condition " << x << "\n";
        return false;
    }
    static int call(int) { 
        std::cout << "B::call\n";
        return 0;
    }
};

您可以像这样使loop 调用第n 个类型的condition 和call 和第n 个args:

template <typename... Methods, typename ... Args>
void loop(Args ... args)
{
    auto apply = []<typename m>(auto& a){
        if (m::condition(a)) a = m::call(a); 
    };
    (apply.template operator()<Methods>(args), ...);
}


int main() {
    loop<A,B>(1,2);
}

Output:

A::condition 1
A::call
B::condition 2

【讨论】:

  • 为了概括这一点以将所有 Args 传递给每个方法,我可以做类似(apply.template operator()&lt;Methods&gt;(args ...), ...); 的事情吗?
  • @JonasHjulstad 是的,类似的东西,并且 lambda 也必须是可变的。见编辑
【解决方案2】:

我设法概括了要循环的 463035818_is_not_a_number 的答案,并采用可变数量的输入参数。完整的工作示例:

#include <iostream>

struct method0
{
    static bool condition(double &arg1, double &arg2)
    {
        // Trigger condition
        return (arg1 <= arg2);
    }

    static void call_modify(double &&arg1, double &&arg2)
    {
        std::cout << "method0!" << std::endl;
        arg1 += 1;
    }
};

struct method1
{
    static bool condition(double &arg1, double &arg2)
    {
        // Trigger condition
        return (arg1 > arg2);
    }

    static void call_modify(double &&arg1, double &&arg2)
    {
        std::cout << "method1!" << std::endl;
        arg1 -= 1;
    }
};

template <int iter_max, typename... Methods>
struct wrapper
{
    template <typename... Args>
    static void loop(Args &...args)
    {
        auto apply = []<typename m>(auto&&... a)
        {
            if (m::condition(a...))
                m::call_modify(std::forward<Args>(a)...);
        };
        for (int i = 0; i < iter_max; i++)
        {
            (apply.template operator()<Methods>(std::forward<Args>(args)...), ...);
        }
    }
};

int main()
{
    double arg1 = 2.0;
    double arg2 = 0.;
    constexpr int iter_max = 5;
    wrapper<iter_max, method0, method1>::loop<double, double>(arg1, arg2);
}

输出:

method1!
method1!
method0!
method1!
method0!
method1!
method0!
method1!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多