Why std :: tuple decomposes into rvalue links

Why does std :: tuple break up into rvalue links?

#include <tuple> template <typename, typename> struct same_type; template <typename T> struct same_type<T, T> {}; void foo() { std::tuple tuple(1, 'a', 2.3, true); auto[i, c, d, b] = tuple; same_type<decltype(i), int &&>{}; same_type<decltype(c), char &&>{}; same_type<decltype(d), double &&>{}; same_type<decltype(b), bool &&>{}; } 

This compiles without errors using the gcc trunk. I would expect simple types like

 same_type<decltype(i), int>{}; 

Real time example

+6
source share
1 answer

GCC error. decltype , applied to a structured binding, returns the reference type, which for an instance of type tuple is the exact type returned by std::tuple_element . In other words, the language is quite complicated here to hide the fact that these are actually links.

[dcl.type.simple] / 4 :

For expression e type indicated by decltype(e) is defined as follows:

  • If e is an un-spaced id expression, calling structured binding ([dcl.struct.bind]), decltype(e) is the reference type specified in the structured binding declaration specification;
  • [...]

[dcl.struct.bind] / 3 :

Otherwise, if the expression std::tuple_size<E>::value is a well-formed integral constant expression [...] Given the type Ti denoted by std::tuple_element<i, E>::type , each vi is a variable of type " reference to Ti "initialized by the initializer, where the reference is an lvalue reference if the initializer is an lvalue and rvalue otherwise; reference type Ti .

+9
source

Source: https://habr.com/ru/post/1012333/


All Articles