I wish to have a function_list<> template class that holds a vector of std::function<> values. A key point is that I want to declare my list with the same form I would declare a single std::function<>. e.g.
function_list<void()> functions; // list of std::functions that take and return void
Below is my attempt to implement this. I tried to follow the same syntax used by the std::function<> template in the functional header file of the standard library.
#include <functional>
#include <vector>
#include <iostream>
template<class _R, class... _Args>
class function_list<_R(_Args...)>
{
public:
std::vector<std::function<_R(_Args...)>> functions;
};
int main()
{
function_list<void()> fl;
fl.functions.push_back([]() { std::cout << "Hello, World!" << std::endl; });
fl.functions.push_back([]() { std::cout << "Goodbye, Cruel World!" << std::endl; });
for (auto& f : fl.functions)
f();
return 0;
}
This doesn't compile and fails with the following errors (clang on OSX 10.14):
test.cpp:6:7: error: explicit specialization of non-template class 'function_list'
class function_list<_R(_Args...)>
^ ~~~~~~~~~~~~~~
test.cpp:9:28: error: function cannot return function type 'void ()'
std::vector<std::function<_R(_Args...)>> functions;
^
test.cpp:14:24: note: in instantiation of template class 'function_list<void ()>' requested here
function_list<void()> fl;
^
2 errors generated.
What am I doing wrong? Thanks for your help.
Aucun commentaire:
Enregistrer un commentaire