std::not1()
原型如下:
template< class Predicate >
std::unary_negate<Predicate> not1(const Predicate& pred);
这有效地禁止了移动语义。为什么它不是原型:
template< class Predicate >
std::unary_negate<Predicate> not1(Predicate pred);
这样,复制或移动取决于pred
的构造方式。然后,该函数只将pred
移动到构造的std::unary_negate
对象。
答案 0 :(得分:5)
单独进行改变完全没用。 not1
所做的是使用std::unary_negate<Predicate>
构造pred
作为构造函数的参数。但std::unary_negate<Predicate>
唯一相关的构造函数需要const Predicate &
,不 Predicate &&
。
合乎逻辑的后续问题是,为什么std::unary_negate<Predicate>
没有构造函数采用Predicate &&
?它在设计时显然不可能采用这样的论证,因为右值参考尚不存在。至于后来,这是一个猜测,但我会说lambdas已经很好地满足了需求,所以unary_negate
除了向后兼容性之外没有多大意义。