I am building some library functions, one of which is basically a timer class with an arbitrary key. It looks conceptually like this:
template <typename Key>
class Timer
{
void tic(Key key) {tics[key] = std::clock();
void toc(Key key)
{
// calling this before tic has been called is fine
if (!tic.find(key))
tocs[key] = 0;
else
tocs[key] = std::clock() - tics[key];
// BUT: writing code that calls "tocs" without ever calling "tics" should trigger
// a compile-time error! How do I do this? Is it possible?
}
private:
std::map<Key,clock_t> tics;
std::map<Key,clock_t> tocs;
}
The task of this class is just to measure the time between tic
and toc
calls for each key. It should be perfectly legal to call toc
before tic
in order to allow the class to measure time between function calls, for example. However, calling a tic
or a toc
without some corresponding toc/tic in other parts of the code simply makes no sense, so it's obviously a coding error - which I would like to report at compile-time.
So, this should be ok:
Timer<int> timer;
while (1)
{
timer.toc(0);
// this reports the time elapses between the while
// loop ending and the while loop starting
timer.tic(1);
}
but these should generate compile-time errors (well, warnings would be more suitable):
Timer<int> timer;
while (1)
{
timer.toc(1); // this will always return 0
timer.tic(0); // this is an unused initialization
}
Is it possible to achieve this? I suspect the answer is no but I wanted to make sure, because it would be really neat.