本文介绍了调整原子的矢量大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何调整原子矢量的大小?
How to resize a vector of atomics ?
下面的代码不能编译:
#include <iostream>
#include <vector>
#include <atomic>
int main()
{
std::vector<std::atomic<int>> v;
v.resize(1000); // Problem here!
v[0] = 1;
return 0;
}
错误:
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/vector:62:0,
from main.cpp:2:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::atomic<int>; _Args = {std::atomic<int>}]’:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_uninitialized.h:75:53: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<std::atomic<int>*>; _ForwardIterator = std::atomic<int>*; bool _TrivialValueTypes = false]’
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_uninitialized.h:117:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<std::atomic<int>*>; _ForwardIterator = std::atomic<int>*]’
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_uninitialized.h:258:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<std::atomic<int>*>; _ForwardIterator = std::atomic<int>*; _Tp = std::atomic<int>]’
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_uninitialized.h:281:69: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = std::atomic<int>*; _ForwardIterator = std::atomic<int>*; _Allocator = std::allocator<std::atomic<int> >]’
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/vector.tcc:556:42: required from ‘void std::vector<_Tp, _Alloc>::_M_default_append(std::vector<_Tp, _Alloc>::size_type) [with _Tp = std::atomic<int>; _Alloc = std::allocator<std::atomic<int> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_vector.h:667:41: required from ‘void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = std::atomic<int>; _Alloc = std::allocator<std::atomic<int> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
main.cpp:8:17: required from here
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/stl_construct.h:75:7: error: use of deleted function ‘std::atomic<int>::atomic(const std::atomic<int>&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
In file included from main.cpp:3:0:
/usr/local/gcc-4.8.1/include/c++/4.8.1/atomic:601:7: error: declared here
atomic(const atomic&) = delete;
^
推荐答案
t ...
既不是复制/移动可构造的,也不能指定一个 std :: atomic< T& code>到另一个;这意味着它没有使用
(size_type)
。
void resize (size_type sz);
需要:
T
应为CopyInsertable * this
。
Requires:
T
shall be CopyInsertable into *this
.
建议解决
您需要使用其他类型,其在修改元素时不需要已经构造的数据被移动,被复制或
You will need to use some other container-type which doesn't require already constructed data to be moved, copied, or copy/move assigned, upon modifying the elements already stored inside.
你也可以在你的
std :: atomic
分配,但实际上只是浅读/写底层原子的值。
You could also define a wrapper around your
std::atomic
that fakes copy/moves/assigns, but actually just shallow read/writes the value of the underlying atomic.
这篇关于调整原子的矢量大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!