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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(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.

Reply via email to