mercredi 8 avril 2020

Initialize an array of template structures

The following snippet of code simply creates a structure that has three members. One of them is a callback function. I would like to initialize an array of these structures but I don't know the syntax, where I can have multiple callbacks with varying prototypes.

#include <iostream>
#include <functional>

template <typename Func>
struct Foo
{
    Foo(int a, int b, Func func):
    _a{a},
    _b{b},
    _func{func}{}
   int _a;
   int _b;
  Func _func;
};

int main() {
    auto test = [](){std::cout << "hello\n";};
    Foo<std::function<void(void)>> foo(5,10, test);
    foo._func();

    //array version
    //......................
}

Aucun commentaire:

Enregistrer un commentaire