mercredi 2 novembre 2016

functions as template arguments

I have a TaskWrapper:

template <typename T, T (*F)(T)>
struct TaskWrapper {
  static inline T run(T clock) {
    return F(clock);
  }
};

to use it I have to specify the T template argument:

uint16_t task1(uint16_t clock) { return clock + 1; }
typedef tasks::TaskWrapper<uint16_t, task1> Task;

I would like to simply write:

typedef tasks::TaskWrapper<task1> Task;

and let the compiler figure out that the return and argument type is uint16_t.

Notes:

The TaskWrapper is obviously simplified and in reality has a few other arguments, which are passed during the typedef.

The wrapped functions can only be:

uint8_t task(uint8_t clock);
uint16_t task(uint16_t clock); // or
uint32_t task(uint32_t clock);

The TaskWrapper is passed as template argument to another class, which will at some point call Task::run(...);

C++11 would be fine.

Aucun commentaire:

Enregistrer un commentaire