如何简化由make_variant_over生成的类型
How to simplify type generated by make_variant_over
Boost变体具有称为make_variant_over
的函数,该函数采用MPL序列(例如list<A, B, C>
),并从这些类型中产生一个变体。
但是,如果仔细看,生成的类型从来都不是简单的variant<A, B, C>
。
例如,在此代码中,
#include<boost/variant.hpp>
int main(){
using List = boost::mpl::list<double, int>;
using Variant = boost::make_variant_over<List>::type;
}
Variant
是 boost::variant<boost::detail::variant::over_sequence<boost::mpl::list<double, int, mpl_::na, ...> >>
。
看起来它可以与boost::variant<double, int>
互换使用,但不是相同的类型。充其量在阅读编译器错误时会产生混乱,而在最坏的情况下,它可能很难实现依赖于参数的确切类型的某些功能。
是否有一种方法可以强制生产的变体类型中的简化?
使用 boost::mpl::fold
得到它。由于无法实例化空模板变体,因此必须小心递归。
可以做到,但是我们可能没有做任何支持,因为boost::variant<T1, T2,...>
可能仍在boost::variant<...variant::over_sequence<T1, T2,...>>
中实现。
真正的力量可以是可以使用简化的类型结构使变体类型独特。
namespace detail{
template <typename TList, typename T> struct ExtendTList;
template<typename T>
struct ExtendTList<boost::variant<void>, T>{
using type = boost::variant<T>;
};
template<typename T, typename... Ts>
struct ExtendTList<boost::variant<Ts...>, T>{
using type = boost::variant<Ts..., T>;
};
}
template<class Seq>
using make_simple_variant_over = typename boost::mpl::fold<
typename boost::mpl::fold<
Seq,
boost::mpl::set<>,
boost::mpl::insert<boost::mpl::_1, boost::mpl::_2>
>::type,
boost::variant<void>,
detail::ExtendTList<boost::mpl::_1, boost::mpl::_2>
>;
...
using variant_type = make_simple_variant_over<boost::mpl::vector<int, int, long>>::type;
variant_type
现在正好是boost::variant<int, long>
。
(并且不是boost::variant<boost::detail::variant::over_sequence<boost::mpl::list<int, long, mpl_::na, ...> >>
也不是boost::variant<boost::detail::variant::over_sequence<boost::mpl::list<int, int, long, mpl_::na, ...> >>
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 类中的字符串不命名类型