[Bug c++/114220] False positive warning: possibly dangling reference to a temporary [-Wdangling-reference]
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114220 Xi Ruoyao changed: What|Removed |Added CC||asharafutdinov at adalisk dot com --- Comment #5 from Xi Ruoyao --- *** Bug 114225 has been marked as a duplicate of this bug. ***
[Bug c++/114220] False positive warning: possibly dangling reference to a temporary [-Wdangling-reference]
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114220 --- Comment #4 from Andrew Pinski --- (In reply to Дилян Палаузов from comment #3) > > The warning is designed this way explictly because you are returning a > > reference and taking a reference as an argument and in the case of b2, the > > tempory is `std::string("u")` . > > > In GCC 14+ (since r14-9263-gc7607c4cf18986), you can add > > [[gnu::no_dangling]] to the z2 function definition to mark it as not > > returning a dangling reference (from the arguments). and the warning goes > > away. > > If the declaration and definitions are in different files, do I have to add > [[gnu::no_dangling]] only to the function declaration ? Yes only on the declaration is needed. > > That said, is the warning triggered only based on the function declaration > (accepting as parameter a reference to temporary and returning a reference), > when on the function invocation the parameter is indeed a reference to a > temporary? Yes that is correct, it is based on the function declaration only and the warning does not look into the function to see it does not return the a referenced based on the reference being passed.
[Bug c++/114220] False positive warning: possibly dangling reference to a temporary [-Wdangling-reference]
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114220 --- Comment #3 from Дилян Палаузов --- > The warning is designed this way explictly because you are returning a > reference and taking a reference as an argument and in the case of b2, the > tempory is `std::string("u")` . > In GCC 14+ (since r14-9263-gc7607c4cf18986), you can add [[gnu::no_dangling]] > to the z2 function definition to mark it as not returning a dangling > reference (from the arguments). and the warning goes away. If the declaration and definitions are in different files, do I have to add [[gnu::no_dangling]] only to the function declaration ? That said, is the warning triggered only based on the function declaration (accepting as parameter a reference to temporary and returning a reference), when on the function invocation the parameter is indeed a reference to a temporary?
[Bug c++/114220] False positive warning: possibly dangling reference to a temporary [-Wdangling-reference]
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114220 --- Comment #2 from Дилян Палаузов --- Is my reading correct, that the warning is triggered, when a function receives as parameter a reference to a temporary and returns a reference? If this is the only criterion, then it is a wrong assumption, that both references are somehow related.
[Bug c++/114220] False positive warning: possibly dangling reference to a temporary [-Wdangling-reference]
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114220 Andrew Pinski changed: What|Removed |Added Status|UNCONFIRMED |RESOLVED Resolution|--- |INVALID --- Comment #1 from Andrew Pinski --- const std::string& z2(const std::string& m); const std::string& z2(const std::string& m) { return hashTable2.at(m); } const std::string& b2 { z2("u") }; The warning is designed this way explictly because you are returning a reference and taking a reference as an argument and in the case of b2, the tempory is `std::string("u")` . In GCC 14+ (since r14-9263-gc7607c4cf18986), you can add [[gnu::no_dangling]] to the z2 function definition to mark it as not returning a dangling reference (from the arguments). and the warning goes away.