【发布时间】:2015-12-07 23:38:19
【问题描述】:
我希望编译器强制执行左值(非引用)的 const-ness,但不知道这在 C++ 中是否可行。一个例子:
int foo() { return 5; }
int main() {
// Is there anything I can add to the declaration of foo()
// that would make the following cause a compile-error?
int a = foo();
// Whereas this compiles fine.
const int a = foo();
}
【问题讨论】:
-
什么是“非参考左值”? “左值”是一种表达式,表达式永远不是引用。
-
您的代码中仍然没有“左值”。您正在使用初始化表达式定义变量。
-
既然您退回了副本,出于好奇,您为什么需要这个?
-
@ʎǝɹɟɟɟǝſ:这是一个左值引用,但不是“引用左值”。后者不存在。
-
好的,那么它是一个左值引用。既然我们已经整理了这些技术细节,我们是否可以同意它们对于所提出的问题并不重要?
标签: c++ constants pass-by-value lvalue