Search code examples
c++c++17structured-bindings

Why does auto seemingly deduce a reference when using structured bindings?


Consider the following code that uses structured bindings from C++17:

int a = 0, b = 0;
auto [x, y] = std::tie(a, b);
y = 1;
std::cout << a << ' ' << b << '\n';

Since I used auto, I would expect the code to print 0 0 as y should be a copy. However, it prints 0 1. Why? I thought that a bare auto never deduces a reference.


Solution

  • As cppreference notes, the portion of the declaration preceding [ (i.e. auto in your case) does not apply to the introduced identifiers. Instead, it applies to a hidden variable that is created by the compiler under the covers. Your structured-binding declaration

    auto [x, y] = std::tie(a, b);
    

    is roughly equivalent to

    auto e = std::tie(a, b);
    decltype(std::get<0>(e)) x = std::get<0>(e);
    decltype(std::get<1>(e)) y = std::get<1>(e);
    

    As you can see, auto is applied to the hidden variable e and not to the declaration of x and y. The type of e is std::tuple<int&, int&>, and decltype(std::get<1>(e)) gives you int&.