を使用してオーバーロードされた関数にパラメーターをバインドする方法がわかりませんstd::bind
。どういうわけかstd::bind
、オーバーロードされた型を推測できません (そのテンプレート パラメーターの場合)。関数をオーバーロードしなければ、すべてが機能します。以下のコード:
#include <iostream>
#include <functional>
#include <cmath>
using namespace std;
using namespace std::placeholders;
double f(double x)
{
return x;
}
// std::bind works if this overloaded is commented out
float f(float x)
{
return x;
}
// want to bind to `f(2)`, for the double(double) version
int main()
{
// none of the lines below compile:
// auto f_binder = std::bind(f, static_cast<double>(2));
// auto f_binder = bind((std::function<double(double)>)f, \
// static_cast<double>(2));
// auto f_binder = bind<std::function<double(double)>>(f, \
// static_cast<double>(2));
// auto f_binder = bind<std::function<double(double)>>\
// ((std::function<double(double)>)f,\
// static_cast<double>(2));
// cout << f_binder() << endl; // should output 2
}
私の理解では、オーバーロードされているstd::bind
ため、テンプレートパラメーターを何らかの形で推測することはできませんf
が、それらを指定する方法がわかりません。コード(コメント行)で4つの可能な方法を試しましたが、どれも機能しません。の関数のタイプを指定するにはどうすればよいstd::bind
ですか? どんな助けでも大歓迎です!