Search code examples
c++iteratorc++17structured-bindingsstd-tie

Why structured binding fails with 'std::tie' d objects?


I was curious to do this asked problem in the following way:

#include <iostream>
#include <set>
#include <iterator>
#include <array>
#include <tuple>
#include <type_traits>

int main()
{
    const std::set<int> s{ 0, 1, 2, 3, 4, 5, 6, 7, 8 };
    auto iter = s.find(5);
    using IterType = decltype(iter);

    // using `std::array` works fine!
    const auto& [pv1, nxt1] = std::array<IterType, 2>{std::prev(iter), std::next(iter)};
    std::cout <<"using std::array<IterType, 2> :"<< *pv1 << " " << *nxt1 << '\n'; // prints: 4 6

    // using ` std::make_tuple` works fine!
    const auto& [pv2, nxt2] = std::make_tuple(std::prev(iter), std::next(iter));
    std::cout << "using std::make_tuple :" << *pv2 << " " << *pv2 << '\n';        // prints: 4 6

    // using `std::tie` deduction happens in MSVC, but not in GCC and Clang
    const auto& [pv3, nxt3] = std::tie(std::prev(iter), std::next(iter));
    // following is an assertion failure in MSVC with /O2  /std:c++17
    std::cout << "using std::tie :" << *pv3 << " " << *nxt3<< '\n';
}

I std::tie d the returned iterators of std::prev and std::next, and allowed the structured binding to do the auto deduction.

const auto& [pv3, nxt3] = std::tie(std::prev(iter), std::next(iter));

looks like the only compiler it allows is MSVC v19.14 with /O2 /std:c++17! The GCC 9.1 and clang 8.0 does not agree with that. See in the on-line compiler: https://godbolt.org/z/DTb_OZ

GCC says:

<source>:23:28: error: no matching function for call to 'tie'
        const auto& [pv3, nxt3] = std::tie(std::prev(iter), std::next(iter));
                                  ^~~~~~~~
/opt/compiler-explorer/gcc-8.3.0/lib/gcc/x86_64-linux-gnu/8.3.0/../../../../include/c++/8.3.0/tuple:1605:5: note: candidate function [with _Elements = <std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>>] not viable: expects an l-value for 1st argument
    tie(_Elements&... __args) noexcept
^

Clang says:

<source>: In function 'int main()':
<source>:23:46: error: cannot bind non-const lvalue reference of type 'std::_Rb_tree_const_iterator<int>&' to an rvalue of type 'std::_Rb_tree_const_iterator<int>'
   23 |  const auto& [pv3, nxt3] = std::tie(std::prev(iter), std::next(iter));
      |                                     ~~~~~~~~~^~~~~~
In file included from <source>:5:
/opt/compiler-explorer/gcc-9.1.0/include/c++/9.1.0/tuple:1611:19: note:   initializing argument 1 of 'constexpr std::tuple<_Elements& ...> std::tie(_Elements& ...) [with _Elements = {std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>}]'
 1611 |     tie(_Elements&... __args) noexcept
      |         ~~~~~~~~~~^~~~~~~~~~

Looking to the example given in cppreference.com is MSVC is correct? or Who is(are) right here and why?

Interestingly while running the

std::cout << "using std::tie :" << *pv3 << " " << *nxt3<< '\n';

gives me

enter image description here

(in MSVS 2019, /std:c++17)


Solution

  • Per [tuple.creation]/tie:

    template<class... TTypes>
      constexpr tuple<TTypes&...> tie(TTypes&... t) noexcept;
    

    Here, the arguments are non-const lvalue references. std::prev(iter) and std::next(iter) cannot be bound to lvalue references, so the code should be rejected. The reason why MSVC accepts this is explained in a comment:

    Enable /Za flag to compiler, then the code will be refused. MVSC [sic] has extenstion which allows to bind temps to Lvalue reference. tie gets Lvalue references, but prev, next returns temporary. – rafix07 2019-07-25 07:52:58Z