std::bind
と一緒に使用するstd::protect
がない理由は、にあります。C++ 11?std :: protectがないのはなぜですか?
Boost.Bindはboost::bind
がそれを認識し、評価しないように、その引数をラップboost::protect
ヘルパーを提供します。 std::[c]ref
は、ほとんどの場合、十分に置換えられますが、の値のを引数に取ることはありません。私はそれが実装されなかった理由を認識していないよ、まあ
#include <type_traits>
#include <functional>
int add(int a, int b)
{ return a + b; }
struct invoke_with_42
{
template <typename FunObj>
auto operator()(FunObj&& fun_obj) const -> decltype((fun_obj(42)))
{ return fun_obj(42); }
};
int main()
{
//// Nested bind expression evaluated
//auto bind_expr =
// std::bind<int>(invoke_with_42{}
// , std::bind(&add, 1, std::placeholders::_1));
//// Compilation error, cref does not take rvalues
//auto bind_expr =
// std::bind<int>(invoke_with_42{}
// , std::cref(std::bind(&add, 1, std::placeholders::_1)));
//// Ok, inner_bind_expr must be kept alive
auto inner_bind_expr =
std::bind(&add, 1, std::placeholders::_1);
auto outer_bind_expr =
std::bind<int>(invoke_with_42{}, std::cref(inner_bind_expr));
//// Ok, with protect
//auto bind_expr =
// std::bind<int>(invoke_with_42{}
// , std::protect(std::bind(&add, 1, std::placeholders::_1)));
}
提案されましたか? –
'rvalue'の' cref'はおそらく悲惨です。一時的な生涯は 'bind'オブジェクトが渡されている間はほとんど保持されません。 – Yakk
また、ランタイムオーバーヘッドを追加しますが、 'bind'結果を' std :: function'に割り当てることで "保護"することもできます。 – Potatoswatter