lundi 13 janvier 2020

Varidic template member function to init varidic template object

I can't understand how to fix linker error with this code organization:


/* -------  super_clas.hpp  ------- */
#include "variadic_class.hpp"
template <typename T>
class SuperClass
{
   private:
        std:vector<T> input;
   public:
         template <typename F, typename ...Args>
         void variadic_function(std::function<F(Args...)> f);
   ...
}

template<typename T>
template<typename F, typename ...Args>
void SuperClass<T>::variadic_function(std::function<F(Args...)> f)
{
    VariadicClass<T,F,Args...> object(f);
    object.exec(input);
}


/* -------  variadic_class.hpp  ------- */
template <typename T, typename F,typename ...Args>
class VariadicClass {
    private:
        std::function<F(Args...)> f;
    public:
        VariadicClass(std::function<F(Args...)> f);
};

template <typename T, typename F,typename ...Args>
Worker<T,F,Args...>::Worker(std::function<F(Args...)> fun) : f(fun)
{}

template <typename T, typename F,typename ...Args>
F VariadicClass<T,F,Args...>::exec(std::vector<T> data)
{
    return f(data);
}

I want to pass whatever std::function with the first parameter an std::vector to SuperClass, generate an instance of VariadicClass and exec. For example:

std::function<std::vector<int>(std::vector<int>)> function1 = [](std::vector<int> data)
{
  //do something with data
}

std::function<std::vector<int>(std::vector<int>, int, int)> function2 = [](std::vector<int> data,int a, int b)
{
 //do something with data, a and b
}

SuperClass<int> s();
s.variadic_function(function1);
s.variadic_function(function2);

Linker can't find implementation of VariadicClass::variadic_function. I undestand I'm using variadic template in wrong way but is there a way to modifiy correctly VariadicClass to do what I want? Thanks in advance

Aucun commentaire:

Enregistrer un commentaire