1

I'm learning structured binding, I'm confused with the type of identifier about "tuple-like" binding protocol, for example:

    int a = 3;
    double b = 2.1;

    tuple<int, double&> tpl(a, b);
    auto [a1, b1] = tpl;
    auto& [a2, b2] = tpl;
    auto&& [a3, b3] = tpl;
    const auto& [a4, b4] = tpl;
    using Ta1 = decltype(a1);    // this is 'int', 
    using Tb1 = decltype(b1);    //         'double&'
    using Ta2 = decltype(a2);    //         'int'
    using Tb2 = decltype(b2);    //         'double&'
    using Ta3 = decltype(a3);    //         'int'
    using Tb3 = decltype(b3);    //         'double&'
    using Ta4 = decltype(a4);    //         'const int'
    using Tb4 = decltype(b4);    //         'double &'

first, why Tb4 is double&, but not const double&?

second, is there any difference between auto& and auto&&? when should I use & or &&?

Thanks for your answer!

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
beyondV
  • 36
  • 2
  • 1
    "why Tb4 is double&, but not const double&". Because `const` is applied to the reference type `double&`, not to a deeper level. But there's no such thing as a const reference type (not to be confused with reference-to-const), since references are essentially always `const` (bound at initialization, can't be rebound). It's similar to `const double*` vs `double* const` - in the latter, the pointer itself is `const` (can't be re-pointed), in the former the pointee is `const` (can't be changed through the pointer, but the pointer itself can be made to point to a different object). – Igor Tandetnik Jul 31 '20 at 15:14

0 Answers0