找到你要的答案

Q:Compare tuples of different sizes

Q:比较不同大小的元组

Why isn't it possible to compare two tuples of different size like this:

#include <tuple>
int main() {
  std::tuple<int, int> t1(1, 2);
  std::tuple<int> t2(1);
  if(std::tuple_size<decltype(t1)>::value == std::tuple_size<decltype(t2)>::value)
    return (t1 == t2);
  else
    return 0;
}

I know that t1==t2 is not possible. But in this example it wouldn't be executed. Is there a possibility to compare tuples of different sizes?

为什么不可以比较大小不同的两个元组这样:

#include <tuple>
int main() {
  std::tuple<int, int> t1(1, 2);
  std::tuple<int> t2(1);
  if(std::tuple_size<decltype(t1)>::value == std::tuple_size<decltype(t2)>::value)
    return (t1 == t2);
  else
    return 0;
}

我知道T1 = = T2是不可能的。但在这个例子中它不会被执行。有一种可能性,比较不同大小的元组?

answer1: 回答1:

operator== requires the tuples to be of equal lengths.

§ 20.4.2.7 [tuple.rel]:

template<class... TTypes, class... UTypes>
constexpr bool operator==(const tuple<TTypes...>& t, const tuple<UTypes...>& u);

1Requires: For all i, where 0 <= i and i < sizeof...(TTypes), get<i>(t) == get<i>(u) is a valid expression returning a type that is convertible to bool. sizeof...(TTypes) == sizeof...(UTypes).

If you want two tuples of different lengths to be considered unequal, you'd need to implement this logic yourself:

template <typename... Ts, typename... Us>
auto compare(const std::tuple<Ts...>& t1, const std::tuple<Us...>& t2)
    -> typename std::enable_if<sizeof...(Ts) == sizeof...(Us), bool>::type
{
    return t1 == t2;
}

template <typename... Ts, typename... Us>
auto compare(const std::tuple<Ts...>& t1, const std::tuple<Us...>& t2)
    -> typename std::enable_if<sizeof...(Ts) != sizeof...(Us), bool>::type
{
    return false;
}

DEMO

This way, the code comparing two tuples, t1 == t2, is instantiated only when the lengths of tuples match each other. In your scenario, a compiler is unable to compile your code, since there is no predefined operator== for such a case.

运算符= =要求元组是相等的长度。

[相关]§20.4.2.7元组:

template<class... TTypes, class... UTypes>
constexpr bool operator==(const tuple<TTypes...>& t, const tuple<UTypes...>& u);

1requires:我,0 & lt;=我& lt;sizeof…(类型),获得<;我>;(t)= =得到<;我>;(U)是一个有效的表达式返回一个类型转换为bool。sizeof…(类型)= = sizeof(UTypes)。

如果你想要两个元组长度不同的被认为是不平等的,你需要自己来实现这个逻辑:

template <typename... Ts, typename... Us>
auto compare(const std::tuple<Ts...>& t1, const std::tuple<Us...>& t2)
    -> typename std::enable_if<sizeof...(Ts) == sizeof...(Us), bool>::type
{
    return t1 == t2;
}

template <typename... Ts, typename... Us>
auto compare(const std::tuple<Ts...>& t1, const std::tuple<Us...>& t2)
    -> typename std::enable_if<sizeof...(Ts) != sizeof...(Us), bool>::type
{
    return false;
}

演示

这种方式,比较两个元组代码,T1 = = T2,实例化只有元组的长度相互匹配。在您的方案中,编译器无法编译代码,因为在这种情况下没有预定义的操作符==。

answer2: 回答2:

You may write several overloads:

template<typename ...Ts>
bool is_equal(const std::tuple<Ts...>& lhs, const std::tuple<Ts...>& rhs)
{
    return lhs == rhs;
}

template<typename ...Ts, typename... Us>
bool is_equal(const std::tuple<Ts...>&, const std::tuple<Us...>&)
{
    return false;
}

Live example

你可以写几个重载:

template<typename ...Ts>
bool is_equal(const std::tuple<Ts...>& lhs, const std::tuple<Ts...>& rhs)
{
    return lhs == rhs;
}

template<typename ...Ts, typename... Us>
bool is_equal(const std::tuple<Ts...>&, const std::tuple<Us...>&)
{
    return false;
}

活生生的例子

answer3: 回答3:

You have problem with size mismatch. Read this, maybe it can help you. Implementing comparison operators via 'tuple' and 'tie', a good idea?

You have problem with size mismatch. Read this, maybe it can help you. Implementing comparison operators via 'tuple' and 'tie', a good idea?

c++  c++11  tuples