jeudi 4 juillet 2019

std::tie fails with "cannot bind non-const lvalue reference" when passed value from a function call

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?

Demo: https://godbolt.org/z/cWbQC0

Aucun commentaire:

Enregistrer un commentaire