将 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 与多态资源分配器一起使用 的相关文章

  • 为什么相同的代码在同一台计算机上的执行时间可能不同?

    我是 C 编程新手 我编写了代码并希望获得它的运行时 这就是我所做的 每次运行代码时 我都会得到不同的运行时值 这样对吗 或者我的代码有问题吗 int main int argc char argv time t start end sta
  • 如何在 Visual Studio 2010 中增强 XAML 设计器?

    当我使用 XAML 设计器时 进入设计器和退出设计器是如此困难和缓慢 当我这样做时 Visual Studio 卡了一段时间 有什么方法可以增强 XAML 设计器和编辑器吗 Ant 保存 XAML 文件时非常慢 这通常意味着您可能有复杂的
  • 如何使用MemoryCache代替Timer来触发一个方法?

    以下方法通过等待已运行操作的结果来处理并发请求 对数据的请求可能会使用相同 不同的凭据同时出现 对于每组唯一的凭据 最多可以有一个GetCurrentInternal呼叫正在进行中 当准备就绪时 该呼叫的结果将返回给所有排队的服务员 pri
  • VB.NET 相当于 C# 属性简写吗?

    是否有与 C 等效的 VB NET public string FirstName get set 我知道你能做到 Public Property name As String Get Return name ToString End Ge
  • 启动时出现 OData v4 错误:找不到段“Whatever”的资源

    我正在构建新的 v4 服务 一切进展顺利 直到我为新模型 实体添加了新控制器 并在启动站点进行测试运行时收到此错误 控制器似乎编码正确 就像其他控制器一样 控制器 CustomersOData 中的操作 GetFeed 上的路径模板 Cus
  • 推导指南中的引用和值之间的差异

    考虑类型A template
  • 如何修复此错误“GDI+ 中发生一般错误”?

    从默认名称打开图像并以默认名称保存 覆盖它 我需要从 Image Default jpg 制作图形 将其放在 picturebox1 image 上并在 picurebox1 上绘制一些图形 它有效 这不是我的问题 但我无法保存 pictu
  • 互斥体实现可以互换(独立于线程实现)

    所有互斥体实现最终都会调用相同的基本系统 硬件调用吗 这意味着它们可以互换吗 具体来说 如果我使用 gnu parallel算法 使用openmp 并且我想让他们称之为线程安全的类我可以使用boost mutex用于锁定 或者我必须编写自己
  • 如何访问另一个窗体上的ListView控件

    当单击与 ListView 所在表单不同的表单中的按钮时 我试图填充 ListView 我在 Form1 中创建了一个方法以在 Form2 中使用 并将参数传递给 Form1 中的方法 然后填充 ListView 当我调试时 我得到了传递的
  • 无法在 Windows 运行时组件库的 UserControl 中创建依赖项属性

    我想在用户控件内创建数据可绑定属性 这个用户控件包含一个 Windows 运行时组件 项目 我使用下面的代码来创建属性 public MyItem CurrentItem get return MyItem GetValue Current
  • ASP.NET:获取自 1970 年 1 月 1 日以来的毫秒数

    我有一个 ASP NET VB NET 日期 我试图获取自 1970 年 1 月 1 日以来的毫秒数 我尝试在 MSDN 中寻找方法 但找不到任何东西 有谁知道如何做到这一点 从 NET 4 6 开始 该方法ToUnixTimeMillis
  • 未定义的行为或误报

    我 基本上 在野外遇到过以下情况 x x 5 显然 它可以在早期版本的 gcc 下编译干净 在 gcc 4 5 1 下生成警告 据我所知 警告是由 Wsequence point 生成的 所以我的问题是 这是否违反了标准中关于在序列点之间操
  • 如何使用 watin 中的 FileUploadDialogHandler 访问文件上传对话框

    我正在使用 IE8 和 watin 并尝试通过我的网页测试上传文件 我不能简单地使用 set 方法设置上传文件 例如 ie FileUpload Find ById someId Set C Desktop image jpg 因为上传文本
  • 批量更新 SQL Server C#

    我有一个 270k 行的数据库 带有主键mid和一个名为value 我有一个包含中值和值的文本文件 现在我想更新表格 以便将每个值分配给正确的中间值 我当前的方法是从 C 读取文本文件 并为我读取的每一行更新表中的一行 必须有更快的方法来做
  • Visual Studio 中的测试单独成功,但一组失败

    当我在 Visual Studio 中单独运行测试时 它们都顺利通过 然而 当我同时运行所有这些时 有些通过 有些失败 我尝试在每个测试方法之间暂停 1 秒 但没有成功 有任何想法吗 在此先感谢您的帮助 你们可能有一些共享数据 检查正在使用
  • 如何编写一个同时需要请求和响应Dtos的ServiceStack插件

    我需要提供本地化数据服务 所有本地化的响应 Dto 都共享相同的属性 IE 我定义了一个接口 ILocalizedDto 来标记那些 Dto 在请求端 有一个ILocalizedRequest对于需要本地化的请求 Using IPlugin
  • 私有模板函数

    我有一堂课 C h class C private template
  • HttpWebRequest 在第二次调用时超时

    为什么以下代码在第二次 及后续 运行时超时 代码挂在 using Stream objStream request GetResponse GetResponseStream 然后引发 WebException 表示请求已超时 我已经尝试过
  • 如何在 C# 中调整图像大小同时保持高质量?

    我从这里找到了一篇关于图像处理的文章 http www switchonthecode com tutorials csharp tutorial image editing saving cropping and resizing htt
  • 如何使用 Word Automation 获取页面范围

    如何使用办公自动化找到 Microsoft Word 中第 n 页的范围 似乎没有 getPageRange n 函数 并且不清楚它们是如何划分的 这就是您从 VBA 执行此操作的方法 转换为 Matlab COM 调用应该相当简单 Pub

随机推荐

  • 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