4 ms·
Reddit discussion: https://www.reddit.com/r/cpp/comments/5zzurr/c_value_category_cheatsheet/ https://www.reddit.com/r/cpp/comments/5zzurr/c_value_categor...
by fytch 10y ago
Reddit discussion: https://www.reddit.com/r/cpp/comments/5zzurr/c_value_category_cheatsheet/ https://www.reddit.com/r/cpp/comments/5zzurr/c_value_categor...
- Jare 10y agoI'd love it if someone could shed some light on my question here: https://www.reddit.com/r/cpp/comments/5zzurr/c_value_category_cheatsheet/df2xvbj/ https://www.reddit.com/r/cpp/comments/5zzurr/c_value_categor... int const &f(int const &a) { return a; } int r = f(3); Is the value of r implementation-defined according to the standard?
- beached_whale 10y agoThis might answer that https://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/ https://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-th... I think this is similar to the std::max/std::min stuff
- Jare 10y agoNo, that post refers to the lifetime of temporaries held by local reference variables. My question is about temporaries created to pass reference parameters to a function. Is the lifetime of that temporary the BODY of the function? The full expression that calls the function? The enclosing scope of that full expression? (for the record, VS2017 does the latter)
- deleted 10y ago[deleted]
- fauigerzigerk 10y agoI don't see a problem here. r receives a copy of a, not a reference to anything. Or have I suddenly gone blind by looking at C++ code one too many times? :)
- Jare 10y agoIf the lifetime of the temporary holding the value 3 can (implementation-defined) be limited to the body of the function, the assignment to r happens when the temporary can (implementation-defined) be destroyed already.
- fauigerzigerk 10y agoAh, now I see what you mean. This is not a problem. The temporary ceases to exist after the function call expression. Copying the return value has to be done before that, otherwise you could never return the value of any local variable.
- Jare 10y agoThat's what I had always thought would happen; it makes sense. However, if I'm reading things correctly, the posted article disagrees, and this link also disagrees: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0135r1.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p013... "It is implementation-defined whether the lifetime of a parameter ends when the function in which it is defined returns or at the end of the enclosing full-expression." Hence why I asked for some substantive clarification or solid interpretation of the standard. "Common sense" doesn't cut it with C++. :)
- fauigerzigerk 10y agoI'm not sure where you see a disagreement with section 5.2.2/4 of the standard: "It is implementation-defined whether the lifetime of a parameter ends when the function in which it is defined returns or at the end of the enclosing full-expression." Regardless of where exactly the lifetime of the temporary ends (end of function or end of expression), your example would be correct and not UB. I don't think it is in dispute whether or not the return expression inside the function is evaluated before the lifetime of the parameter ends, but that is when the copy of a into r is made. Where do you see a disagreement with the posted article?