提供数个工具以创建并访问未初始化存储
std::destroy_n
template< class ForwardIt, class Size > |
(1) | (C++17 起) |
template< class ExecutionPolicy, class ForwardIt, class Size > |
(2) | (C++17 起) |
1) 销毁从 first
开始的范围中的 n
个对象,如同用
for (; n > 0; (void) ++first, --n)
std::destroy_at(std::addressof(*first));
2) 同 (1) ,但按照 policy
执行。此重载不参与重载决议,除非 std::is_execution_policy_v
first | - | 要销毁的元素范围的起始 |
n | - | 要销毁的元素数目 |
policy | - | 所用的执行策略。细节见执行策略。 |
类型要求 | ||
- ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。 |
||
- 通过 ForwardIt 合法实例的自增、赋值、比较或间接均不抛出异常。 |
已被销毁的元素的范围结尾(即 std::next(first, n) )。
与 n
成线性。
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
ExecutionPolicy
为标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy
,行为是实现定义的。namespace std
{
template
void destroy_at(T* p)
{
p->~T();
}
template< class ForwardIt >
void destroy(ForwardIt first, ForwardIt last)
{
std::cout << __FUNCTION__ << std::endl;
for (; first != last; ++first)
{
std::destroy_at(std::addressof(*first));
}
}
template
ForwardIt destroy_n(ForwardIt first, Size n)
{
for (; n > 0; (void) ++first, --n)
{
std::destroy_at(std::addressof(*first));
}
return first;
}
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct Cell
{
int x;
int y;
Cell(): x(0), y(0)
{
}
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
friend Cell operator+(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x += rcell.x;
cell.y += rcell.y;
return cell;
}
friend Cell operator-(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x -= rcell.x;
cell.y -= rcell.y;
return cell;
}
friend Cell operator*(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x *= rcell.x;
cell.y *= rcell.y;
return cell;
}
friend Cell operator/(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x /= rcell.x;
cell.y /= rcell.y;
return cell;
}
friend Cell operator%(const Cell &lcell, const Cell &rcell)
{
Cell cell = lcell;
cell.x %= rcell.x;
cell.y %= rcell.y;
return cell;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
namespace std
{
template
void destroy_at(T* p)
{
p->~T();
}
template< class ForwardIt >
void destroy(ForwardIt first, ForwardIt last)
{
std::cout << __FUNCTION__ << std::endl;
for (; first != last; ++first)
{
std::destroy_at(std::addressof(*first));
}
}
template
ForwardIt destroy_n(ForwardIt first, Size n)
{
for (; n > 0; (void) ++first, --n)
{
std::destroy_at(std::addressof(*first));
}
return first;
}
template
void uninitialized_value_construct(ForwardIt first, ForwardIt last)
{
using Value = typename std::iterator_traits::value_type;
ForwardIt current = first;
try
{
for (; current != last; ++current)
{
::new (static_cast(std::addressof(*current))) Value();
}
}
catch (...)
{
std::destroy(first, current);
throw;
}
}
template
ForwardIt uninitialized_value_construct_n(ForwardIt first, Size n)
{
using T = typename std::iterator_traits::value_type;
ForwardIt current = first;
try
{
for (; n > 0 ; (void) ++current, --n)
{
::new (static_cast(std::addressof(*current))) T();
}
return current;
}
catch (...)
{
std::destroy(first, current);
throw;
}
}
}
int main()
{
std::cout << "std::destroy_n" << std::endl;
std::cout << std::boolalpha;
std::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
auto generate = []()
{
int n = std::rand() % 10 + 110;
Cell cell{n, n};
return cell;
};
//3) 构造拥有 count 个有值 value 的元素的容器。
std::vector vector1(8, generate());
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator(std::cout, " "));
std::cout << std::endl;
std::cout << "std::destroy_n vector1: ";
std::destroy_n(vector1.begin(), vector1.size());
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator(std::cout, " "));
std::cout << std::endl;
std::vector vector2(vector1.size(), Cell{101, 101});
std::cout << "vector2: ";
std::copy(vector2.begin(), vector2.end(), std::ostream_iterator(std::cout, " "));
std::cout << std::endl;
std::destroy_n(vector2.begin(), vector2.size());
std::cout << "std::destroy_n vector2: ";
std::copy(vector2.begin(), vector2.end(), std::ostream_iterator(std::cout, " "));
std::cout << std::endl;
std::vector vector3;
// 为vector3预留空间
vector3.resize(vector1.size());
std::uninitialized_value_construct_n(vector3.begin(), vector3.size());
std::cout << "vector3: ";
std::copy(vector3.begin(), vector3.end(), std::ostream_iterator(std::cout, " "));
std::cout << std::endl;
std::cout << "std::destroy_n vector3: ";
std::copy(vector3.begin(), vector3.end(), std::ostream_iterator(std::cout, " "));
std::destroy_n(vector3.begin(), vector3.size());
std::cout << std::endl;
return 0;
}
| | | | | | | | |
std::destroy_n
vector1: {112,112} {112,112} {112,112} {112,112} {112,112} {112,112} {112,112} {112,112}
std::destroy_n vector1: {112,112} {112,112} {112,112} {112,112} {112,112} {112,112} {112,112} {112,112}
vector2: {101,101} {101,101} {101,101} {101,101} {101,101} {101,101} {101,101} {101,101}
std::destroy_n vector2: {101,101} {101,101} {101,101} {101,101} {101,101} {101,101} {101,101} {101,101}
vector3: {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0}
std::destroy_n vector3: {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0}