c++ - Perfect Forwarding - 关于 const 和 non-const ref 的问题

标签 c++ perfect-forwarding

看这篇文章http://thbecker.net/articles/rvalue_references/section_07.html . 我们可以找到这句话:

Firstly, if factory had not one, but several arguments, you would have to provide overloads for all combinations of non-const and const reference for the various arguments.

我的问题是,为什么我们需要为非 const 和 const 引用的组合提供重载?在我看来,我们只需要为 const 引用提供重载,因为我们可以将右值和左值都传递给 const 引用。

最佳答案

如果您转发到的函数采用常量,那么您需要传递一个常量引用。如果您转发的值是非常量,那么您需要传递一个非常量,因为您将需要转发到非常量函数。

关于c++ - Perfect Forwarding - 关于 const 和 non-const ref 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54428572/

相关文章:

c++ - 当我终止应用程序时无法保存写入文件的数据

c++ - 如何为非 const 和 const 重载实现一次成员函数?

c++ - 完善的转运容器元素

c++ - 为什么要明确 move 转发引用?

c++ - 为什么这个函数会把RAX压栈作为第一个操作呢?

c++ - 如何将表从 Lua 传递到 C++?

c++ - 函数转发参数,什么都不做

c++ - std::forward 如何接收正确的参数?

c++ - 不理解左值的双重转发 - 按值传递时

c# - UE4 - 对象有反射 - 那为什么要使用 C++?