我有一个 VSCode 的 cmake 项目,有几个构建变体。
也就是说,我将 cmake-variants.json 定义为:
{
"buildType": {
"default": "Debug",
"description": "The build type",
"choices": {
"Debug": {
"short": "Debug",
"long": "Debug: with debug info",
"buildType": "Debug"
},
"Release": {
"short": "Release",
"long": "Release: no debug info",
"buildType": "Release"
},
...
}
},
"buildVariant": {
"default": "gcc-a64",
"description": "Build variant (host or cross-compiling with GCC or Clang)",
"choices": {
"gcc-a64": {
"short": "gcc-a64",
"long": "GCCg cross-compile for A64",
},
....
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在 settings.json 中,我使用变体扩展定义 cmake …
我想建立一个由多态对象类型组成的树,Node这些对象是使用自定义 PMR 分配器分配的。
到目前为止,一切都运行良好,但我不知道如何正确删除使用非标准分配器分配的多态对象?我只是想出了一个解决方案来声明一个静态对象,该对象持有对std::pmr::memory_resource.. 的引用,但这很糟糕。有没有“正确”的方法来删除自定义分配的多态对象?
这是一个独立的示例:
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <functional>
#include <memory_resource>
struct Node {
// NOTE: this is actually not necessary..
using allocator_type = std::pmr::polymorphic_allocator<Node>;
void operator delete(void *ptr, std::size_t sz) noexcept {
Node::deleter(ptr, sz);
}
// don't bother with getters/setters so far..
std::pmr::string name;
template <class TNode >
static TNode *create(std::string_view name, std::pmr::memory_resource *res) {
std::pmr::polymorphic_allocator<TNode> alloc(res);
auto ptr = alloc.allocate(1);
::new (ptr) TNode(alloc);
ptr->name = …Run Code Online (Sandbox Code Playgroud)