c++ - 迭代器和简单的赋值/析构函数

标签 c++ c++11 boost

我尝试使用 boost::lockfree:queue 对迭代器进行排队,但在 static_assert 上编译失败(仅限 Debug模式)

boost::has_trivial_destructor::value

boost::has_trivial_assign::value

代码:

using list_it_t = typename std::list<Elem<T, dimens>>::iterator;
[...]
boost::lockfree::queue<list_it_t> iterators(1024);

其中 Elem 是 POD 类型,但据我所知,该行为也可以用其他迭代器重现。

我一直认为迭代器满足这两个条件?!它们在哪些方面是不可分配或不可破坏的?

我还能以某种方式使用迭代器吗?还是必须更改为元素上的指针?我正在遍历列表并将迭代器插入队列以供其他线程处理。

规范:

  • MSVC v141
  • boost 1.65

最佳答案

std::list::iterator 的唯一要求是它必须是 BidirectionalIterator。 .所以是的,标准库实现可以选择具有非平凡的构造函数、析构函数、赋值运算符......

只能假设实现选择定义此类特殊函数以提供调试检查或信息。

如果你看一下 example implementation of std::list ,您会发现第 122 行...

00110   template<typename _Tp>
00111     struct _List_iterator
00112     {
00113       typedef _List_iterator<_Tp>           _Self;
00114       typedef _List_node<_Tp>               _Node;
00115 
00116       typedef ptrdiff_t                     difference_type;
00117       typedef bidirectional_iterator_tag    iterator_category;
00118       typedef _Tp                           value_type;
00119       typedef _Tp*                          pointer;
00120       typedef _Tp&                          reference;
00121 
00122       _List_iterator()
00123       : _M_node() { }
00124 
00125       _List_iterator(_List_node_base* __x)
00126       : _M_node(__x) { }
...
00175       // The only member points to the %list element.
00176       _List_node_base* _M_node;
00177     };

...我猜是一个用户提供的构造函数来提供更好的故障。

关于c++ - 迭代器和简单的赋值/析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50695849/

相关文章:

c++ - 编译器如何评估模板函数?

c++ - 如何避免 const cast 进行 map 访问?

C++11 类内引用成员初始化生成错误拷贝

linux - Ubuntu 12.04 升级 boost 1.46 到 1.60

c++ - 遍历 boost::multi_array 的维度

c++ - 为什么 "pointer=pointer+i"得到了一种随机值

c++ - 如何允许模板类构造函数根据基类型支持不同数量的参数?

c++ - g++ 中的 'explicit' 关键字对简单构造函数(不是复制/赋值构造函数)没有影响?

c++ - 如何将字符串添加到 vector (并随后显示它)?

c++ - 使用来自matlab的boost::threads时挂起和/或segfault,而不是直接调用时