vendredi 22 mai 2020

std::bind and std::function term does not evalue as taking 0 arguments?

I'm working on generic class that will be used to run functions of different prototype for performance testing of algorithms.

I stuck because std::function can't execute what it was bound with, here is sample code, with comment where the error happens:

#include <utility>
#include <functional>

template<typename ReturnType>
class Performance
{
public:
    template<typename... Args>
    using Algorithm = std::function<ReturnType(Args...)>;

    template<typename... Params>
    void run(const Algorithm<Params...>& ref, const Algorithm<Params...>& target)
    {
        // ERROR: term does not evaluate as taking 0 args
        ref();
        target();
    }
};

void bar1(int, int)
{
    // algorithm 1
}

void bar2(int, int)
{
    // algorithm 2
}

int main()
{
    using test = Performance<void>;
    using Algorithm = test::Algorithm<int, int>;

    int x = 0;
    int y = 1;

    Algorithm ref = std::bind(bar1, std::ref(x), std::ref(y));
    Algorithm target = std::bind(bar2, std::ref(x), std::ref(y));

    test foobar;

    foobar.run(ref, target);
}

Aucun commentaire:

Enregistrer un commentaire