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_qATHWpbLx3YVE_W4E2QOsyp7Dw1YoUwnYR1VcFWPKMIwVp6r9CQjn617D_upBrG0ikxOIkR0Kxe8jrnuZiJzm-MXX41pfpSbXSHmjfrrF6eiCmeFrsyrswWH7R-3P_7sLOGtAAflXOdRLm7v-Jpns42S9Zk1ULkaZYkLJtXdcrnVc6rmmcsL7Msm2lRSu2W0RXWkLXyCYILvI-ubmfvR6CWLGEsmfN5kqZzxuM8yTPGkyzPRcqbkkVpIrdC6Zj80HdiZpfBZdmtHb7Uynk3vhQoXOtWygAYEXrltVz2BcJPx9bRdsASGBT_H1Jo07n-lH3QxiDvLuye_bF4FkAvA-L_AWOgPAY>53494</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang seems to be overzealous with lifetime errors in constexpr
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
TobiSchluter
</td>
</tr>
</table>
<pre>
Consider this (derived from the Eigen library, tested with clang 13.0.0 and clang 12)
```
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();
}
```
Godbolt link here: https://godbolt.org/z/njfxf9jP5
As you can see in GCC's intermediate output, ~B is always called before ~A. Nevertheless, clang prints
source>:44:12: error: call to consteval function 'f' is not a constant expression
return f();
^
<source>:13:43: note: read of object outside its lifetime is not allowed in a constant expression
CONSTEXPR A(const A& other) { m_val = other.m_val;}
^
<source>:28:37: note: in call to 'A(A())'
CONSTEXPR A finished() { return m_a; }
^
<source>:27:22: note: in call to '&A() << 1->finished()'
CONSTEXPR ~B() { finished(); }
^
<source>:38:10: note: in call to '&A() << 1->~B()'
A a{(A() << 1, 2, 3, 4).finished()};
^
<source>:44:12: note: in call to 'f()'
return f();
^
(I think clang is wrong here, but of course gcc may be getting the destruction order wrong.)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVl2PmzoQ_TXkZVREcL544IFks1Wlq97qtrq6b5UBk3jr4Mg2m6a__s4YEkg22XbVIuKAGc-cOeM5kOvymK50bWUpDLittBDEC7yWz6KEyugdTgpYy42oQcnccHMM4hU4YR0aHKTbQqF4vYExC6MwAl6Xp4k4iJMgegiirBtnUXf6W-tMUzhYBmx5YRWzUlSyFrD6--PnL-v_Pv0DBQJ04vve3Db5N_urM3nmauirC5FBMO9i7JtcySJgnQHg0YdBxwvETNaw-4q-IGAPMEaAOPVwb4WPTFcz0MiVeenBT4f-npK97SqYrwfxyeiSl0trWTsgdz1gI1xjajhF6RH3y2nRGcVd10vQe2G40yZgKzrjBS6kWp5XvQB3qmVPNPn0nOy-8rPxVSD07E2MqHwWLPPW8eI0Mb9H1tKzfYIZdxhB9tzzsOdfEh8dP0Gc0S5_paZYiOWAV9xn0m5F2U69theuTC-rwi9r8oLBIf8Z7U-W3SrDOcUewWWe_XwXus2lTfqqhi_CUyNRkKpLtrMclhQ43fvCtUl6cNgkKAoxDYyGCT4Kr5gbpDyAx8PzNr6HjRBt7iLq_FSv-7jSnnZ8r8tcK4e6Vn8DbFFBO3Dr3N5SAeJHPDetSagNInj8gb_6qfpeJU-fpkNPmYWjbqDgNVghkEN4v8KKzS3RKcxOlJI7Abpx-8YRQbTJAMWWqwM_WlyoFMppLipthJeCED6KZ2FQOZSwlpa0qro36NF2XacbUyDoNcKdTHBAycUMhDG0aTLvFZzut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