std::destroy_at 定义于头文件<memory> template<classT> voiddestroy_at(T*p); (C++17 起) (C++20 前) template<classT> constexprvoiddestroy_at(T*p); (C++20 起) 若T不是数组类型,则调用p所指向对象的析构函数,如同用p->~T()。 若T是数组类型,则程序非良构(C++20 前)按顺序递归地销毁*p的元...
在某常量表达式 e 的求值中调用 destroy_at 时,参数 p 必须指向生存期始于 e 的求值内的对象。 (C++20 起)示例下列示例演示如何用 destroy_at 销毁元素的相接序列。 运行此代码 #include <iostream> #include <memory> #include <new> struct Tracer { int value; ~Tracer() { std::cout << value <...
struct destroy_at_fn { template<std::destructible T> constexpr void operator()(T *p) const noexcept { if constexpr (std::is_array_v<T>) for (auto &elem : *p) operator()(std::addressof(elem)); else p->~T(); } }; inline constexpr destroy_at_fn destroy_at{}; Notes destroy_...
{std::cout<<value<<" destructed\n";}};intmain(){alignas(Tracer)unsignedcharbuffer[sizeof(Tracer)*8];for(inti=0;i<8;++i)new(buffer+sizeof(Tracer)*i)Tracer{i};//manually construct objectsautoptr=std::launder(reinterpret_cast<Tracer*>(buffer));for(inti=0;i<8;++i)std::destroy_at(...