如何检查两个元组的所有成员是否不同?

时间:2016-11-23 10:01:26

标签: c++ c++11 tuples stdtuple

如果两个比较元组中至少一个成员不同,则

std::tuple<...>::operator!=返回true。

如果两个比较元组的所有成员不同,我需要一个能返回true的函数:

template <class... Args>
bool areAllMembersDifferent( const std::tuple<Args...>& left, const std::tuple<Args...>& right )
{
    bool allDiff = true;

    // iterate through the tuples are set allDiff to false if one member's is different than other's

    return allDiff;
}

受到我在网络上发现的启发,我写了这个(改编了一个打印元组内容的功能):

template <std::size_t N, std::size_t, class = make_index_sequence<N>>
struct CheckTupleLoop;

template <std::size_t N, std::size_t J, std::size_t... Is>
struct CheckTupleLoop<N, J, index_sequence<Is...>> {
    template <class Tup>
    int operator()(bool& allDiff, const Tup &left,const Tup &right) {
        if ( std::get<J>(left) == std::get<J>(right) )
            allDiff = false;
        return 0;
    }
};

template <class... Args>
bool areAllMembersDifferent( const std::tuple<Args...>& left, const std::tuple<Args...>& right )
{
    bool allDiff = true;
    CheckTupleLoop<sizeof...(Args)>{}(allDiff,left,right);
    return allDiff;
}

但这显然不正确,因为编译器报告了我Error C2955 'CheckTupleLoop': use of class template requires template argument list

C ++ 11中任何类型的bool areAllMembersDifferent实现都是可以接受的(使用或不使用我的第一次尝试方法)。

3 个答案:

答案 0 :(得分:5)

您可以使用以下内容:

namespace detail
{

template <std::size_t ... Is, typename Tuple>
bool areAllMembersDifferent(std::index_sequence<Is...>,
                            const Tuple& left,
                            const Tuple& right)
{
    bool res = true;

    const int dummy[] = {0, (res &= std::get<Is>(left) != std::get<Is>(right), 0)...};
    static_cast<void>(dummy); // Avoid warning for unused variable
    return res;
}

}

template <typename Tuple>
bool areAllMembersDifferent(const Tuple&left, const Tuple& right)
{
    return detail::areAllMembersDifferent(
        std::make_index_sequence<std::tuple_size<Tuple>::value>(), left, right);
}

Demo

可以轻松找到std::make_index_sequence(即C ++ 14)的c ++ 11的实现

在C ++ 17中,您甚至可以将辅助函数简化为:

namespace detail
{

template <std::size_t ... Is, typename Tuple>
bool areAllMembersDifferent(std::index_sequence<Is...>,
                            const Tuple& left,
                            const Tuple& right)
{
    return (std::get<Is>(left) != std::get<Is>(right) && ...);
}

}

答案 1 :(得分:2)

您可以使用以下符合C ++ 11的解决方案来实现您的需求:

template <size_t N>
struct CompareTuples
{
    template<class... Args>
    static bool areAllMembersDifferent(const std::tuple<Args...>& left, const std::tuple<Args...>& right)
    {
        return (std::get<N>(left) != std::get<N>(right)) && CompareTuples<N-1>::areAllMembersDifferent(left, right);
    }
};

template<>
struct CompareTuples<0>
{
    template<class... Args>
    static bool areAllMembersDifferent(const std::tuple<Args...>& left, const std::tuple<Args...>& right)
    {
        return (std::get<0>(left) != std::get<0>(right));
    }
};

template<class... Args>
bool areAllMembersDifferent(const std::tuple<Args...>& left, const std::tuple<Args...>& right)
{
    return CompareTuples<std::tuple_size<std::tuple<Args...>>::value-1>::areAllMembersDifferent(left, right);
}

答案 2 :(得分:0)

Jarod42的答案很合理,但这是我的2美分:

#include <iostream>
#include <tuple>
#include <limits>

template <size_t index>
struct next_index
{
    static const size_t value = index - 1;
};

template <>
struct next_index<0>
{
    static const size_t value = 0;
};

template <class Tuple, size_t index>
bool is_same(const Tuple& left, const Tuple& right)
{
    if (index != 0)
        return is_same<Tuple, next_index<index>::value>(left, right) and std::get<index>(left) != std::get<index>(right);
    return std::get<index>(left) != std::get<index>(right);
}

template <typename Tuple>
bool areAllMembersDifferent(const Tuple& left, const Tuple& right)
{
    return is_same<Tuple, std::tuple_size<Tuple>::value - 1>(left, right);
}

int main() {
    std::cout << areAllMembersDifferent(std::make_tuple(12, '*', 4.2f), std::make_tuple(11, '#', 4.25f)) << std::endl;
    std::cout << areAllMembersDifferent(std::make_tuple(12, '*', 4.2f), std::make_tuple(11, '#', 4.2f)) << std::endl;
}