【发布时间】:2019-10-13 04:44:19
【问题描述】:
我已经定义了一个带有默认参数的函数:
void resize(size_t n, std::string &s = std::string()); // error: initial value to non-const must be lvalue
正确的方法:
void resize(size_t n, const std::string &s = std::string());
我想知道为什么const 会有所作为?
我知道const 变量可以分配一个左值。他们是同一个问题吗?
const int a = 42;
【问题讨论】:
-
可能与此重复:stackoverflow.com/questions/27463785/…
"The idea is that a function taking a non-const reference parameter is stating that it wants to modify the parameter and allowing it to go back to the caller. Doing so with a temporary is meaningless and most likely an error." -
@selbie 谢谢。我想知道如果我使用移动复制构造将临时推回向量中会怎样?