动态内存管理
未初始化存储
提供数个工具以创建并访问未初始化存储
销毁在给定地址的对象
std::destroy_at
template< class T > | (C++17 起) |
调用 p
所指对象的析构函数,如同用 p->~T()
。
参数
p | - | 指向要被销毁的对象的指针 |
返回值
(无)
可能的实现
namespace std
{
template<class T>
void destroy_at(T* p)
{
std::cout << __FUNCTION__ << std::endl;
p->~T();
}
template< class ForwardIt >
void destroy(ForwardIt first, ForwardIt last)
{
for (; first != last; ++first)
{
std::destroy_at(std::addressof(*first));
}
}
template<class ForwardIt>
void uninitialized_value_construct(ForwardIt first, ForwardIt last)
{
using Value = typename std::iterator_traits<ForwardIt>::value_type;
ForwardIt current = first;
try
{
for (; current != last; ++current)
{
::new (static_cast<void*>(std::addressof(*current))) Value();
}
}
catch (...)
{
std::destroy(first, current);
throw;
}
}
template<class ForwardIt, class Size>
ForwardIt uninitialized_value_construct_n(ForwardIt first, Size n)
{
using T = typename std::iterator_traits<ForwardIt>::value_type;
ForwardIt current = first;
try
{
for (; n > 0 ; (void) ++current, --n)
{
::new (static_cast<void*>(std::addressof(*current))) T();
}
return current;
}
catch (...)
{
std::destroy(first, current);
throw;
}
}
}
调用示例
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <random>
#include <vector>
#include <cassert>
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<class T>
void destroy_at(T* p)
{
std::cout << __FUNCTION__ << std::endl;
p->~T();
}
template< class ForwardIt >
void destroy(ForwardIt first, ForwardIt last)
{
for (; first != last; ++first)
{
std::destroy_at(std::addressof(*first));
}
}
template<class ForwardIt>
void uninitialized_value_construct(ForwardIt first, ForwardIt last)
{
using Value = typename std::iterator_traits<ForwardIt>::value_type;
ForwardIt current = first;
try
{
for (; current != last; ++current)
{
::new (static_cast<void*>(std::addressof(*current))) Value();
}
}
catch (...)
{
std::destroy(first, current);
throw;
}
}
template<class ForwardIt, class Size>
ForwardIt uninitialized_value_construct_n(ForwardIt first, Size n)
{
using T = typename std::iterator_traits<ForwardIt>::value_type;
ForwardIt current = first;
try
{
for (; n > 0 ; (void) ++current, --n)
{
::new (static_cast<void*>(std::addressof(*current))) T();
}
return current;
}
catch (...)
{
std::destroy(first, current);
throw;
}
}
}
int main()
{
std::cout << "std::destroy_at" << 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;
};
auto destroy_at = [](const Cell & cell)
{
std::destroy_at(&cell);
};
//3) 构造拥有 count 个有值 value 的元素的容器。
std::vector<Cell> vector1(8, generate());
std::uninitialized_value_construct_n(vector1.begin(), vector1.size());
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::for_each(vector1.begin(), vector1.end(), destroy_at);
std::cout << std::endl;
std::vector<Cell> vector2(vector1.size(), Cell{101, 101});
std::uninitialized_value_construct_n(vector2.begin(), vector2.size());
std::cout << "vector2: ";
std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::for_each(vector1.begin(), vector1.end(), destroy_at);
std::cout << std::endl;
std::vector<Cell> 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<Cell>(std::cout, " "));
std::cout << std::endl;
std::for_each(vector1.begin(), vector1.end(), destroy_at);
std::cout << std::endl;
return 0;
}
输出
std::destroy_at
vector1: {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0}
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
vector2: {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0}
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
vector3: {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0}
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at
destroy_at