- memory_resource[meta header]
- function[meta id-type]
- std::pmr[meta namespace]
- polymorphic_allocator[meta class]
- cpp17[meta cpp]
template <class T>
void destroy(T* p);指定された領域にあるTのオブジェクトを破棄する。
p-- 対象となるオブジェクトが構築されているメモリへのポインタ
あたかもp->~T()を実行したように、pの指すTのオブジェクトを破棄する。
メモリ領域の解放は行われないため、別にdeallocateで行う必要がある。
#include <iostream>
#include <memory_resource>
int main()
{
std::pmr::polymorphic_allocator<int> alloc{};
//メモリの確保
int* array = alloc.allocate(4);
//要素を構築
for (int i = 0; i < 4; ++i) {
alloc.construct(array + i, i);
}
for (int i = 0; i < 4; ++i) {
std::cout << array[i] << std::endl;
}
//要素を破棄
for (int i = 0; i < 4; ++i) {
alloc.destroy(array + i);
}
//メモリの解放
alloc.deallocate(array, 4);
}- destroy[color ff0000]
- allocate[link allocate.md]
- construct[link construct.md]
- deallocate[link deallocate.md]
0
1
2
3
- C++17
- Clang: ??
- GCC: 9.1
- Visual C++: 2017 update 6
- 2017, 2019共にこの関数は意図的に実装されていない(必要なら
destroy_atを利用する)
- 2017, 2019共にこの関数は意図的に実装されていない(必要なら
- P0220R1 Adopt Library Fundamentals V1 TS Components for C++17 (R1)
- P0337r0 | Delete operator= for polymorphic_allocator
- Working Draft, C++ Extensions for Library Fundamentals, Version 2
- destroy: Missing member function of std::pmr::polymorphic_allocator - Developer Community
- VC++においてこの関数の実装がされていない理由について