https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81420
Ion Lupascu <ion.lupascu at barclays dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |ion.lupascu at barclays dot com
--- Comment #7 from Ion Lupascu <ion.lupascu at barclays dot com> ---
Not really, The issue still persists
Check this example:
#include <iostream>
#include <limits>
#include <optional>
std::optional<double> getValue(){
return 3.14;
}
int main(){
const double &v = *getValue();
std::cout<<"v:=" << v << std::endl;
if (v == 0.0) {
__builtin_abort();
}
return 0;
}
compiled with GCC trunk (v13) and the following flags: -O3 -std=c++17 -Werror
-Wall -Wextra
returns:
Program returned: 139
v:=0
link to play: https://godbolt.org/z/fPhxqe1h3
clang behaves correctly till v 10, it extends the lifetime, in v10 it catches
with dangling-gsl check. when adding a more complex value rather than double
clang doesn't catch it but it extends the lifetime. e.g.:
#include <iostream>
#include <limits>
#include <optional>
namespace
{
struct Data{
double v;
};
} // namespace
std::optional<Data> getValue(){
return Data{3.14};
}
int main(){
const double &v = getValue()->v;
std::cout<<"v:=" << v << std::endl;
if (v == 0.0) {
__builtin_abort();
}
return 0;
}
link to play : https://godbolt.org/z/WoeGqhn7E
where GCC catches with the following error:
<source>: In function 'int main()':
<source>:20:25: error: using a dangling pointer to an unnamed temporary
[-Werror=dangling-pointer=]
20 | std::cout<<"v:=" << v << std::endl;
| ^
<source>:19:31: note: unnamed temporary defined here
19 | const double &v = getValue()->v;
| ~~~~~~~~^~
cc1plus: all warnings being treated as errors
ASM generation compiler returned: 1
GCC must first fix the temporary values lifetime when bounded by reference.