使用访问者中的Boost变体中的类型不编译
Classify Types in Boost Variant using Visitor does not compile
我在整个项目中都使用boost-variant
。在一种情况下,我需要将boost-variant
中包含的类型分类为几个类。由于我的变体中有很多类型,因此我想到了在访问者中定义几个变体的想法。这些变体基本上是类型 ->类映射。
下面的代码说明了我想实现的目标。
#include <iostream>
#include <boost/variant.hpp>
#include <string>
enum class Type {
Integer,
Float,
NonNumeric
};
struct IsNum : public boost::static_visitor<Type> {
typedef boost::variant<int, size_t> IntegerTypes;
typedef boost::variant<float, double> FloatTypes;
typedef boost::variant<std::string> NonNumericTypes;
result_type operator()(const IntegerTypes& t) const {
return Type::Integer;
}
result_type operator()(const FloatTypes& t) const {
return Type::Float;
}
result_type operator()(const NonNumericTypes& t) const {
return Type::NonNumeric;
}
};
int main() {
boost::variant<int, std::string, double> value;
value = 5;
IsNum visitor;
auto result = value.apply_visitor(visitor);
}
不幸的是,代码不会编译。MSVC以编译器错误C3066
结束。使用此参数调用此类型的对象有不同的可能性?它可能是三个operator()
功能之一。
,但基本上我只能将5转换为变体类型IntegerTypes
。
这种问题的解决方案是什么?
自己的解决方案
尝试使用boost-mpl
后,我到达了此解决方案。该函数Contains
代表可重复使用的软件,可以包含在我程序的其他部分中。我仍然希望该解决方案更接近我的原始发布的源代码。
#include <iostream>
#include <boost/variant.hpp>
#include <string>
#include <boost/mpl/contains.hpp>
enum class Type {
Integer,
Float,
NonNumeric
};
template<typename V, typename T>
using ContainsImpl = typename boost::mpl::contains<typename V::types, T>::type;
template<typename V, typename T>
bool Contains(const T&) {
return ContainsImpl<V, T>::value;
}
struct IsNum : public boost::static_visitor<Type> {
typedef boost::variant<int, size_t> IntegerTypes;
typedef boost::variant<float, double> FloatTypes;
typedef boost::variant<std::string> NonNumericTypes;
template<typename T>
result_type operator()(const T& t) const {
if (Contains<IntegerTypes>(t)) {
return Type::Integer;
} else if (Contains<FloatTypes>(t)) {
return Type::Float;
} else if (Contains<NonNumericTypes>(t)) {
return Type::NonNumeric;
}
return Type::NonNumeric;
}
};
int main() {
boost::variant<int, std::string, double> value;
value = 5.;
IsNum visitor;
auto result = value.apply_visitor(visitor);
if (result == Type::Integer) {
std::cout << "Integer" << std::endl;
}
if (result == Type::Float) {
std::cout << "Float" << std::endl;
}
if (result == Type::NonNumeric) {
std::cout << "Non Numeric" << std::endl;
}
}
#include <string>
#include <boost/variant.hpp>
#include <boost/mpl/list.hpp>
#include <boost/mpl/contains.hpp>
#include <boost/type_traits.hpp>
enum class Type
{
Integer,
Float,
NonNumeric
};
struct IsNum : public boost::static_visitor<Type>
{
typedef boost::mpl::list<int, size_t> IntegerTypes;
typedef boost::mpl::list<float, double> FloatTypes;
typedef boost::mpl::list<std::string> NonNumericTypes;
template <typename T>
typename boost::enable_if<boost::mpl::contains<IntegerTypes, T>, result_type>::type
operator()(const T& t) const
{
return Type::Integer;
}
template <typename T>
typename boost::enable_if<boost::mpl::contains<FloatTypes, T>, result_type>::type
operator()(const T& t) const
{
return Type::Float;
}
template <typename T>
typename boost::enable_if<boost::mpl::contains<NonNumericTypes, T>, result_type>::type
operator()(const T& t) const
{
return Type::NonNumeric;
}
};
demo
相关文章:
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 使用简单类型列表实现的指数编译时间.为什么
- 编译标准库类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- 为什么 std::make_shared 无法编译带有已删除运算符 new 的类型?
- 标量类型的特征模板无法编译固定大小的子矩阵操作
- 在其他容器中使用 boost::container::static_vector 时,GCC 编译错误"将'const s'绑定到类型's&'的引用丢弃限定符"
- 由于类型从 C 转换为 C++,无法编译错误 C2440
- 通过编译时值推断整数的类型
- 基于派生类型的编译时行为分支
- C++ 编译错误:gnu_printf是无法识别的格式函数类型
- 将函数类型作为模板参数传递不会编译
- 枚举类的基础类型别名为整型类型(编译错误)
- 无法将类型转换为类型*-C++编译错误
- 返回仅移动类型编译,即使复制构造函数不可用
- 如何根据模板中的类型编译函数
- 无法使用模板化类型编译 va_arg() 调用,因为模板参数中的逗号