c++ - boost::variant 与包含值的比较

标签 c++ boost

我正在尝试找到一种方法来比较 boost::variant 与基础值,而无需从该基础值构造变体。问题在“main()”函数的注释中定义 辅助问题是关于代码中定义的比较运算符。如何减少比较运算符的数量?如果 boost::variant 包含 6 种不同的类型,我是否必须定义 6!运算符能够比较两个变体?

谢谢!

#include <boost/variant.hpp>
namespace test {

    namespace Tag {
        struct Level1{ int t{ 1 }; };
        struct Level2{ int t{ 2 }; };
    }

    template <typename Kind> struct Node;

    using LevelOne = Node<Tag::Level1>;
    using LevelTwo = Node<Tag::Level2>;

    using VariantNode = boost::variant
    <
        boost::recursive_wrapper<LevelOne>,
        boost::recursive_wrapper<LevelTwo>
    >;

    typedef VariantNode* pTree;
    typedef std::vector<pTree> lstTree;

    template <typename Kind> struct Node
    {
        Node(pTree p, std::string n) : parent(p), name(n) {}
        Node(const Node& another) : name(another.name), parent(another.parent) {}
        virtual ~Node() {}
        std::string name;
        pTree parent;
    };

    bool operator == (const LevelOne& one, const LevelTwo& two) {
        return false;
    }
    bool operator == (const LevelTwo& two, const LevelOne& one) {
        return false;
    }
    bool operator == (const LevelOne& one, const LevelOne& two) {
        return true;
    }
    bool operator == (const LevelTwo& one, const LevelTwo& two) {
        return true;
    }
}

int main(int argc, char *argv[])
{
    using namespace test;
    LevelOne l1(nullptr, "level one");
    VariantNode tl2 = VariantNode(LevelTwo(nullptr, "level two"));
    VariantNode tl1 = VariantNode(LevelOne(nullptr, "level one"));
    bool rv = (tl1 == tl2); // this line compiles OK (comparing two variants)
    // comparison below does not compile, because "l1" is not a variant. 
    // Question: How can I compare "variant" value "tl1" 
    // with one of the possible content values "l1"
    bool rv1 = (tl1 == l1); 
    return 1;
}

最佳答案

以下将适用于变体中任意数量的类型:

template<typename T>
struct equality_visitor : boost::static_visitor<bool> {
    explicit constexpr equality_visitor(T const& t) noexcept : t_{ &t } { }

    template<typename U, std::enable_if_t<std::is_same<T, U>::value>* = nullptr>
    constexpr bool operator ()(U const& u) const {
        return *t_ == u;
    }

    template<typename U, std::enable_if_t<!std::is_same<T, U>::value>* = nullptr>
    constexpr bool operator ()(U const&) const {
        return false;
    }

private:
    T const* t_;
};

template<
    typename T,
    typename... Ts,
    typename = std::enable_if_t<
        boost::mpl::contains<typename boost::variant<Ts...>::types, T>::value
    >
>
bool operator ==(T const& t, boost::variant<Ts...> const& v) {
    equality_visitor<T> ev{ t };
    return v.apply_visitor(ev);
}

template<
    typename T,
    typename... Ts,
    typename = std::enable_if_t<
        boost::mpl::contains<typename boost::variant<Ts...>::types, T>::value
    >
>
bool operator !=(T const& t, boost::variant<Ts...> const& v) {
    return !(t == v);
}

要注意的是,比较必须始终采用 value == variant 的形式或 value != variant而不是 variant == valuevariant != value .这是因为 boost::variant<>本身将这些运算符定义为始终 static_assert ,而且我们没有办法让全局运营商比 variant<> 更专业的内置的。

Online Demo

关于c++ - boost::variant 与包含值的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38400344/

相关文章:

c++ - webassembly 和 get_nprocs()

c++ - 为什么我们在大括号前需要分号?

c++ - 访问存储在 vector 中的指针内的字符串时崩溃

c++ - 在并发创建线程之前读取修改的变量是否安全?

c++ - 在 C++ 中将 UpdateResource 与 StringTables 结合使用

C++将无穷大转换为最大值并将-inf转换为最小值

c++ - 使用 firebreath 插件防止浏览器关闭

c++ - 变体与继承

c++ - 尝试非常频繁地发送数据包时,boost::asio 数据包传输失败

c++ - 将反序列化 boost 为动态类型