vendredi 29 mai 2015

how to pass both function pointers and lambda's using one interface

I am trying to use function pointers and lambda's together using one interface. I decided to use std::function, but i quickly found out std::function cannot deal with overloaded functions by itself.

Example:

void foobar(double i_double ){std::cout << "double argument" << i_double << std::endl;}
void foobar(){std::cout << "no argument" << std::endl;}
void foobar(int){std::cout << "int argument" << std::endl;}

std::function<void(double)> func = static_cast<void(*)(double)>(&foobar);

Only by using a static_cast this code compiles.. Since at our company we have quite a lot of overloaded functions this is too much of a hassle. For now i decided to implement two interfaces.. one for std::function objects and another one for function pointers, although i would really like to just wrap the function pointers in a std::function object as well (without additional code on the calling side).

Any "nice" solution to this problem?

Aucun commentaire:

Enregistrer un commentaire