Lifetime of implicitly casted temporaries
Posted
by
Answeror
on Stack Overflow
See other posts from Stack Overflow
or by Answeror
Published on 2010-12-24T15:05:15Z
Indexed on
2010/12/24
15:54 UTC
Read the original article
Hit count: 218
I have seen this question. It seems that regardless of the cast, the temporary object(s) will "survive" until the fullexpression evaluated. But in the following scenario:
void foo(boost::tuple<const double&> n) {
printf("%lf\n", n.get<0>());
}
int main() {
foo(boost::tuple<const double&>(2));//#1
foo(boost::make_tuple(2));//#2
return 0;
}
1 run well, but 2 do not. And MSVC gave me a warning about 2: "reference member is initialized to a temporary that doesn't persist after the constructor exits"
Now I am wondering why they both make a temporary "double" object and pass it to boost::tuple<const double&>
and only 2 failed.
© Stack Overflow or respective owner