I expected this code to work, but it does not compile:
#include <tuple>
struct S
{
int x = 0;
int y() const { return 1; }
};
bool f(const S& a, const S& b)
{
return std::tie(a.x, a.y()) < std::tie(b.x, b.y());
}
GCC 9 says:
error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
return std::tie(a.x, a.y()) < std::tie(b.x, b.y());
~~~^~
What's wrong with the code, how can it be fixed, and why? I'm trying to write a concise comparison function, and usually std::tie
supports that (indeed this is the textbook use case for std::tie
).