为什么std :: not1()通过const引用而不是值来获取参数?

时间:2016-02-22 08:07:46

标签: c++ language-lawyer c++-standard-library

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对象。

1 个答案:

答案 0 :(得分:5)

单独进行改变完全没用。 not1所做的是使用std::unary_negate<Predicate>构造pred作为构造函数的参数。但std::unary_negate<Predicate>唯一相关的构造函数需要const Predicate & Predicate &&

合乎逻辑的后续问题是,为什么std::unary_negate<Predicate>没有构造函数采用Predicate &&?它在设计时显然不可能采用这样的论证,因为右值参考尚不存在。至于后来,这是一个猜测,但我会说lambdas已经很好地满足了需求,所以unary_negate除了向后兼容性之外没有多大意义。

相关问题