为什么 std::not1() 通过常量引用而不是按值获取参数
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 &&
?在设计时,它显然不可能接受这样的论点,因为右值引用还不存在。至于后来,这有点猜测,但我想说 lambda 已经很好地满足了需求,所以除了向后兼容性之外,unary_negate
没有多大意义。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 有没有办法在从编译器获取参数时避免预处理器宏?
- C++ - 声明指向返回任何类型并获取任意数量参数的函数的指针
- 如何从参数中获取函数签名?
- 获取可变参数模板参数包的相关类型
- 在 C++11 中获取函子作为参数
- 如何使用可变参数模板获取参数的变量号及其大小C++
- 是否可以获取成员函数模板参数的拥有对象?
- 如何从 x64 程序集中的堆栈中获取参数?
- 获取第一个模板参数的可靠方法
- 如何从参数包获取指向对象的指针
- C++ 如何使派生类自动获取基类参数
- 通过命令行参数获取llvm ir文件时面临问题
- 在C++中,是否允许将函数指针强制转换为将指向基类或派生类的指针作为参数获取的函数指针
- 使用一个可选参数获取两个参数到main
- cpp 映射作为函数参数获取警告信息 "discards qualifiers"
- 从控制台应用中的命令行参数获取文件名C++
- 从模板参数获取函数参数
- 使用参数获取其他参数