c++ - Why is this call of overloaded function ambiguous? -


why constructor call ambiguous?

#include <functional>  class {     std::function<int(void)> f_;     std::function<float(void)> g_; public:     a(std::function<int(void)> f) { f_ = f; }     a(std::function<float(void)> g) { g_ = g; }     ~a() {} };  int main() {     a([](){ return (int)1; });     return 0; } 

note typecast.

is there way tell compiler constructor overload use?

it's defect in standard. see dr 2132:

consider following:

#include <functional>  void f(std::function<void()>) {} void f(std::function<void(int)>) {}  int main() {   f([]{});   f([](int){}); } 

the calls f in main ambiguous. apparently because conversion sequences std::function lambdas identical. standard specifies function object given std::function "shall callable (20.8.11.2) argument types argtypes , return type r." doesn't if not case, constructor isn't part of overload set.

try using function pointer argument instead:

a(int f()) { f_ = f; } a(float g()) { g_ = g; } 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -