- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
对于一个小的图书馆项目,我正在使用 boost::tuple。现在,我面临着将我通过元编程操作的“缺点列表”转回 boost::tuple<...> 类型的问题。 “肮脏”的解决方案是提供大量的部分特化
template<class T> struct id{typedef T type;};
template<class TL> struct type_list_to_tuple_type;
template<class T1>
struct type_list_to_tuple_type<
boost::tuples::cons<T1,boost::tuples::null_type>
> : id<boost::tuple<T1> > {}
template<class T1, class T2>
struct type_list_to_tuple_type<
boost::tuples::cons<T1,
boost::tuples::cons<T2,boost::tuples::null_type> >
> : id<boost::tuple<T1,T2> > {}
template<class T1, class T2, class T3>
struct type_list_to_tuple_type<
boost::tuples::cons<T1,
boost::tuples::cons<T2,
boost::tuples::cons<T3,boost::tuples::null_type> > >
> : id<boost::tuple<T1,T2,T3> > {}
...
但这很乏味且容易出错,尤其是因为我需要支持可能包含许多元素的元组。这些元组类型是通过运算符重载自动生成的。如果可能的话,我想避免写这么多的特化。
关于如何在没有任何 C++0x 功能的情况下做到这一点的想法?我想这是不可能的。但也许我忽略了一些东西。
编辑:我实际上在实验性 C++0x 支持下尝试了这个,结果发现它还不能工作:
template<class TPH>
class type_pack_holder_to_tuple_type;
template<class...Types>
class type_pack_holder_to_tuple_type<
type_pack_holder<Types...> >
: id< boost::tuple<Types...> > {};
G++ 4.5.1 说:
sorry, unimplemented: cannot expand 'Types ...' into
a fixed-length argument list
:-(
最佳答案
如果您正在进行模板元编程并且需要从类型列表转换为元组,也许您应该考虑使用 Boost.MPL和 Boost.Fusion .前者提供一组编译时容器和算法来操作时间列表,后者通过提供“混合”容器和算法在纯编译时(MPL)和纯运行时(STL)之间建立联系,可以是可以在编译时通过模板元编程进行操作,也可以在运行时作为元组进行操作。
但是,为了回答您的问题,我认为您不需要将缺点列表转换为元组,因为元组类只是为了更容易地声明元组而提供的便利。事实上,tuple 只是简单地继承了它对应的 cons 列表,例如tuple<int, float>
继承自 cons<int, cons<float, null_type> >
不添加任何成员数据或功能。所以基本上,当你声明一个 tuple
,库“创建”相应的缺点列表;因为您已经创建了自己的缺点列表,所以您不再需要元组类。
关于c++ - 将 boost::tuples::cons<...> 转回相应的 boost::tuple<...>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4014529/
我有一个包含文件名 和文件路径 的元组列表。我想找到重复的 filename(但 filepath 可能不同),即文件名相同但 filepath 可能不同的元组。 元组列表示例: file_info
我有一个像这样定义的变量 auto drum = std::make_tuple ( std::make_tuple ( 0.3f , Ex
我有一个包含几个字段的自定义结构,我想在快速 switch 语句中对其进行模式匹配,这样我就可以通过将其中一个字段与另一个字段进行比较来自定义匹配正则表达式。 例如鉴于这种结构: struct MyS
我有一种动态元组结构: template //Should only be tuples class DynamicTuple { vector data; //All data is st
这个问题在这里已经有了答案: What and When to use Tuple? [duplicate] (5 个答案) 关闭 8 年前。 我正在查看 Tuple 的在线示例,但我没有看到任何理
在我的项目中我有很多坐标要处理,在二维情况下我发现(cons x y)的构造比(list x y)快和 (vector x y)。 但是,我不知道如何将 cons 扩展到 3D 或更进一步,因为我没有
我有以下 Scala 代码: def f(x: Int, y: Int): Option[String] = x*y match { case 0 => None case n =>
我的直觉告诉我,在一般情况下,只有宏或复杂类型的体操才能解决这个问题。 Shapeless 或 Scalaz 可以在这里帮助我吗?这是 N=2 问题的具体实例,但我正在寻找的解决方案适用于所有合理的
为什么这段 Scala 代码是这样的: class Test { def foo: (Int, String) = { (123, "123") } def bar: Unit
我是 python 和 pygame 的新手,我正在尝试学习向量和类的基础知识,但在这个过程中我搞砸了,而且我在理解和修复标题中的错误消息方面苦苦挣扎。 这是我的 Vector 类的代码: impor
我正在编写一个程序来打开和读取一个 txt 文件,并在每一行中循环。将第 2 列和第 4 列中的值相乘并将其分配给第 5 列。 A 500.00 A 84.15 ? B 648.80 B 77.61
我知道还有其他几个问题提出了完全相同的问题,但是当我运行时: 导入命令 从 pyDes 导入 * def encrypt(data, password,): k = des(password,
我有一个元组列表,内容如下: >>>myList [(), (), ('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', '
给定一个 boost::tuple 和 std::tuple,你如何在它们之间进行转换? 也就是说,您将如何实现以下两个功能? template boost::tuple asBoostTuple(
我无法初始化 std::tuple来自 std::tuple 的逐元素元素兼容类型。为什么它不像 boost::tuple 那样工作? #include #include template st
我是 Storm 的新手并且我正在尝试找出如何编写一个 bolt 测试来测试子类 BaseRichBolt 中的 execute(Tuple tuple) 方法。 问题是 Tuple 似乎是不可变的,
如果我有如下元组列表: [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')] 我想删除重复的元组(在内容和内部项目顺序方面重复)以便输出为: [('a',
我编写了一个简单的脚本来模拟基于每用户平均收入 (ARPU)、利润率和客户保持客户的年数 (ltvYears) 的客户生命周期值(value) (LTV)。下面是我的脚本。它在“ltvYears =
以下是我的代码,它是一组元组:。输出:设置([(‘A’,20160129,36.44),(‘A’,20160104,41.06),(‘A’,20160201,37.37)])。如何将另一个元组(‘A’
我用以下代码编写了一个程序: import pandas as pd import numpy as np from typing import Tuple def split_data(self,
我是一名优秀的程序员,十分优秀!