variadic templates for timers

For the timer system I figured you would want to define a Timer object that would hold the delay, function pointer, and the arguments to be passed. But of course with n possible arguments to make it more useful.
 
"C3520" 'Args': parameter pack must be expanded in this context

is the compile error I get for the Timer's [Args arg] variable is the closest I have gotten to 'working' over the past couple of hours. Does anybody have advice on how to work this out?


Timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
template<typename ... Args>
class Timer
{
private:

    float delay;
    void (*func)(Args ...);
    Args args; //error

public:

    Timer( float delay = 0, void (*func)(const Args ... args) = NULL, const Args ... args = NULL )
    {
        this-> delay = delay;
        this-> func = func;
		this->args = args;
        this-> tempClock = tempClock;
    };

	static Timer Simple( float delay, void (*func)(const Args ... args), const Args ... args )
	{
		Timer object( delay, func, args );
		Timers.push_back(object);
	};

	static void Update();
};


Timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "timer.h"

std::vector<Timer<int>> Timers;

void Timer<int>::Update()
{
	for (unsigned short i = 1; i < Timers.size() ; i++)
	{
		if ( Timers[i].delay*1000 < Timers[i].tempClock.getElapsedTime().asMilliseconds() )
		{
			Timers[i].func(Timers[i].args);
			Timers.erase(Timers.begin() + i);
		}
	}		
};


#usage#
1
2
3
4
5
6
7
8

void PrintTest(int num, const char *string)
{
	std::cout << "there are " << num << " " << string << std::endl;
}

Timer<int, const char*> a = Timer<int, const char*>::Simple(1.0f, PrintTest, 1, "duck(s)");
Last edited on
Topic archived. No new replies allowed.