C++11 迭代器和返回的 std::unique_ptr 的范围
C++11 iterator and the scope of a returned std::unique_ptr
>问题
据我了解,当std::unique_ptr
从函数返回到右值时,它的生存期应包含使用该右值的语句。但是使用 gcc 6.4.1 编译时,Foo::iterator()
的返回值在函数 crashing_version()
中的 C++11 foreach 语句开始之前超出了范围。如下面的输出所示,析构函数是在计算包含表达式后立即调用的。这是 gcc 中的错误,还是糟糕的编程实践?
用例
此模式的目标是在不公开私有向量的情况下使迭代可用。这似乎需要一些像Foo::Iterator
这样的对象,因为有两个单独的列表要迭代。
#include <iostream>
#include <memory>
#include <vector>
class Foo {
/* Goal: allow iteration without exposing the vector objects. */
std::vector<int> _list0;
std::vector<int> _list1;
public:
class Iterator {
int _list_id;
Foo& _foo;
public:
Iterator(int list_id, Foo& foo) : _list_id(list_id), _foo(foo) {}
~Iterator() {
std::cout << "~Iterator(): Destroying iterator of the "
<< (_list_id == 0 ? "even" : "odd") << " listn";
}
std::vector<int>::iterator begin() {
if (_list_id == 0)
return _foo._list0.begin();
else
return _foo._list1.begin();
}
std::vector<int>::iterator end() {
if (_list_id == 0)
return _foo._list0.end();
else
return _foo._list1.end();
}
};
void add(int i) {
if ((i % 2) == 0)
_list0.push_back(i);
else
_list1.push_back(i);
}
std::unique_ptr<Iterator> iterator(int list_id) {
return std::make_unique<Iterator>(list_id, *this);
}
};
void working_version() {
Foo foo;
for (int i = 0; i < 10; i++)
foo.add(i);
/* This works because the unique_ptr stays in scope through the loop. */
std::cout << "Valid iterator usage: n";
std::unique_ptr<Foo::Iterator> evens = foo.iterator(0);
for (int i : *evens)
std::cout << i << "n";
}
void crashing_version() {
Foo foo;
for (int i = 0; i < 10; i++)
foo.add(i);
/* Crash! The unique_ptr goes out of scope before the loop starts. */
std::cout << "Corrupt iterator usage: n";
for (int i : *foo.iterator(1))
std::cout << i << "n";
}
int main() {
working_version();
crashing_version();
return 0;
}
程序输出:
Valid iterator usage:
0
2
4
6
8
~Iterator(): Destroying iterator of the even list
Corrupt iterator usage:
~Iterator(): Destroying iterator of the odd list
1
3
5
7
9
for(range_declaration:range_expression)
表达式等效于(在 c++11 和 c++14 中):
{
auto && __range = range_expression ;
for (
auto __begin = begin_expr, __end = end_expr;
__begin != __end;
++__begin)
{
range_declaration = *__begin;
loop_statement
}
}
源,变量以__
开头仅作为指数存在。
我们替代:
for (int i : *evens)
std::cout << i << "n";
我们得到:
{
auto && __range = *evens;
for (
auto __begin = begin_expr, __end = end_expr;
__begin != __end;
++__begin)
{
int i = *__begin;
std::cout << i << "n";
}
}
我们现在可以清楚地看到您的错误。 您的唯一 ptr 持续时间与__range
行一样长,但在取消引用后,唯一 ptr 消失了,我们在 __range
中有一个悬空的引用。
您可以使用一个小助手来解决此问题:
template<class Ptr>
struct range_ptr_t {
Ptr p;
auto begin() const {
using std::begin;
return begin(*p);
}
auto end() const {
using std::end;
return end(*p);
}
};
template<class Ptr>
range_ptr_t<std::decay_t<Ptr>> range_ptr( Ptr&& ptr ) {
return {std::forward<Ptr>(ptr)};
}
现在我们做到了:
for (int i : range_ptr(evens))
std::cout << i << "n";
我们不再有独特的PTR死在我们身上。
将range_expression
的生存期延长到 for(:)
循环的主体可能是个好主意,因为此问题会导致其他问题(例如链接范围适配器时),最终导致类似的烦人的解决方法。
最小测试用例:
std::unique_ptr<std::vector<int>> foo() {
return std::make_unique<std::vector<int>>( std::vector<int>{ 1, 2, 3} );
}
int main() {
for (int x : range_ptr(foo())) {
std::cout << x << 'n';
}
}
您的代码表现出未定义的行为。 GCC、MSVC 和 Clang 的行为都相同; 迭代器的析构函数在输出任何内容之前运行。
在这种情况下,基于范围的 for 循环可以被视为缓存函数调用的便捷方式,因此您的代码等效于 this* ([stmt.ranged]):
auto&& range = *foo.iterator(1);
for (auto __begin = range.begin(), __end = range.end(); __begin!=__end; ++__begin){
int i = *__begin;
std::cout << i << "n";
}
通过取消引用unique_ptr,range
成为对底层Iterator
的引用,然后立即超出范围。
*这些规则在 C++17 中略有变化,因此__begin
和__end
不需要是同一类型
- 为什么 std::unique 不调用 std::sort?
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 生成"unique"矩阵
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 我对 std::unique(算法)C++有问题
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- std::shared_ptr::unique(),复制和线程安全
- 如何在C++03中用自定义谓词调用std::unique
- C++中的指针否定 (!ptr == NULL)
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误