2017-03-02 66 views
3

我在整个项目中都使用boost-variant。在一种情况下,我需要将包含在我的boost-variant中的类型分为几类。由于我的变体中有很多类型,我想到了在访问者中定义几个变体的想法。这些变体基本上是type-> class映射。使用访问者的Boost变体中的分类类型无法编译

下面的代码说明了我想达到的目的。

#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; 
    } 
} 

回答

2
#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

+0

酷。我现在也衍生出这个解决方案。这似乎是唯一可行的方法。但读它可能会让你头痛。 :-)我也会发布我的解决方案。非常感谢。 – Aleph0

+1

@FrankSimon更多的选择包括:标签调度和为每种类型添加单独的过载 –

+0

不知道标签调度是什么。我会认为我必须学习更多。 :-) – Aleph0