首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Visitor对Boost变体中的类型进行分类不编译

使用Visitor对Boost变体中的类型进行分类不编译
EN

Stack Overflow用户
提问于 2017-03-02 08:06:01
回答 1查看 373关注 0票数 3

我在整个项目中都使用boost-variant。在一个场景中,我需要将boost-variant中包含的类型分类为几个类。由于我的变体中有很多类型,所以我想出了在访问者中定义几个变体的想法。这些变体基本上是类型->类映射。

下面的代码说明了我想要实现的目标。

代码语言:javascript
复制
    #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表示一段可重用的软件,它可能包含在我的程序的其他部分中。尽管如此,我还是希望这个解决方案能更接近我最初发布的源代码。

代码语言:javascript
复制
#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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-02 08:17:32

代码语言:javascript
复制
#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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42549860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档