如何将 std::tuple 类型与 boost::mpl 算法一起使用?

2023-12-27

The boost::mpl算法似乎无法工作std::tuple开箱即用的类型,例如,以下内容无法编译(boost-1.46.0,g++ snapshot 2011-02-19):

#include <tuple>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/contains.hpp>

namespace mpl=boost::mpl;

typedef mpl::vector<int,float,bool> types;
static_assert(mpl::contains<types, float>::value, "vector contains bool");

typedef std::tuple<int,float,bool> types2;
// the following does not compile:
// error: no class template named ‘apply’ in ‘struct boost::mpl::contains_impl<boost::mpl::non_sequence_tag>’
static_assert(mpl::contains<types2, float>::value, "tuple contains bool");

最简单的制作方法是什么boost::mpl算法工作于std::tuple?

  • 是否有事件。boost::fusion提供此功能(因为它这样做是为了boost::tuple)?
  • 如果没有,是否有可能继续进行融合实施?boost::tuple to std::tuple easily?
  • 如果都不是,我真的必须实施吗MPL 文档中列出的所有内在元函数 http://www.boost.org/doc/libs/1_46_0/libs/mpl/doc/refmanual/intrinsic-metafunctions.html或者哪些就足够了? (文档只说“许多内在元函数提供了在大多数情况下都可以工作的默认实现”,但不清楚到底是哪些。并且一些仅提供开始和结束的测试并没有引导我到任何地方)。

如果您不想将 std::tuple 转换为 mpl 类型,您可以重载标签调度 boost mpl 使用:

#include <tuple>
#include <boost/mpl/sequence_tag.hpp>
#include <boost/mpl/pop_front_fwd.hpp>
#include <boost/mpl/push_front_fwd.hpp>
#include <boost/mpl/push_back_fwd.hpp>
#include <boost/mpl/front_fwd.hpp>
#include <boost/mpl/empty_fwd.hpp>
#include <boost/mpl/size_fwd.hpp>
#include <boost/mpl/at_fwd.hpp>
#include <boost/mpl/back_fwd.hpp>
#include <boost/mpl/clear_fwd.hpp>
#include <boost/mpl/pop_back_fwd.hpp>
#include <boost/mpl/iterator_tags.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/begin_end_fwd.hpp>


namespace boost { namespace mpl {
  namespace aux { struct std_tuple; }

  template<class ... Args>
  struct sequence_tag<std::tuple<Args...> >
  {
    typedef aux::std_tuple type;
  };

  template<>
  struct front_impl< aux::std_tuple >
  {
    template< typename Tuple > struct apply
        : std::tuple_element<0, Tuple>
    {
    };
  };

  template<>
  struct empty_impl< aux::std_tuple >
  {
      template< typename Tuple > struct apply
          : std::integral_constant<bool, std::tuple_size<Tuple>::value == 0>
      {
      };
  };

  template<>
  struct pop_front_impl< aux::std_tuple >
  {
    template< typename Tuple > struct apply;

    template< class First, class ... Types > struct apply<std::tuple<First, Types...>>
    {
      typedef std::tuple<Types...> type;
    };
  };

  template<>
  struct push_front_impl< aux::std_tuple >
  {
    template< typename Tuple, typename T > struct apply;

    template< typename T, typename ... Args >
    struct apply<std::tuple<Args...>, T>
    {
        typedef std::tuple<T, Args...> type;
    };
  };

  template<>
  struct push_back_impl< aux::std_tuple >
  {
    template< typename Tuple, typename T > struct apply;

    template< typename T, typename ... Args  >
    struct apply<std::tuple<Args...>, T>
    {
      typedef std::tuple<Args..., T> type;
    };
  };


  template<>
  struct size_impl< aux::std_tuple >
  {
    template< typename Tuple > struct apply
        : std::tuple_size<Tuple>
    {
    };
  };

  template<>
  struct at_impl< aux::std_tuple >
  {
    template< typename Tuple, typename N > struct apply
        : std::tuple_element<N::value, Tuple>
    {
    };
  };

  template<>
  struct back_impl< aux::std_tuple >
  {
    template< typename Tuple > struct apply
        : std::tuple_element<std::tuple_size<Tuple>::value - 1, Tuple>
    {
    };
  };

  template<>
  struct clear_impl< aux::std_tuple >
  {
    template< typename Tuple > struct apply
    {
      typedef std::tuple<> type;
    };
  };

  template<>
  struct pop_back_impl< aux::std_tuple >
  {
    template<int ...> struct tuple_seq {};
    template<int N, int ...S> struct tuple_gens : tuple_gens<N-1, N-1, S...> {};
    template<int ...S> struct tuple_gens<0, S...>{ typedef tuple_seq<S...> type; };

    template < class Tuple, class Index> struct apply_impl;
    template < class Tuple, int ... S> struct apply_impl<Tuple, tuple_seq<S...>>
    {
      typedef std::tuple<typename std::tuple_element<S, Tuple>::type...> type;
    };

    template< typename Tuple > struct apply : apply_impl<Tuple, typename tuple_gens<std::tuple_size<Tuple>::value - 1>::type> { };
  };

template< class ... Args >
struct tuple_iter;

  template< class ... Args >
  struct tuple_iter<std::tuple<Args...>>
  {
    typedef aux::std_tuple tag;
    typedef forward_iterator_tag category;
  };

template<>
struct begin_impl< aux::std_tuple >
{
  template< class Tuple > struct apply
  {
    typedef tuple_iter<Tuple> type;
  };
};

template<>
struct end_impl< aux::std_tuple >
{
  template< typename > struct apply
  {
    typedef tuple_iter<std::tuple<>> type;
  };
};

template< typename First, class ... Args >
struct deref< tuple_iter<std::tuple<First, Args...> > >
{
  typedef First type;
};

template< typename First, class ... Args >
struct next< tuple_iter<std::tuple<First, Args...>> >
{
  typedef tuple_iter< std::tuple<Args...> > type;
};

} }

以及相关的测试:

#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/back.hpp>
#include <boost/mpl/clear.hpp>
#include <boost/mpl/pop_back.hpp>
#include <boost/mpl/contains.hpp>


#include <boost/mpl/aux_/test.hpp>
MPL_TEST_CASE()
{
  typedef std::tuple<int, char, bool> Tuple;
  MPL_ASSERT((is_same<front<Tuple>::type, int>));
  MPL_ASSERT_RELATION( size<Tuple>::type::value, ==, 3 );
  MPL_ASSERT(( is_same< pop_front<Tuple>::type, std::tuple<char, bool> > ));
  MPL_ASSERT(( is_same< push_front<Tuple, unsigned>::type, std::tuple<unsigned, int, char, bool> > ));
  MPL_ASSERT(( is_same< push_back<Tuple, unsigned>::type, std::tuple<int, char, bool, unsigned> > ));
  MPL_ASSERT_RELATION( empty<Tuple>::type::value, ==, false );
  MPL_ASSERT(( is_same< at_c<Tuple, 0>::type, int > ));
  MPL_ASSERT(( is_same< at_c<Tuple, 1>::type, char > ));
  MPL_ASSERT(( is_same< back<Tuple>::type, bool > ));
  MPL_ASSERT(( is_same< clear<Tuple>::type, std::tuple<> > ));
  MPL_ASSERT(( is_same< pop_back<Tuple>::type, std::tuple<int, char> > ));
  MPL_ASSERT(( contains<Tuple, int> ));
  MPL_ASSERT(( contains<Tuple, char> ));
  MPL_ASSERT(( contains<Tuple, bool> ));
  MPL_ASSERT_NOT(( contains<Tuple, unsigned> ));

}

我用 gcc 4.7.2 和 clang 3.2 对此进行了测试。它应该包含使用 mpl 中的任何内容所需的一切(实际上比它需要的多一点)。您可以将元组视为 mpl::list (前向迭代器编译类型)。所以,为了让它发挥得更好,你应该实现 boost::mpl::list 的功能。快速浏览一下 boost/mpl/list/aux_ 目录: begin_end.hppempty.hppiterator.hpppop_front.hpppush_back.hppsize.hpp 清除.hpp front.hpp push_front.hpp tag.hpp 是需要实施的相关文件。

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

如何将 std::tuple 类型与 boost::mpl 算法一起使用? 的相关文章

  • 检查两个数是否是彼此的排列?

    给定两个数字 a b 使得 1 例如 123 是 312 的有效排列 我也不想对数字中的数字进行排序 如果您指的是数字的字符 例如 1927 和 9721 则 至少 有几种方法 如果允许排序 一种方法是简单地sprintf将它们放入两个缓冲
  • 无法使用已与其底层 RCW 分离的 COM 对象。在 oledb 中

    我收到此错误 但我不知道我做错了什么 下面的代码在backrgroundworker中 将异常详细信息复制到剪贴板 System Runtime InteropServices InvalidComObjectException 未处理 通
  • 访问私人成员[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 通过将类的私有成员转换为 void 指针 然后转换为结构来访问类的私有成员是否合适 我认为我无权修改包含我需要访问的数据成员的类 如果不道德 我
  • 获取按下的按钮的返回值

    我有一个在特定事件中弹出的表单 它从数组中提取按钮并将标签值设置为特定值 因此 如果您要按下或单击此按钮 该函数应返回标签值 我怎样才能做到这一点 我如何知道点击了哪个按钮 此时代码返回 DialogResult 但我想从函数返回 Tag
  • 将数组向左或向右旋转一定数量的位置,复杂度为 o(n)

    我想编写一个程序 根据用户的输入 正 gt 负 include
  • 在 Visual Studio 2008 上设置预调试事件

    我想在 Visual Studio 中开始调试程序之前运行一个任务 我每次调试程序时都需要运行此任务 因此构建后事件还不够好 我查看了设置的 调试 选项卡 但没有这样的选项 有什么办法可以做到这一点吗 你唯一可以尝试的 IMO 就是尝试Co
  • C#:如何防止主窗体过早显示

    在我的 main 方法中 我像往常一样启动主窗体 Application EnableVisualStyles Application SetCompatibleTextRenderingDefault false Application
  • Json.NET - 反序列化接口属性引发错误“类型是接口或抽象类,无法实例化”

    我有一个类 其属性是接口 public class Foo public int Number get set public ISomething Thing get set 尝试反序列化Foo使用 Json NET 的类给我一条错误消息
  • Qt moc 在头文件中实现?

    是否可以告诉 Qt MOC 我想声明该类并在单个文件中实现它 而不是将它们拆分为 h 和 cpp 文件 如果要在 cpp 文件中声明并实现 QObject 子类 则必须手动包含 moc 文件 例如 文件main cpp struct Sub
  • Web API - 访问 DbContext 类中的 HttpContext

    在我的 C Web API 应用程序中 我添加了CreatedDate and CreatedBy所有表中的列 现在 每当在任何表中添加新记录时 我想填充这些列 为此目的我已经覆盖SaveChanges and SaveChangesAsy
  • 指针减法混乱

    当我们从另一个指针中减去一个指针时 差值不等于它们相距多少字节 而是等于它们相距多少个整数 如果指向整数 为什么这样 这个想法是你指向内存块 06 07 08 09 10 11 mem 18 24 17 53 7 14 data 如果你有i
  • 使用 System.Text.Json 即时格式化 JSON 流

    我有一个未缩进的 Json 字符串 例如 hash 123 id 456 我想缩进字符串并将其序列化为 JSON 文件 天真地 我可以使用缩进字符串Newtonsoft如下 using Newtonsoft Json Linq JToken
  • 如何返回 json 结果并将 unicode 字符转义为 \u1234

    我正在实现一个返回 json 结果的方法 例如 public JsonResult MethodName Guid key var result ApiHelper GetData key Data is stored in db as v
  • for循环中计数器变量的范围是多少?

    我在 Visual Studio 2008 中收到以下错误 Error 1 A local variable named i cannot be declared in this scope because it would give a
  • 从库中捕获主线程 SynchronizationContext 或 Dispatcher

    我有一个 C 库 希望能够将工作发送 发布到 主 ui 线程 如果存在 该库可供以下人员使用 一个winforms应用程序 本机应用程序 带 UI 控制台应用程序 没有 UI 在库中 我想在初始化期间捕获一些东西 Synchronizati
  • Discord.net 无法在 Linux 上运行

    我正在尝试让在 Linux VPS 上运行的 Discord net 中编码的不和谐机器人 我通过单声道运行 但我不断收到此错误 Unhandled Exception System Exception Connection lost at
  • 控制到达非 void 函数末尾 -wreturn-type

    这是查找四个数字中的最大值的代码 include
  • 将文本叠加在图像背景上并转换为 PDF

    使用 NET 我想以编程方式创建一个 PDF 它仅包含一个背景图像 其上有两个具有不同字体和位置的标签 我已阅读过有关现有 PDF 库的信息 但不知道 如果适用 哪一个对于如此简单的任务来说最简单 有人愿意指导我吗 P D 我不想使用生成的
  • C - 直接从键盘缓冲区读取

    这是C语言中的一个问题 如何直接读取键盘缓冲区中的数据 我想直接访问数据并将其存储在变量中 变量应该是什么数据类型 我需要它用于我们研究所目前正在开发的操作系统 它被称为 ICS OS 我不太清楚具体细节 它在 x86 32 位机器上运行
  • 限制C#中的并行线程数

    我正在编写一个 C 程序来生成并通过 FTP 上传 50 万个文件 我想并行处理4个文件 因为机器有4个核心 文件生成需要更长的时间 是否可以将以下 Powershell 示例转换为 C 或者是否有更好的框架 例如 C 中的 Actor 框

随机推荐