相关疑难解决方法(0)

延长临时工的寿命

允许这样做的背后的设计理由是什么?

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)

第二行可能出现什么问题(第一行不会出错)?

c++ const reference temporary rvalue

14
推荐指数
2
解决办法
977
查看次数

typedef和const指针的容器

以下代码行编译得很好并且表现得很好:

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)相同?

c++ containers stl typedef

8
推荐指数
4
解决办法
1427
查看次数

标签 统计

c++ ×2

const ×1

containers ×1

reference ×1

rvalue ×1

stl ×1

temporary ×1

typedef ×1