왜C++ 11 std::bind
와 함께 사용 할 std::protect
이 없다?std :: protect가없는 이유는 무엇입니까?
부스트. 바인딩은 boost::bind
이 인식하고 평가하지 않도록 해당 인수를 래핑하는헬퍼를 제공합니다. 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