What is the difference between std::tie and std::forward_as_tuple

Let's just look at the signatures. std::tie() is:

template< class... Types >
constexpr tuple<Types&...> tie( Types&... args ) noexcept;

whereas std::forward_as_tuple() is:

template< class... Types >
constexpr tuple<Types&&...> forward_as_tuple( Types&&... args ) noexcept;

The only difference is that the former accepts only lvalues whereas the latter accepts lvalues and rvalues. If all of your inputs are lvalues, as they are in your use-case, they are exactly equivalent.

std::tie() is largely intended as the left-hand side of assignment (e.g. std::tie(a, b) = foo; to unpack a pair), whereas std::forward_as_tuple() is largely intended to pass things around in functions to avoid copies. But they can both be used to solve this problem. tie is obviously quite a bit shorter, and arguably more well-known (the cppreference example for tie uses it to implement operator<), so that would get my vote.

Tags:

C++

C++14