gcc 未给出的 Clang 错误"attempted to construct a reference element in a tuple with an rvalue"

Clang error "attempted to construct a reference element in a tuple with an rvalue" not given by gcc

本文关键字:element reference in tuple rvalue an with construct to Clang attempted      更新时间:2023-10-16

我已经编写了以下(相对)简单的std::tuple zip函数实现(类似于Python的zip),具有完美的转发:

template <size_t I, size_t N>
struct tuple_zip_helper {
  template <typename... Tuples>
  constexpr auto operator()(Tuples&&... tuples) const {
    return tuple_cat(
      make_tuple( forward_as_tuple(get<I>(forward<Tuples>(tuples))...) ),
      tuple_zip_helper<I+1, N>()(forward<Tuples>(tuples)...)
    );
  }
};
template <size_t N>
struct tuple_zip_helper<N, N> {
  template <typename... Tuples>
  constexpr auto operator()(Tuples&&...) const {
    return forward_as_tuple();
  }
};
namespace std {
  // Extend min to handle single argument case, for generality
  template <typename T>
  constexpr decltype(auto) min(T&& val) { return forward<T>(val); }
}
template <typename... Tuples>
auto tuple_zip(Tuples&&... tuples) {
  static constexpr size_t min_size = min(tuple_size<decay_t<Tuples>>::value...);
  return tuple_zip_helper<0, min_size>()( forward<Tuples>(tuples)... );
}

这似乎适用于两个或更多元组,即使在混合左值和右值时,甚至当我使用BlabberMouth类检查虚假副本和移动时:

template <typename Tuple>
void f(Tuple&& tup) {
  cout << get<0>(get<0>(tup)).data << endl;
}
struct Blabbermouth {
  Blabbermouth(string const& str) : data(str) { }
  Blabbermouth(Blabbermouth const& other) : data(other.data) { cout << data << " copied" << endl; }
  Blabbermouth(Blabbermouth&& other) : data(move(other.data)) { cout << data << " moved" << endl; }
  string data;
};
int main(int argc, char** argv) {
  Blabbermouth x("hello ");
  // prints "hello"
  f(tuple_zip(
      forward_as_tuple(x, 2),
      forward_as_tuple(Blabbermouth("world"), 3)
  ));
}

当我只给它一个tuple,而不混合左值和右值时,它也很好(clang-3.9,clang的早期版本也会阻塞它):

f(tuple_zip( forward_as_tuple(Blabbermouth("world"), 3) ));  // prints "world"

然而,当我混合左值和右值时,只给出一个元组,clang会对noexecpt规范中的某些内容感到奇怪(但gcc很好,甚至可以正确运行):

auto x = BlabberMouth("hello");
f(tuple_zip( forward_as_tuple(x, 3) ));  // clang freaks out, gcc okay

实时演示

我做错了什么(如果有的话)?gcc应该抱怨,还是clang不应该抱怨?我的代码是否有任何我只是"幸运"的悬挂引用,这就是clang反对的原因?我应该采取不同的做法吗?如果clang是这里的错误,有人能提出一个解决办法吗?(和/或将我链接到错误报告?)


更新

@Oktalist提供了一个更简单的例子来说明同样的问题:

struct foo {};
int main(int argc, char** argv)
{
  foo f;
  std::tuple<foo&> t(f);
  std::tuple_cat(std::make_tuple(t), std::make_tuple());
}

(我也考虑过让我的例子变得更简单,但我不确定我所做的是否与此完全类似,主要是因为我不完全理解完美转发如何与auto/decltype(auto)返回值、返回值优化(RVO)、std::getstd::make_tuple相互作用,所以我想确保我没有做其他愚蠢的事情。)

该错误是由对tuple_cat的调用引起的(尽管不完全在其中;它似乎在libc++的tuple的构造函数和SFINAE条件的拜占庭迷宫中有些混乱),因此解决方法是避免使用它。

无论如何,做递归tuple_cats是没有意义的;一个包扩展(或两个)就可以了。

template<size_t I, typename... Tuples>
constexpr auto tuple_zip_one(Tuples&&... tuples) {
    return forward_as_tuple(get<I>(forward<Tuples>(tuples))...);
}
template<size_t...Is, typename... Tuples>
constexpr auto tuple_zip_helper(index_sequence<Is...>, Tuples&&... tuples) {
    return make_tuple(tuple_zip_one<Is>(forward<Tuples>(tuples)...)...);
}
template <typename... Tuples>
auto tuple_zip(Tuples&&... tuples) {
  static constexpr size_t min_size = min({tuple_size<decay_t<Tuples>>::value...});
  return tuple_zip_helper( make_index_sequence<min_size>(), forward<Tuples>(tuples)... );
}

我随意删除了导致min过载的UB,并简单地使用标准的initializer_list版本。

演示。

相关文章: