c++11 - how to pass both function pointers and lambda's using one interface -


i trying use function pointers , lambdas using 1 interface. decided use std::function, found out cannot deal overloaded functions 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); 

this code compiles using static_cast. since @ our company have quite lot of overloaded functions of hassle. decided implement 2 interfaces: 1 std::function objects , 1 function pointers, although wrap function pointers in std::function object (without additional code on calling side).

any "nice" solution problem?

edit: way using part of large framework, boils down code below. hope following code below makes use case clear.

namespace {     bool isallowed(string) {...}     bool isallowed(int) {...} }  citeratorfilter<string>( listofstrings, isallowed );  csomeobject object; citeratorfilter<string>( listofstrings,      [&object](string i_string ) { return object.isallowed( i_string ); }  ); 

currently citeratorfilter needs implement both function pointer , std::function interface, not nice in opinion.

see... xy problem.

this or similar not needed:

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

you need use templates:

template <class func> citeratorfilter<string>( listofstrings, func func) {   // call func whatever may function pointer, lambda, callable object:   func(..whatever params...); } 

you can use perfect fowarding func if want


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -