2016-07-15 59 views
0

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

谢谢!

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

你'布尔运算符==(...)'可以作为模板。事实上,你不需要任何自定义模板魔法,只需返回'std :: is_same :: value'。 – lorro

+0

@lorro - 谢谢!比较现在定义为: 模板<类型名女,类型名S>布尔运算符==(常量F&F,常量S&S){ \t \t布尔issame =标准:: is_same ::值; \t \t if(!issame) \t \t \t return false; \t \t return f.name == s.name; \t} 剩下的问题 - 如何将boost :: variant与基础值进行比较,而不用从此值构建变体? –

+0

使用'get <>()'(如果我明白你的话)。 – lorro

回答

2

下面将在变异任意数量的工种:

template<typename T> 
struct equality_visitor : boost::static_visitor<bool> { 
    explicit constexpr equality_visitor(T t) : t_(std::move(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 t_; 
}; 

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

美中不足的是,比较必须始终形式value == variantvalue != variant,而不是variant == valuevariant != value。这是因为boost::variant<>本身将这些运营商定义为始终static_assert,并且我们无法让全球运营商比variant<>的内置运营商更专业化。

Online Demo

+0

@lidjarn - 谢谢!它编译并在vs2013中运行(没有constexpr)。我没有使用可变参数模板,所以我正在研究代码。大帮忙! –