将 std::allocate_shared 与多态资源分配器一起使用

2023-12-25

我正在尝试创建共享指针std::pmr::monotonic_buffer_resource,我无法编译它。我缺少什么?

https://godbolt.org/z/R9​​jdju https://godbolt.org/z/R9jdju

#include <memory>
#include <memory_resource>

int main() {
    char buffer[100];
    std::pmr::monotonic_buffer_resource mbr(buffer, 100);
    std::shared_ptr<double> sp = std::allocate_shared<double>(mbr);
}


In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/alloc_traits.h:34,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_uninitialized.h:67,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory:66,
                 from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/alloc_traits.h: In substitution of 'template<class _Alloc, class _Up> using __alloc_rebind = typename std::__allocator_traits_base::__rebind<_Alloc, _Up>::type [with _Alloc = std::pmr::monotonic_buffer_resource; _Up = std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>]':
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:542:13:   required from 'class std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:679:43:   required from 'std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:1371:71:   required from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:408:59:   required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:859:14:   required from 'std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}]'
<source>:7:66:   required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/alloc_traits.h:78:11: error: no type named 'type' in 'struct std::__allocator_traits_base::__rebind<std::pmr::monotonic_buffer_resource, std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>, void>'
   78 |     using __alloc_rebind
      |           ^~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:52,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory:84,
                 from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h: In instantiation of 'std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]':
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:1371:71:   required from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:408:59:   required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:859:14:   required from 'std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}]'
<source>:7:66:   required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:682:16: error: use of deleted function 'std::pmr::monotonic_buffer_resource::monotonic_buffer_resource(const std::pmr::monotonic_buffer_resource&)'
  682 |    auto __pi = ::new (__mem)
      |                ^~~~~~~~~~~~~
  683 |      _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
      |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from <source>:2:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory_resource:604:5: note: declared here
  604 |     monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:52,
                 from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory:84,
                 from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:546:33: note:   initializing argument 1 of 'std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {}; _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
  546 |  _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
      |                          ~~~~~~~^~~
  

那个长错误消息基本上可以归结为这两个错误:

错误:'struct std::__allocator_traits_base::__rebind<:pmr::monotonic_buffer_resource std::_sp_counted_ptr_inplace std::pmr::monotonic_buffer_resource __gnu_cxx::_s_atomic>, void> 中没有名为“type”的类型'

错误:使用已删除的函数 'std::pmr::monotonic_buffer_resource::monotonic_buffer_resource(const std::pmr::monotonic_buffer_resource&)'

std::pmr::monotonic_buffer_resource https://en.cppreference.com/w/cpp/memory/monotonic_buffer_resource不满足要求std::allocate_shared() https://en.cppreference.com/w/cpp/memory/shared_ptr/allocate_shared期望,具体来说:

所有内存分配都是使用copy of alloc,它必须满足分配器 https://en.cppreference.com/w/cpp/named_req/Allocator要求。

特别是,“一个COPY of alloc”,此后失败monotonic_buffer_resource的复制构造函数是delete所以它无法被复制。

正如@MilesBudnek 在评论中所述,您可以将monotonic_buffer_resource里面一个std::pmr::polymorphic_allocator https://en.cppreference.com/w/cpp/memory/polymorphic_allocator,它被设计用作Allocator https://en.cppreference.com/w/cpp/named_req/Allocator对于标准集装箱:

类模板std::pmr::polymorphic_allocator is an 分配器 https://en.cppreference.com/w/cpp/named_req/Allocator它表现出不同的分配行为,具体取决于std::pmr::memory_resource它是由它构建的。

例如:

#include <memory>
#include <memory_resource>

int main() {
    char buffer[100];
    std::pmr::monotonic_buffer_resource mbr(buffer, 100);
    auto sp = std::allocate_shared<double, std::pmr::polymorphic_allocator<double>>(&mbr);
}

https://godbolt.org/z/-xFfFY https://godbolt.org/z/-xFfFY

或者:

#include <memory>
#include <memory_resource>

int main() {
    char buffer[100];
    std::pmr::monotonic_buffer_resource mbr(buffer, 100);
    std::pmr::polymorphic_allocator<double> alloc(&mbr);
    auto sp = std::allocate_shared<double>(alloc);
}

https://godbolt.org/z/GLE4-5 https://godbolt.org/z/GLE4-5

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 std::allocate_shared 与多态资源分配器一起使用 的相关文章

  • EF Core Group By 翻译支持条件总和

    听说 EF Core 2 1 将支持翻译小组 我感到非常兴奋 我下载了预览版并开始测试它 但发现我在很多地方仍然没有得到翻译分组 在下面的代码片段中 对 TotalFlagCases 的查询将阻止翻译分组工作 无论如何 我可以重写这个以便我
  • 为什么 C# Array.BinarySearch 这么快?

    我已经实施了一个很简单用于在整数数组中查找整数的 C 中的 binarySearch 实现 二分查找 static int binarySearch int arr int i int low 0 high arr Length 1 mid
  • 为什么两个不同的 Base64 字符串的转换会返回相等的字节数组?

    我想知道为什么从 base64 字符串转换会为不同的字符串返回相同的字节数组 const string s1 dg const string s2 dq byte a1 Convert FromBase64String s1 byte a2
  • 按成员序列化

    我已经实现了template
  • 秒表有最长运行时间吗?

    多久可以Stopwatch在 NET 中运行 如果达到该限制 它会回绕到负数还是从 0 重新开始 Stopwatch Elapsed返回一个TimeSpan From MSDN https learn microsoft com en us
  • OleDbDataAdapter 未填充所有行

    嘿 我正在使用 DataAdapter 读取 Excel 文件并用该数据填充数据表 这是我的查询和连接字符串 private string Query SELECT FROM Sheet1 private string ConnectStr
  • 堆栈溢出:堆栈空间中重复的临时分配?

    struct MemBlock char mem 1024 MemBlock operator const MemBlock b const return MemBlock global void foo int step 0 if ste
  • C++ OpenSSL 导出私钥

    到目前为止 我成功地使用了 SSL 但遇到了令人困惑的障碍 我生成了 RSA 密钥对 之前使用 PEM write bio RSAPrivateKey 来导出它们 然而 手册页声称该格式已经过时 实际上它看起来与通常的 PEM 格式不同 相
  • 带动态元素的 WPF 启动屏幕。如何?

    我是 WPF 新手 我需要一些帮助 我有一个加载缓慢的 WPF 应用程序 因此我显示启动屏幕作为权宜之计 但是 我希望能够在每次运行时更改屏幕 并在文本区域中显示不同的引言 这是一个生产力应用程序 所以我将使用非愚蠢但激励性的引言 当然 如
  • 转发声明和包含

    在使用库时 无论是我自己的还是外部的 都有很多带有前向声明的类 根据情况 相同的类也包含在内 当我使用某个类时 我需要知道该类使用的某些对象是前向声明的还是 include d 原因是我想知道是否应该包含两个标题还是只包含一个标题 现在我知
  • 如何序列化/反序列化自定义数据集

    我有一个 winforms 应用程序 它使用强类型的自定义数据集来保存数据进行处理 它由数据库中的数据填充 我有一个用户控件 它接受任何自定义数据集并在数据网格中显示内容 这用于测试和调试 为了使控件可重用 我将自定义数据集视为普通的 Sy
  • 使用 x509 证书签署 json 文档或字符串

    如何使用 x509 证书签署 json 文档或字符串 public static void fund string filePath C Users VIKAS Desktop Data xml Read the file XmlDocum
  • 如何从两个不同的项目中获取文件夹的相对路径

    我有两个项目和一个共享库 用于从此文件夹加载图像 C MainProject Project1 Images 项目1的文件夹 C MainProject Project1 Files Bin x86 Debug 其中有project1 ex
  • cmake 将标头包含到每个源文件中

    其实我有一个简单的问题 但找不到答案 也许你可以给我指一个副本 所以 问题是 是否可以告诉 cmake 指示编译器在每个源文件的开头自动包含一些头文件 这样就不需要放置 include foo h 了 谢谢 CMake 没有针对此特定用例的
  • 基于 OpenCV 边缘的物体检测 C++

    我有一个应用程序 我必须检测场景中某些项目的存在 这些项目可以旋转并稍微缩放 更大或更小 我尝试过使用关键点检测器 但它们不够快且不够准确 因此 我决定首先使用 Canny 或更快的边缘检测算法 检测模板和搜索区域中的边缘 然后匹配边缘以查
  • 哪种 C 数据类型可以表示 40 位二进制数?

    我需要表示一个40位的二进制数 应该使用哪种 C 数据类型来处理这个问题 如果您使用的是 C99 或 C11 兼容编译器 则使用int least64 t以获得最大的兼容性 或者 如果您想要无符号类型 uint least64 t 这些都定
  • 如何在文本框中插入图像

    有没有办法在文本框中插入图像 我正在开发一个聊天应用程序 我想用图标图像更改值 等 但我找不到如何在文本框中插入图像 Thanks 如果您使用 RichTextBox 进行聊天 请查看Paste http msdn microsoft co
  • 如何防止用户控件表单在 C# 中处理键盘输入(箭头键)

    我的用户控件包含其他可以选择的控件 我想实现使用箭头键导航子控件的方法 问题是家长控制拦截箭头键并使用它来滚动其视图什么是我想避免的事情 我想自己解决控制内容的导航问题 我如何控制由箭头键引起的标准行为 提前致谢 MTH 这通常是通过重写
  • 使用.NET技术录制屏幕视频[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有没有一种方法可以使用 NET 技术来录制屏幕 无论是桌面还是窗口 我的目标是免费的 我喜欢小型 低
  • 对来自流读取器的过滤数据执行小计

    编辑问题未得到解答 我有一个基于 1 个标准的过滤输出 前 3 个数字是 110 210 或 310 给出 3 个不同的组 从流阅读器控制台 问题已编辑 因为第一个答案是我给出的具体示例的字面解决方案 我使用的实际字符串长度为 450 个

随机推荐

  • Jayway JsonPath读取长Java

    在 JSON 中 我收到一个 unix 时间戳 order date 1531380888 我想把这个值读入long所以我可以用它创建一个 Date 对象 Configuration conf Configuration builder m
  • 将 NA 值的框添加到连续图的 ggplot 图例中

    我有一张带有图例渐变的地图 我想为 NA 值添加一个框 我的问题非常类似于this one https stackoverflow com questions 29151167 add na value to ggplot legend f
  • Github SSH 部署密钥的权限被拒绝

    我创建了一个新的存储库 能够使用 SSH 进行克隆并提交等等 但是当我尝试推送时出现以下错误 ERROR Permission to Ronin11 MealPlanr git denied to deploy key fatal Coul
  • 间歇性 SQL 异常 - 网络相关或特定于实例的错误

    我们有一个非常奇怪的间歇性问题 该问题在上个月左右开始出现 其中一些与 mssql 服务器的连接失败并出现错误 System Data SqlClient SqlException A network related or instance
  • 有什么方法可以检查变量是否是真正的 jqXHR?

    正如标题已经提到的 有没有办法检查变量是否是真正的jqXHR 我的意思是 想象的 var resource get resource if resource instanceof jqXHR do something 我试图解决的实际问题是
  • SQLAlchemy 基本问题

    我相信对于任何有 SQLAlchemy 经验的人来说 这都是基础知识 但我觉得这些文档没有什么帮助 而且我厌倦了挠头 给定两个类 class User Base tablename users id Column Integer prima
  • 包装 slf4j API

    我想将 slf4j 与 Logback 改造为遗留应用程序 好处是 遗留应用程序有自己的日志框架 所以我所要做的就是更改日志框架以记录到 slf4j 而不是 log4j 这就像做梦一样 我很高兴 直到我注意到 Logback 为每个日志事件
  • 如何让 IntelliSense 自动完成 XAML 中自定义/用户控件的枚举? [复制]

    这个问题在这里已经有答案了 可能的重复 WPF 如何在 vs2008 xaml 编辑器智能感知中显示枚举属性值 https stackoverflow com questions 419802 wpf how to display enum
  • 当我传递特定参数时,使用 ElementTree 的 iter() 解析 XML 找不到我的标签

    尝试从标签返回属性和值 逐字逐句地遵循 ElementTree 文档不会产生任何结果 没有错误 它只是运行并且不打印任何内容 如果我在没有参数的情况下运行 iter 它会打印每个标签 但如果有参数 它什么也不做 不知道发生了什么事 find
  • 使用 dockerfile 安装 Composer

    我对 docker 还很陌生 我尝试在 Dockerfile 中自动执行 Composer install 但在安装时似乎无法 cd 进入我的应用程序 出了什么问题 或者也许还有另一种更好的方法来做到这一点 我的 docker compos
  • 如何显示 MKAnnotation 的副标题 2 行文本并更改右侧按钮的图像?

    我正在查看 Apple 的 MapCallouts 示例 了解地图注释和标注 单击图钉时出现的气泡 每个注释都有坐标 标题和副标题 我想用两行显示字幕 我尝试过 NSString subtitle return Founded June 2
  • 即使用户已登录,wolkenkit 也会重定向到 Auth0

    我只是尝试按照 wolkenkit 文档使用聊天模板测试 wolkenkit 的身份验证 用户登录似乎可以工作 但即使用户已经登录 用户也会被重定向到 Auth0 客户端无需调用auth login方法 这是来自客户端的代码片段 wolke
  • 使 JEditable 适用于新元素 (.live)

    我正在使用 JEditable 插件进行就地编辑 我有一个 设置 功能 它调用 editable 所有相关课程 问题是 我有新附加的元素 我也想使其可编辑 显然 是新增的 editable 永远不会被叫到他们 换句话说 我希望获得 jque
  • Firebase 离线商店 - 查询未返回在线商店中的更改

    我在用着Firebase离线能力设置为 true let ref FIRDatabase database referenceWithPath my data child my users id scoresRef keepSynced t
  • 如何使用 javascript/jquery 动态更改图像?

    所以 我有这个http jsfiddle net ithril UjGhE 1 http jsfiddle net ithril UjGhE 1 请检查一下 我在这里尝试的是将主图像 img 标签的 src 动态更改为所单击图像的相同 sr
  • android 列表视图意图

    单击列表视图后 我无法创建意图 完成后提示错误 应用程序意外停止 请重试 public void onItemClick AdapterView
  • 缺少 Sweet Alert 的选择选项

    这可能是一个 ServiceNow 问题 但我添加了一个甜蜜警报来显示一个选择框 这样我就可以收集一个值以传递到下一条记录 但是选择框没有显示 弹出窗口只是没有框或选项 我缺少什么 截屏 选择框警报 https i stack imgur
  • Laravel 中的动态数据库连接

    我知道在 Laravel 中你可以通过在config database php文件 然后使用DB connection my conn name 但是无论如何都可以使用未在其中指定的连接config database php file 我正
  • 将类转换为字节数组 + C#

    如何在 C 中将类转换为字节数组 这是一个托管代码 因此以下代码失败 int objsize System Runtime InteropServices Marshal SizeOf objTimeSeries3D byte arr ne
  • 将 std::allocate_shared 与多态资源分配器一起使用

    我正在尝试创建共享指针std pmr monotonic buffer resource 我无法编译它 我缺少什么 https godbolt org z R9 jdju https godbolt org z R9jdju include