https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98675

Tobias Schlüter <tobi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tobi at gcc dot gnu.org

--- Comment #2 from Tobias Schlüter <tobi at gcc dot gnu.org> ---
Here's another testcase (derived from code in the Eigen library ) that seems to
illustrate the same issue. Please follow the compiler explorer link to see a
non-constexpr version and its gimple that makes clear that there indeed is a
lifetime issue and also clang's error message https://godbolt.org/z/4zoKaoKa5

struct B;

#define CONSTEXPR constexpr
#define CONSTEVAL consteval

struct A {
public:
    CONSTEXPR A() { m_val = 1; }
    CONSTEXPR A(const A& other) { m_val = other.m_val;}
    CONSTEXPR ~A() {};

    CONSTEXPR int val() { return m_val; }

    int m_val;

    CONSTEXPR B operator<<(int);
};

struct B {
    A& m_a;
    CONSTEXPR B(A& ref) : m_a(ref) {}
    CONSTEXPR B& operator,(int i) { m_a.m_val = i; return *this; }
    CONSTEXPR ~B() { finished(); }
    CONSTEXPR A finished() { return m_a; }
};

CONSTEXPR B A::operator<<(int i) {
    m_val = i;
    return B(*this);
}

CONSTEVAL int f()
{
    A a = (A() << 1, 2, 3, 4).finished();
    return a.val();
}

int g()
{
    return f();
}

Reply via email to