Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++14

For a given class, if I want to write all the comparison operators, to avoid code duplication, I would write them something like this:

class B {
public:
    bool operator==(Type const& rhs) const {
        return as_tuple() == rhs.as_tuple();
    }

    bool operator!=(Type const& rhs) const {
        return as_tuple() != rhs.as_tuple();
    }

    // .. and same for other operators ..

private:
    auto as_tuple() const {
        return std::tie(a, b, c); // all the members
    }
};

I could implement as_tuple() there with std::tie() or I could implement it with std::forward_as_tuple(). Is there a difference? Which should I prefer?

like image 826
Alexey Starinsky Avatar asked Aug 22 '18 16:08

Alexey Starinsky


People also ask

What does std :: tie do?

std::tie. Creates a tuple of lvalue references to its arguments or instances of std::ignore.

What is std :: tuple?

Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair. If std::is_trivially_destructible<Ti>::value is true for every Ti in Types , the destructor of tuple is trivial.

Is std :: tuple a container?

The new std::array and std::tuple containers provide developers with additional ways to manage structured data efficiently.


1 Answers

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.

like image 199
Barry Avatar answered Oct 15 '22 02:10

Barry