I'd like to create a task scheduler and first thing I have to do is collect tasks in a container. This is what I'm trying
#include <iostream>
#include <forward_list>
#include <future>
using namespace std;
template <typename R, typename ...ArgTypes>
struct TaskScheduler
{
forward_list<packaged_task<R(ArgTypes...)>> taskList_;
packaged_task<R(ArgTypes...)> * addTask(R fn, ArgTypes... args) {
taskList_.emplace_front(R(ArgTypes...));
return &taskList_.front();
}
void runEmAll(void) {
while (!taskList_.empty()) {
auto f = taskList_.front().get_future();
taskList_.front();
taskList_.pop_front();
}
}
};
int workExample1(std::string& s, int n) { return 0; }
int workExample2(std::string& s, int n) { return 1; }
int main()
{
TaskScheduler<int(), std::string&, int> ts;
std::string str("ABC");
ts.addTask(workExample1, str, 3);
ts.addTask(workExample2, str, 3);
ts.runEmAll();
}
Compiling this on Microsoft Visual Studio Community 2019 I get these errors
Build started...
1>------ Build started: Project: Packaged_task_test, Configuration: Debug Win32 ------
1>Packaged_task_test.cpp
1>E:\_Projekty\Packaged_task_test\Packaged_task_test.cpp(13,1): error C2091: function returns function
1>E:\_Projekty\Packaged_task_test\Packaged_task_test.cpp(32): message : see reference to class template instantiation 'TaskScheduler<int (void),std::string &,int>' being compiled
1>E:\_Projekty\Packaged_task_test\Packaged_task_test.cpp(14,1): error C2091: function returns function
1>E:\_Projekty\Packaged_task_test\Packaged_task_test.cpp(34,33): error C2664: 'std::packaged_task<R (__cdecl *(std::string &,int))> *TaskScheduler<R,std::string &,int>::addTask(R (__cdecl *),std::string &,int)': cannot convert argument 1 from 'int (__cdecl *)(std::string &,int)' to 'R (__cdecl *)'
1> with
1> [
1> R=int (void)
1> ]
1>E:\_Projekty\Packaged_task_test\Packaged_task_test.cpp(34,13): message : None of the functions with this name in scope match the target type
1>E:\_Projekty\Packaged_task_test\Packaged_task_test.cpp(14,34): message : see declaration of 'TaskScheduler<int (void),std::string &,int>::addTask'
1>E:\_Projekty\Packaged_task_test\Packaged_task_test.cpp(35,33): error C2664: 'std::packaged_task<R (__cdecl *(std::string &,int))> *TaskScheduler<R,std::string &,int>::addTask(R (__cdecl *),std::string &,int)': cannot convert argument 1 from 'int (__cdecl *)(std::string &,int)' to 'R (__cdecl *)'
1> with
1> [
1> R=int (void)
1> ]
1>E:\_Projekty\Packaged_task_test\Packaged_task_test.cpp(35,13): message : None of the functions with this name in scope match the target type
1>E:\_Projekty\Packaged_task_test\Packaged_task_test.cpp(14,34): message : see declaration of 'TaskScheduler<int (void),std::string &,int>::addTask'
1>Done building project "Packaged_task_test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
How to define taskList_ and addTask(...) and then how to call addTask(...)?
Many thanks in advance!