#include <string>
#include <iostream>
#include <functional>
using namespace std::placeholders;
struct overloaded
{
int operator()(const std::string& s) const { return 1; }
int operator()(double d) const { return 2; }
};
template <typename T>
auto makefoo(const T& t) -> decltype(std::bind(overloaded(), t))
{
return std::bind(overloaded(), t);
}
int main()
{
overloaded foo;
// based on local bind expression:
auto unresolved = std::bind(foo, _1);
std::cout << unresolved(3.14) << std::endl; // should print 2
std::cout << unresolved("3.14") << std::endl; // should print 1
// based on a factory function template
std::cout << makefoo(3.14)() << std::endl; // should print 2
std::cout << makefoo("3.14")() << std::endl; // should print 1
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ6OnBsYWNlaG9sZGVyczsKCnN0cnVjdCBvdmVybG9hZGVkCnsKICAgIGludCBvcGVyYXRvcigpKGNvbnN0IHN0ZDo6c3RyaW5nJiBzKSBjb25zdCB7IHJldHVybiAxOyB9CiAgICBpbnQgb3BlcmF0b3IoKShkb3VibGUgZCkgICAgICAgICAgICAgY29uc3QgeyByZXR1cm4gMjsgfQp9OwoKdGVtcGxhdGUgPHR5cGVuYW1lIFQ+CmF1dG8gbWFrZWZvbyhjb25zdCBUJiB0KSAtPiBkZWNsdHlwZShzdGQ6OmJpbmQob3ZlcmxvYWRlZCgpLCB0KSkKewogICAgcmV0dXJuIHN0ZDo6YmluZChvdmVybG9hZGVkKCksIHQpOwp9CgppbnQgbWFpbigpCnsKICAgIG92ZXJsb2FkZWQgZm9vOwoKICAgIC8vIGJhc2VkIG9uIGxvY2FsIGJpbmQgZXhwcmVzc2lvbjoKICAgIGF1dG8gdW5yZXNvbHZlZCA9IHN0ZDo6YmluZChmb28sIF8xKTsKICAgIHN0ZDo6Y291dCA8PCB1bnJlc29sdmVkKDMuMTQpICAgPDwgc3RkOjplbmRsOyAgLy8gc2hvdWxkIHByaW50IDIKICAgIHN0ZDo6Y291dCA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