https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114225
Bug ID: 114225
Summary: False positive -Werror=dangling-reference
Product: gcc
Version: 13.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: asharafutdinov at adalisk dot com
Target Milestone: ---
#include <variant>
struct A
{
int i;
};
struct Getter
{
Getter(const A & a) : var(&a) {}
const int & operator ()(const A * a) const { return a->i; }
const int & value() const { return std::visit(*this, var); }
std::variant<const A *> var;
};
int main()
{
A a { .i = 1 };
const auto & i = Getter{ a }.value();
return i;
}
The code above produces an incorrect dangling reference warning with g++-13
-std=c++20 -Wall -Wextra -pedantic-errors:
<source>: In function 'int main()':
<source>:19:18: warning: possibly dangling reference to a temporary
[-Wdangling-reference]
19 | const auto & i = Getter{ a }.value();
| ^
<source>:19:39: note: the temporary was destroyed at the end of the full
expression 'Getter(a).Getter::value()'
19 | const auto & i = Getter{ a }.value();
| ~~~~~~~~~~~~~~~~~^~