Tuesday, August 29, 2017

C++11: Bind: Overloading

When you are binding with an overloaded function, you need to specify the type of the function. Here is an example:

#include <functional>
typedef int (F)(int);

int    f(int    i) { return 0  ; }
double f(double d) { return 0.0; }

   F    * f1 =              f;
   F    * f2 = (int(*)(int))f;
   F    * f3 = (F   *      )f;
// auto   f4 =              f; // Does not compile
   auto   f5 = (int(*)(int))f;
   auto   f6 = (F   *      )f;

// F    * g1 = std::bind(             f, std::placeholders::_1); // Does not compile
// F    * g2 = std::bind((int(*)(int))f, std::placeholders::_1); // Does not compile
// F    * g3 = std::bind((F   *      )f, std::placeholders::_1); // Does not compile
// auto   g4 = std::bind(             f, std::placeholders::_1); // Does not compile
   auto   g5 = std::bind((int(*)(int))f, std::placeholders::_1);
   auto   g6 = std::bind((F   *      )f, std::placeholders::_1);

int main()
{
    return 0;
}
Reference: https://isocpp.org/wiki/faq/cpp11-library#std-function

No comments:

Post a Comment