c++ - no viable overloaded '=' for overloaded static member functions -
c++ - no viable overloaded '=' for overloaded static member functions -
i have simplified code consisting of class static function, stored in map:
#include <iostream> #include <functional> #include <map> class { public: static void f(const std::string &s) { std::cout << s; } }; std::map<std::string, std::function<void(std::string const &)>> fs; int main() { fs["f"] = &a::f; fs["f"]("hello"); }
this prints expected hello.
the problem occurs if overload f() with:
static void f(const std::string &s, int c) { while(c-->0) { std::cout << s; } }
this results in error:
error: no viable overloaded '=' fs["f"] = &a::f; ~~~~~~~ ^ ~~~~~ /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2241:7: note: candidate function not viable: no overload of 'f' matching 'const std::function<void (const std::basic_string<char> &)>' 1st argument operator=(const function& __x) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2259:7: note: candidate function not viable: no overload of 'f' matching 'std::function<void (const std::basic_string<char> &)>' 1st argument operator=(function&& __x) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2273:7: note: candidate function not viable: no overload of 'f' matching 'nullptr_t' 1st argument operator=(nullptr_t) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2302:2: note: candidate template ignored: couldn't infer template argument '_functor' operator=(_functor&& __f) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/functional:2311:2: note: candidate template ignored: couldn't infer template argument '_functor' operator=(reference_wrapper<_functor> __f) noexcept ^
however, calling both functions works:
a::f("hello "); a::f("echo ", 3);
so, question are:
why code not compiling though operator= seems exist , function if don't overload f()? how can work without giving both functions different names?
why code not compiling though operator= seems exist , function if don't overload f()?
because compiler doesn't know overload choose. how he? there no criterion upon can decide 1 suited better. every std::function
allows arbitrary function objects assigned , doesn't check any signatures. if wanted save function pointers of particular signature should have declared map
appropriately.
how can work without giving both functions different names?
as mentioned works casting look function pointer of specific type.
fs["f"] = static_cast<void(*)(std::string const&)>( &a::f );
this way no ambiguities arise; there 1 overload can casted function pointer type. if appears more typedef feasible.
or little helper class template:
template <typename... exact> struct funptr { template <typename r> constexpr auto operator()(r(*p)(exact...)) -> decltype(p) { homecoming p; } }; fs["f"] = funptr<std::string const&>()(&a::f);
demo.
c++ static assignment-operator std-function
Comments
Post a Comment