mercredi 13 novembre 2019

Best practice for callable function argument

Every time I write a signature that accepts a templated callable, I always wonder what the best type for the parameter is. Should it be a value type or a const reference type?

For example,

template <class Func>
void execute_func(Func func) {
    /* ... */
}

// vs.

template <class Func>
void execute_func(const Func& func) {
    /* ... */
}

Is there any situation where the callable isn't 64bits (aka a pointer to func)?

A C function pointer is 64bits. A lambda without any capture is a function pointer. A lambda with capture (closure) is still the address of its operator()() const.

Maybe std::function behaves differently?

Aucun commentaire:

Enregistrer un commentaire