0
// this compiles
mfunc<int, float> ok(some_func);

// this does not ???
mfunc<int, float>* no = make_mfunc(some_func);

Error C2672 'make_mfunc': no matching overloaded function found

Error C2784 'mfunc *make_mfunc(std::function &&)': could not deduce template argument for 'std::function &&' from 'void (int,float)'

Error C2784 'mfunc *make_mfunc(const std::function &)': could not deduce template argument for 'const std::function &' from 'void (int,float)'

// code
struct ifunc
{
};

template <typename ...Args>
struct mfunc : public ifunc
{
    using callback_type = std::function<void(Args...)>;

    mfunc(const std::function<void(Args...)>& func) : func(func) { }
    mfunc(std::function<void(Args...)>&& func) : func(std::move(func)) { }

    std::function<void(Args...)> func;
};

template <typename ...Args>
mfunc<Args...>* make_mfunc(const std::function<void(Args...)>& func)
{
    return new mfunc<Args...>(func);
}

template <typename ...Args>
mfunc<Args...>* make_mfunc(std::function<void(Args...)>&& func)
{
    return new mfunc<Args...>(std::move(func));
}

void some_func(int a, float b)
{
    printf("%d, %f", a, b);
}

0 Answers0