Why does std :: not1 () accept a parameter by const reference instead of a value?

std::not1() prototyped as follows:

template< class Predicate >
std::unary_negate<Predicate> not1(const Predicate& pred);

This effectively prohibits the semantics of movement. Why is it not prototyped as:

template< class Predicate >
std::unary_negate<Predicate> not1(Predicate pred);

Thus, copying or moving depends on how it is built pred. Then the function simply moves predto the constructed object std::unary_negate.

+4
source share
1 answer

It would be completely useless to make this change by itself. What not1does is a std::unary_negate<Predicate>, using predas an argument to the constructor. But the only relevant constructor std::unary_negate<Predicate>takes const Predicate &, not Predicate &&.

, std::unary_negate<Predicate> , Predicate &&? , , , rvalue . , , , , unary_negate , .

+5

Source: https://habr.com/ru/post/1629858/


All Articles