如何将 boost::mpl::vector 转换为另一个 boost::mpl::vector
How to transform boost::mpl::vector into another boost::mpl::vector?
假设我有一个boost::mpl::vector
" myvec
",例如定义如下:
using myvec = boost::mpl::vector<int, double, double>;
现在我想定义另一种类型myvecex,它将每个myvec
成员转换为带有添加字符串的std::tuple
。我想得到一个定义如下的类型:
using myvecex = boost::mpl::vector<std::tuple<int, std::string>,
std::tuple<double, std::string>,
std::tuple<double, std::string> >;
但我不想重复自己并命名所有向量成员。相反,我想定义some_smart_template
模板类型,我将以某种方式将每个成员类型转换为元组的逻辑放入其中。
using myvecex2 = some_smart_template<myvec>;
static_assert(std::is_same<myvecex, myvecex2>::value);
在C++中可行吗?
Boost.MPL不仅为您提供容器,还为您提供这些容器的算法。在这种情况下,您想要的是 transform
:
template<
typename Sequence
, typename Op
, typename In = unspecified
>
struct transform
{
typedef unspecified type;
};
语义是你给它一个序列,MPL称之为Lambda表达式,然后你得到另一个序列。具体说来:
using B = mpl::transform<A,
std::tuple<mpl::_1, std::string>
>::type;
或者至少如果apply
支持像 std::tuple
这样的可变参数类模板,这将起作用。所以你只需要编写一个元函数类的操作:
struct tuple_of_strings {
template <class T>
struct apply {
using type = std::tuple<T, std::string>;
};
};
using B = mpl::transform<A, tuple_of_strings>::type;
或元函数:
template <class T>
struct tuple_of_strings {
using type = std::tuple<T, std::string>;
};
using B = mpl::transform<A, tuple_of_strings<_1>>::type;
相关文章:
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- boost::container::vector 无法用谷物序列化?
- 如何从 boost::container::vector<std::string>::iterator 访问索引和对象?
- Boost.interprocess Vector 作为类成员
- 在 boost::<double>asio::buffer 中使用像 std::vector<std::complex> 这样的参数
- 将 std::vector<boost::optional<double>> 转换为 std::vector<double>
- 使用 boost::序列化序列化 std::vector of unique_ptr 在 Linux 上失败
- boost::spirit::x3 phrase_parse 在进入 Vector 之前进行算术运算
- 有没有一种优雅的方法可以从 std::vector 实例化 boost::array
- 使用 boost zlib 从 std::vector 中的文件中读取压缩的 zraw 数据
- boost::d ynamic_bitset 与 std::vector 的结果不一致<bool>?
- 如何使用 boost asio 通过 UDP 套接字发送无符号字符的 std::vector?
- Boost::p roperty_tree:在XML解析器中使用std::vector<>在一个键中存储多个值
- Copy std::vector to boost::interprocess::vector
- C++类似于 std::vector / boost::array 的文件内存映射容器
- 为什么我的结构不能有 boost::variant 类型的成员,但可以有 vector<boost::variant> 类型的成员?
- 使用 std::vector<boost::shared_ptr<Base_Class>> 或 boost::p tr_vector 的性能注意事项是什么<Base>
- 如何将 std::vector<boost::shared_ptr<T>> 复制到 std::list<T>
- 使用 std::vector<boost::function> with boost::bind
- 解析为 vector<boost::string_view> 使用 boost::spirit::x3