如题
#include <iostream>
#include <functional>
using namespace std;
void g(ostream &os)
{
os << "1";
}
int main()
{
//非法
auto f = bind(g, &cout);
f();
//合法
auto h = bind(g, ref(cout));
h();
return 0;
}
天蓬老师2017-04-17 14:26:58
関数プロトタイプはこのように規定されているため、&cout
と記述するとポインタになりますが、これを std::ref
で囲むとプロトタイプの要件を満たします。参照を渡し、不変の std::cref
もあります。パラメータ型の公式要件は次のとおりです:
最後の行を見てください。リクエストは reference_wrapper
でなければならないと書かれています。それが std::ref
と std::cref
の処理です