2

In C++ primer 5th edition p 578 it is said that

We cannot store the name of an overloaded function in an object of type function

The provided code is :

int add (int i, int j) {return i+j;}
Sales_data add (const Sales_data&, const Sales_data&);
map <string, function <int(int, int)>>binops;
binops.insert ({"+", add}); // error : which add ?

What I do not understand is why the compiler cannot tell which add function to insert into the given map : the add which takes Sales_data& as argument does not match the function signature the map was created with and I would expect the compiler to be able to tell that the function to insert into the map is int add (int i, intj) {return i+j;} Why does the compiler not know it ?

user
  • 934
  • 6
  • 17
  • Good question, I'd love to know the answer to this as well. I'd guess it's because it can't infer the type of the function pointer int(add*)(int,int) from function . – ZeroUltimax Mar 06 '17 at 19:48

0 Answers0