繁体   English   中英

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

[英]Why does std::not1() take parameter by const reference instead of by value?

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM