c++ - function overloading with std::function and generic lambdas: std::string preferred over int -
when trying compiling this, suprisingly, gives error because auto
parameter of lambda function has been resolved std::string
, , compiler doesn't know how convert std::string
int
or widget
when calling test
.
but, wonder why compiler has choosen second invok
function instead of first one, when first 1 succeed:
#include <string> #include <functional> struct widget {}; bool test(int ); bool test(widget ); void invok(std::function<bool(int)> ); // #1 void invok(std::function<bool(std::string)> ); // #2 int main() { // error: unresolved overloaded function type // invok(test); // still error: no known conversion std::string // int or widget invok([](auto&& x) { return test(std::forward<decltype(x)>(x)); }); }
that example has been copied a c++ proposal.
the compiler didn't choose #2. it's trying decide if can choose #2.
to that, asks "can generic lambda converted std::function<bool(std::string)>
"?
std::function
's converting constructor says "only if it's callable std::string
rvalue , result type convertible bool
".
compiler tries that, deduce auto
std::string
, substitute signature of function call operator...success! oops, return type auto
, , needs actual type answer "is convertible" question. instantiates body of function call operator template figure out return type.
ouch. body isn't valid std::string
after all. hard error , explosions follow.
Comments
Post a Comment