3
  1. As the title states, is there a specific reason why there is no (implicit) conversion from std::tuple<Ts...>& to std::tuple<Ts&...>? In contrast, the tuple implementation of EASTL provides this conversion.
#include <EASTL/tuple.h>

#include <tuple>

#include <type_traits>

int main()
{
  using TupleRef = std::tuple<int, float>&;
  using RefTuple = std::tuple<int&, float&>;

  using EATupleRef = eastl::tuple<int, float>&;
  using EARefTuple = eastl::tuple<int&, float&>;

  // static_assert(std::is_convertible_v<TupleRef, RefTuple>); // fails to compile
  static_assert(std::is_convertible_v<EATupleRef, EARefTuple>);

  return 0;
}
  1. What do I have to change/add, if I reimplemented the STL tuple implementation?

Here is a link to godbolt show-casing the problem: https://godbolt.org/z/zqfrETKEz

PS: I used the c++17 flag in godbolt since EASTL does not compile with the c++20 flag, but I am also interested in a c++20 solution.

cpplearner
  • 13,776
  • 2
  • 47
  • 72
serkan.tuerker
  • 1,681
  • 10
  • 20
  • Probably because this is quite a special case that noone working on the standard proposal that originally introduced tuples considered and because no proposal that adds this behavior has been made and/or considered important enough since. – Salvage Feb 24 '22 at 13:28

1 Answers1

6

There will be in , as a result of the zip paper (P2321).


Generally speaking, it is typical for overload sets to have one overload taking T const& and another overload taking T&&, it's not often that T& is needed as a distinct 3rd option (and T const&& even less so). This is one of those cases that originally had just the two, but then really does need at least the 3rd.

I'm not sure if you had a particular motivation for needing tuple<int>& to be convertible to tuple<int&>, but zip needs that to work, which is why it changed.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • Thank you for the helpful answer Barry. Yes, I am implementing a `tuple_vector` container, where `value_type == tuple` and `reference == tuple`, when usually `reference == value_type&`. In order to use `tuple_vector` with the `ranges` library and `views`, `std::input_iterator::iterator>` and then `std::indirectly_readable` has to be satisfied, so `tuple&` needs to be convertible to `tuple` (see `std::common_reference_with`). – serkan.tuerker Feb 24 '22 at 23:08
  • Is there an implementation of the `tuple` changes available somewhere Barry? – serkan.tuerker Feb 24 '22 at 23:21