允许这样做的背后的设计理由是什么?
const Foo& a = function_returning_Foo_by_value();
Run Code Online (Sandbox Code Playgroud)
但不是这个
Foo& a = function_returning_Foo_by_value();
Run Code Online (Sandbox Code Playgroud)
?
第二行可能出现什么问题(第一行不会出错)?
以下代码行编译得很好并且表现得很好:
list<const int *> int_pointers; // (1)
Run Code Online (Sandbox Code Playgroud)
以下两行不是:
typedef int * IntPtr;
list<const IntPtr> int_pointers; // (2)
Run Code Online (Sandbox Code Playgroud)
我得到完全相同的编译错误
list<int * const> int_pointers; // (3)
Run Code Online (Sandbox Code Playgroud)
我很清楚最后一行不合法,因为STL容器的元素需要可分配.为什么编译器解释(2)与(3)相同?