Home > Net >  Pass templated parameter pack to callback function (also a templated function)
Pass templated parameter pack to callback function (also a templated function)

Time:09-17

Currently in my code I have a function, that takes various arguments, and a callback object (which is instance of a simple class with a Callback(void) function). This works perfectly fine now, but it clutters code because I have quite a few of these classes, and in most of them their variables are used only by the Callback() function.

I would like to replace the classes with simple function like so:

#include <iostream>
#include <string>

template<class T>
class TestClass {
public:
    template<typename ...TArgs>
    using CallbackFuncType = bool(T &Data, TArgs && ...Args);

    TestClass(const T &Data) : data(Data) {}

    template<typename ...TArgs>
    bool Function(
        CallbackFuncType<TArgs...> Callback,
        TArgs && ...Args
    )
    {
        return Callback(data,
            std::forward<TArgs>(Args)...);
    }

private:
    const T data;
};

struct DataType {
    int valInt;
    double valFloat;

    std::string ToString()
    {
        return std::string(
            "valInt: "   std::to_string(valInt)  
            "valFloat: "   std::to_string(valFloat)
        );
    }
};

bool CBFunction1(DataType &Data, int Arg0, const std::string &Arg1)
{
    std::cout << "Data :" << Data.ToString() << std::endl;

    std::cout << "CBFunction1: " <<
        "Arg0 : " << Arg0 <<
        "Arg1: " << Arg1 <<
        std::endl;

    return true;
}

bool CBFunction2(DataType &Data, int Arg0, double Arg1, const std::string &Arg2)
{
    std::cout << "Data :" << Data.ToString() << std::endl;

    std::cout << "CBFunction2: " <<
        "Arg0 : " << Arg0 <<
        "Arg1: " << Arg0 <<
        "Arg2: " << Arg2 <<
        std::endl;

    return true;
}

int main()
{
    DataType data;
    data.valInt = 1;
    data.valFloat = 1.2;

    TestClass<DataType> testObj(data);

    std::cout << "test" << std::endl;

    std::cout << "call CBFunction1" << std::endl;
    bool val1 = testObj.Function(CBFunction1, 11, "test_string");
    bool val2 = testObj.Function(CBFunction2, 11, 111.111, "test_string");

    return 0;
}

Upon compilation I get the following errors:

1>main.cpp
1>main.cpp(75,24): error C2672: 'TestClass<DataType>::Function': no matching overloaded function found
1>main.cpp(75,64): error C2784: 'bool TestClass<DataType>::Function(bool (__cdecl *)(T &,TArgs &&...),TArgs &&...)': could not deduce template argument for 'bool (__cdecl *)(T &,TArgs &&...)' from 'bool (DataType &,int,const std::string &)'
1>        with
1>        [
1>            T=DataType
1>        ]
1>main.cpp(13): message : see declaration of 'TestClass<DataType>::Function'
1>main.cpp(76,24): error C2672: 'TestClass<DataType>::Function': no matching overloaded function found
1>main.cpp(76,73): error C2784: 'bool TestClass<DataType>::Function(bool (__cdecl *)(T &,TArgs &&...),TArgs &&...)': could not deduce template argument for 'bool (__cdecl *)(T &,TArgs &&...)' from 'bool (DataType &,int,double,const std::string &)'
1>        with
1>        [
1>            T=DataType
1>        ]
1>main.cpp(13): message : see declaration of 'TestClass<DataType>::Function'

I tried to add type_identity_t to Args, but had no success.

CodePudding user response:

template <typename... TArgs>
bool Function(CallbackFuncType<TArgs...> Callback, TArgs&&... Args)

Have several issues, the main one is that TArgs should be deduced exactly identically in both place.

Issue with

template <typename... TArgs>
using CallbackFuncType = bool(T& Data, TArgs&&... Args);

is that it can only match function with argument which are (l or r value) reference.

So something like

template <typename... TArgs>
using CallbackFuncType = bool(T&, TArgs...);

template <typename... TArgs, typename... Ts>
bool Function(CallbackFuncType<TArgs...>* Callback, Ts&& ...Args)
{
    return Callback(data, std::forward<Ts>(Args)...);
}

would fix the issue, but even simpler (and more generic)

template <typename Func, typename... Ts>
bool Function(Func Callback, Ts&& ...Args)
{
    return Callback(data, std::forward<Ts>(Args)...);
}

Demo

  • Related