按位置查找未标记的模板选项/参数/参数

2023-12-03

简而言之:我想从可变参数模板参数中提取各种选项,但不仅通过标签,还通过这些参数的索引,这些参数没有known标签。我喜欢 boost 中的方法(例如heap or lockfree政策),但希望使其兼容STL容器- 分配器参数。

Preface

我目前正在为具有以下签名的可变大小记录/对象的队列/缓冲区编写模板:

// current:
template <typename R = byte, class Alloc = std::allocator<byte>,
  class... Opts> class rqueue;
// what I want:
template <class... Opts> class rqueue;

我有一些其他可能的选择,我是这样描述的:

namespace tag {
    struct allocator {};        ///< specify allocator for data storage
    struct virtual_offset {};   ///< manage offset in virtual memory
    struct fill_empty {};       ///< fill empty space (security or reconstruction)
    struct reconstructible {};  ///< fill empty and leave one slot between wp&rp
} namespace opt {
/// allocator for data storage
    template <class Alloc> struct allocator: tag::allocator {
        typedef Alloc type; };
/// type for offset in virtual memory
    template <class Off> struct virtual_offset: tag::virtual_offset {
        typedef Off type; };
/// type and value to fill empty space
    template <class T, T V> struct fill_empty: tag::fill_empty {
        typedef T type; static constexpr T value = V; };
/// make state pointers reconstructible by leaving one slot between wp&rp
    template <class T, T V> struct reconstructible
      : tag::reconstructible, fill_empty<T, V> {};
}

Usage

// basic queue for custom record class
rqueue<record>;
// advanced record storage that can be written to a file and reconstructed back
rqueue<opt::virtual_offset<unsigned>, opt::reconstructible<byte,0xFF>>;
// specialization for strings with custom allocator
rqueue<string, myalloc>;
// alternative to above
rqueue<const char*, opt::allocator<myalloc>>;

选项包助手

namespace opt {
template<class... Opts> struct bind {
    template<class Tag> static constexpr bool has() {
        return false; }
    template<class Tag, class Default = void>
      using get = Default; };
template<class First, class... More> struct bind<First, More...> {
private:
    template<class Tag> static constexpr bool first() {
        return std::is_same<Tag, First>::value
          || std::is_base_of<Tag, First>::value; }
    template<class Tag, class Default, bool> struct get_ {
        typedef typename bind<More...>::template get<Tag, Default> type; };
    template<class Tag, class Default> struct get_<Tag, Default, true> {
        typedef First type; };
public:
    template<class Tag> static constexpr bool has() {
        return first<Tag>() || bind<More...>::template has<Tag>(); }
    template<class Tag, class Default = void>
      using get = typename get_<Tag, Default, first<Tag>()>::type; };
}

它并不像Boost参数库,但完成了工作...到目前为止,包含未标记的必需参数和标记的可选参数列表。

测试代码

cout << boolalpha;

typedef opt::bind<
  opt::virtual_offset<unsigned>,
  opt::reconstructible<char,0>
  > opts;
cout << opts::has<tag::virtual_offset>() << endl;
cout << opts::has<tag::fill_empty>() << endl;
cout << opts::has<tag::reconstructible>() << endl;
cout << typeid(opts::get<tag::virtual_offset>::type).name() << endl;
cout << typeid(opts::get<tag::fill_empty>::type).name() << endl;
cout << (int)opts::get<tag::fill_empty>::value << endl;

typedef opt::bind<> no;
cout << no::has<tag::virtual_offset>() << endl;
cout << no::has<tag::fill_empty>() << endl;
cout << no::has<tag::reconstructible>() << endl;
cout << typeid(no::get<tag::virtual_offset>).name() << endl;

typedef opt::bind<opt::fill_empty<char,0>> one;
cout << one::has<tag::virtual_offset>() << endl;
cout << one::has<tag::fill_empty>() << endl;
cout << one::has<tag::reconstructible>() << endl;
cout << typeid(one::get<tag::virtual_offset>).name() << endl;

问题

我正在搜索复杂的升压参数/元编程库并以metafunctions它可以完成与我的小帮手相同的工作(当然还有更多),但没有找到解决方案按索引提取未标记的选项。

  1. 我错过了吗?它在某个地方吗?
  2. 你能为我提供一些不同的解决方案吗?

也许我应该忘记它并坚持使用这些标签,或者编写一些复杂的帮助程序过滤掉所有标记的选项并访问索引留下的选项...但在我投降或走很长的路之前,这里是一个询问的好地方:)

Note:如果您引用某个库,请留下如何使用它的描述。谢谢。


我已经成功让它工作了

(已提供的选项包帮助程序的专业化,与原始代码合并)

template<class... Tags> struct tags {
    template<class Option> static constexpr bool match() {
        return false; }};
template<class First, class... More> struct tags<First, More...> {
    template<class Option> static constexpr bool match() {
        return std::is_same<First, Option>::value
          || std::is_base_of<First, Option>::value
          || tags<More...>::template match<Option>(); }};
//-----------------------------------------------------------------------
template<class... Tags, class... Opts>
  struct bind<tags<Tags...>, Opts...> {
    static constexpr size_t size = sizeof...(Opts);
    typedef opt::tags<Tags...> tags;
    template<class Tag> static constexpr bool has() {
        return false; }
    template<class Tag, class Default = void>
      using get = Default;
    template<size_t idx, class Default = void>
      using at = Default;
    static constexpr size_t count = 0; };
template<class... Tags, class First, class... More>
  struct bind<tags<Tags...>, First, More...> {
public:
    typedef opt::tags<Tags...> tags;
    static constexpr size_t size = 1 + sizeof...(More);
private:
    template<size_t idx, class Default, bool> struct at_ {
        typedef typename bind<tags, More...>::template at<idx,Default> type; };
    template<size_t idx, class Default> struct at_<idx, Default, false> {
        typedef typename bind<tags, More...>::template at<idx-1,Default> type; };
    template<class Default> struct at_<0, Default, false> {
        typedef First type; };
public:
    template<class Tag> static constexpr bool has() {
        return bind<First, More...>::template has<Tag>(); }
    template<class Tag, class Default = void>
      using get = typename bind<First, More...>::template get<Tag,Default>;
    template<size_t idx, class Default = void>
      using at = typename at_<idx, Default, tags::template match<First>()>::type;
    static constexpr size_t count = bind<tags, More...>::count
      + (tags::template match<First>() ? 0 : 1); };

Usage

template <class... Opts> class rqueue {
//  bind tags and options
    typedef opt::bind<opt::tags<tag::allocator,
      tag::virtual_offset, tag::fill_empty,
      tag::reconstructible, tag::fixed_size>,
      Opts...> opts;
public:
//  get first untagged option or byte if none
    typedef typename opts::template at<0,byte>
      value_type, record_type, *pointer, &reference;
//  get second untagged option, or allocator option or std::allocator<byte>
    typedef typename std::conditional<
      (opts::count > 1),
      typename opts::template at<1>,
      typename opts::template get<tag::allocator,
      std::allocator<byte>>>::type allocator_type;
//...shorter version of the above
    typedef typename opts::template at<1,
      typename opts::template get<tag::allocator,
      std::allocator<byte>>> allocator_type_v2;
//  get type specified as virtual_offset or void
    typedef typename opts::template get<tag::virtual_offset,
      std::enable_if<true>>::type offset_type;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

按位置查找未标记的模板选项/参数/参数 的相关文章

  • C 编程 - 文件 - fwrite

    我有一个关于编程和文件的问题 while current NULL if current gt Id Doctor 0 current current gt next id doc current gt Id Doctor if curre
  • 没有强命名的代码签名是否会让您的应用程序容易被滥用?

    尝试了解authenticode代码签名和强命名 我是否正确地认为 如果我对引用一些 dll 非强命名 的 exe 进行代码签名 恶意用户就可以替换我的 DLL 并以看似由我签名但正在运行的方式分发应用程序他们的代码 假设这是真的 那么您似
  • 为什么两个不同的 Base64 字符串的转换会返回相等的字节数组?

    我想知道为什么从 base64 字符串转换会为不同的字符串返回相同的字节数组 const string s1 dg const string s2 dq byte a1 Convert FromBase64String s1 byte a2
  • 秒表有最长运行时间吗?

    多久可以Stopwatch在 NET 中运行 如果达到该限制 它会回绕到负数还是从 0 重新开始 Stopwatch Elapsed返回一个TimeSpan From MSDN https learn microsoft com en us
  • 为什么当实例化新的游戏对象时,它没有向它们添加标签? [复制]

    这个问题在这里已经有答案了 using System Collections using System Collections Generic using UnityEngine public class Test MonoBehaviou
  • OleDbDataAdapter 未填充所有行

    嘿 我正在使用 DataAdapter 读取 Excel 文件并用该数据填充数据表 这是我的查询和连接字符串 private string Query SELECT FROM Sheet1 private string ConnectStr
  • 关于 C++ 转换:参数 1 从“[some_class]”到“[some_class]&”没有已知的转换

    我正在研究 C 并且遇到了一个错误 我不知道确切的原因 我已经找到了解决方案 但仍然想知道原因 class Base public void something Base b int main Base b b something Base
  • 使用 Bearer Token 访问 IdentityServer4 上受保护的 API

    我试图寻找此问题的解决方案 但尚未找到正确的搜索文本 我的问题是 如何配置我的 IdentityServer 以便它也可以接受 授权带有 BearerTokens 的 Api 请求 我已经配置并运行了 IdentityServer4 我还在
  • while 循环中的 scanf

    在这段代码中 scanf只工作一次 我究竟做错了什么 include
  • 如何在 C 中调用采用匿名结构的函数?

    如何在 C 中调用采用匿名结构的函数 比如这个函数 void func struct int x p printf i n p x 当提供原型的函数声明在范围内时 调用该函数的参数必须具有与原型中声明的类型兼容的类型 其中 兼容 具有标准定
  • Windows 窗体:如果文本太长,请添加新行到标签

    我正在使用 C 有时 从网络服务返回的文本 我在标签中显示 太长 并且会在表单边缘被截断 如果标签不适合表单 是否有一种简单的方法可以在标签中添加换行符 Thanks 如果您将标签设置为autosize 它会随着您输入的任何文本自动增长 为
  • 覆盖子类中的字段或属性

    我有一个抽象基类 我想声明一个字段或属性 该字段或属性在从该父类继承的每个类中具有不同的值 我想在基类中定义它 以便我可以在基类方法中引用它 例如覆盖 ToString 来表示 此对象的类型为 property field 我有三种方法可以
  • 如何使用 C# / .Net 将文件列表从 AWS S3 下载到我的设备?

    我希望下载存储在 S3 中的多个图像 但目前如果我只能下载一个就足够了 我有对象路径的信息 当我运行以下代码时 出现此错误 遇到错误 消息 读取对象时 访问被拒绝 我首先做一个亚马逊S3客户端基于我的密钥和访问配置的对象连接到服务器 然后创
  • 如何从两个不同的项目中获取文件夹的相对路径

    我有两个项目和一个共享库 用于从此文件夹加载图像 C MainProject Project1 Images 项目1的文件夹 C MainProject Project1 Files Bin x86 Debug 其中有project1 ex
  • C# 成员变量继承

    我对 C 有点陌生 但我在编程方面有相当广泛的背景 我想做的事情 为游戏定义不同的 MapTiles 我已经像这样定义了 MapTile 基类 public class MapTile public Texture2D texture pu
  • 混合 ExecutionContext.SuppressFlow 和任务时 AsyncLocal.Value 出现意外值

    在应用程序中 由于 AsyncLocal 的错误 意外值 我遇到了奇怪的行为 尽管我抑制了执行上下文的流程 但 AsyncLocal Value 属性有时不会在新生成的任务的执行范围内重置 下面我创建了一个最小的可重现示例来演示该问题 pr
  • 是否可以在 .NET Core 中将 gRPC 与 HTTP/1.1 结合使用?

    我有两个网络服务 gRPC 客户端和 gRPC 服务器 服务器是用 NET Core编写的 然而 客户端是托管在 IIS 8 5 上的 NET Framework 4 7 2 Web 应用程序 所以它只支持HTTP 1 1 https le
  • Windows 和 Linux 上的线程

    我在互联网上看到过在 Windows 上使用 C 制作多线程应用程序的教程 以及在 Linux 上执行相同操作的其他教程 但不能同时用于两者 是否存在即使在 Linux 或 Windows 上编译也能工作的函数 您需要使用一个包含两者的实现
  • 如何在文本框中插入图像

    有没有办法在文本框中插入图像 我正在开发一个聊天应用程序 我想用图标图像更改值 等 但我找不到如何在文本框中插入图像 Thanks 如果您使用 RichTextBox 进行聊天 请查看Paste http msdn microsoft co
  • 对来自流读取器的过滤数据执行小计

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

随机推荐

  • Python-将用户输入转换为列表

    有没有办法要求用户输入并将他们的输入转换为列表 元组或字符串 我想要将一系列数字插入到矩阵中 我可以告诉他们将所有数字输入控制台 不带空格并迭代它们 但是还有其他方法可以做到这一点吗 您可以简单地执行以下操作 user input inpu
  • 在客户端离线解析 XSLT

    是否有任何用于 XSLT 的离线 无需通过网络服务器提交代码 解析器 XSLT IDE 交互式开发环境 X选择器 我已经使用了 6 7 年了 免费 有 MSXML 调试器 有 XSLT 1 0 和 XSLT 2 0 的智能感知 此外还有一些
  • 如何使用 Google API 版本 2 在 Google Apps 日历中添加“附件”?

    我正在使用 NET 的 Google API 版本 2 来创建 Google 日历条目 如何将 附件 添加到 Google 日历 Thanx 我试图自己找到这个问题的答案 在使用以下命令查看各种查询的 XML 结果之后日历 API 源 我很
  • 将鼠标事件从 Java UI 传递到后面的应用程序

    我的问题与要求完全相同如何在 C Vista 中将鼠标事件传递给我后面的应用程序 但我需要同样的透明 Java UI 我可以使用 6 0 轻松创建透明的 Java UI 但无法获取有关通过应用程序将事件传递到后面的任何应用程序 例如浏览器
  • JavaFX 和外部 JAR

    我正在尝试部署 JavaFX 应用程序 但是当我在浏览器中运行它时 它似乎无法在外部 jar 文件中找到类 java lang RuntimeException java lang ExceptionInInitializerError C
  • 具有单个 ManagedObjectContext 的 NSManagedObject 的 NSMergeConflict

    我正在使用 coreData 文件夹和文件之间有一对多的关系 我在整个申请过程中仅使用一个 MOC 我只是将它传递给不同的 viewControllers 执行添加 编辑 删除和保存等操作 我的 rootViewController 使用
  • 在flutter中配置local_auth插件

    我的项目使用 kotlin 当我想使用 Local auth 插件时 出现以下错误 PlatformException no fragment activity local auth plugin requires activity to
  • 如何从 asp.net mvc 2 应用程序中的自定义 ValidationAttribute 内部访问其他属性值?

    我正在使用带有 C 和 DataAnnotations 的 asp net mvc 2 情况是这样的 我有一个针对模型类强类型的自定义控件 该控件在视图上显示多次 但具有不同的属性值 例如标题 例如 问题 1 问题 2 问题 3 等都是标题
  • Python递归查找文件并移动到一个目标目录

    该脚本应递归地遍历 rootpath 目录并查找所有具有 mp4 扩展名的文件 打印具有目录结构的文件列表 然后将文件移动到 destDir 目录 我遇到的问题是当尝试将文件移动到新目录时 只有 rootPath 目录中的文件才会移动到新目
  • 是否有一个随机数生成器可以在 O(1) 中跳过/删除 N 次抽奖?

    是否有任何 非加密 伪随机数生成器可以在 O 1 中跳过 删除 N 个绘图 或者可能是 O log N 但小于 O N 特别是对于并行应用 拥有上述类型的发生器将是有利的 您想要生成随机数数组的图像 人们可以为此任务编写一个并行程序 并独立
  • Flex Datagrid - 如何获取鼠标 x/y 坐标的项目?

    我的任务是在 DataGrid 实例中选择一个项目 除了屏幕上的坐标之外什么都没有 我们正在 Flash 应用程序中实现右键单击功能 目标是能够右键单击 DG 行 这将选择该行并显示包含一些上下文命令的弹出窗口 我已经设法在我的 Flex
  • WCF DataServices (CTP2):客户端和服务之间存在类型不匹配

    我正在将 WCF Dataservices CTP2 与 Entity Framework 4 1 结合使用 现在 我尝试通过我的数据上下文获取任何数据 但出现此异常 System Reflection TargetInitationExc
  • 如何在Android中添加2个org.opencv.core.Point对象?

    我是 OpenCV 和 Android 的新手 我正在尝试将 C 代码转换为 java line img matches scene corners 0 Point2f img object cols 0 scene corners 1 P
  • dplyr 自定义滞后函数用于不规则时间序列

    我有一个不规则的时间序列 数据集中存在间隙 此外 数据被分组 滞后函数我已经能够通过观察找到滞后 因此它们在数据集中找到先前的记录 但我想指定一个时间变量并通过匹配滞后时间来计算滞后 这个问题 R滞后 超前不规则时间序列数据正在做类似的事情
  • 升级到 Angular 9 后指令无法与 FormControl 一起使用

    我已经使用指令来启用和禁用表单 这是在一个单独的打字稿文件中 代码如下 import NgControl from angular forms import Directive Input from angular core Directi
  • 为什么lxml在解析时关闭这个“ol”标签?

    这是一些 HTML ol ul li item li ul ol 和一些 python 3 代码lxml解析它并重新打印它 import sys from lxml import etree html document root html
  • 未捕获绘图尺寸无效,图表 api 的宽度 = null,高度 = null

    我正在使用流程图 API 来显示图表 并且成功绘制了数据 我在一页中加载大约 30 个图表 并在 ui tabs 中渲染它们 所有图表都完美渲染 页面工作正常 但我仍然收到此错误 我对所有图表使用了不同的 div id 为所有 div 指定
  • 如何在鼠标悬停(悬停)时更改图像

    如何更改图像 使用设置 img 标签 这不是 div or div
  • Java 处理 if null then new 的不同方法

    好的 我有一个关于处理空值的问题 这个问题很大程度上取决于意见 因此我将询问优点和缺点 假设我有一个可以返回 null 或 JSONArray 的函数 我总是想要一个 JSONArray 因此如果函数的结果为 null 我希望它创建一个空的
  • 按位置查找未标记的模板选项/参数/参数

    简而言之 我想从可变参数模板参数中提取各种选项 但不仅通过标签 还通过这些参数的索引 这些参数没有known标签 我喜欢 boost 中的方法 例如heap or lockfree政策 但希望使其兼容STL容器 分配器参数 Preface