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?
std::tie. Creates a tuple of lvalue references to its arguments or instances of std::ignore.
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.
The new std::array and std::tuple containers provide developers with additional ways to manage structured data efficiently.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With