2016-07-15 57 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)。我沒有使用可變參數模板,所以我正在研究代碼。大幫忙! –