C++ value category cheat-sheet [pdf]
github.com
github.com
http://eli.thegreenplace.net/2011/12/15/understanding-lvalue...
a) markdown with embedded graphviz.
ex: https://github.com/TLmaK0/gravizo
b) a jupyter notebook (which also uses markdown blocks). jupyter can embed C++, so you can have interactive blocks that you can evaluate and play with.
ex: https://github.com/minrk/clingkernel/blob/master/cling.ipynb
Both seem more performant than in-browser PDF rendering.
Thanks for the suggestions.
e.g.
std::string_view sv{ "lvalue" };
or auto sv{ "lvalue"sv }; auto i{42}; // std::initializer_list<int>[*] pretty much
If you don't understand what you are writing, either the compiler (rust-like) or the runtime (script-like) should complain.
C++ usually does neither. I think that's undesirable. Why it does neither makes sense, but I wouldn't call it a strength.
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?I think this is similar to the std::max/std::min stuff
Or have I suddenly gone blind by looking at C++ code one too many times? :)
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.
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++. :)
"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?
int const &r = f(3);
and int r = f(3);
would both be UB under that implementation-defined case.The posted article, second row of "common mistakes", describes returning a const reference parameter, when called with a prvalue, as blanket UB.
Finally someone clarified: https://timsong-cpp.github.io/cppwp/class.temporary#6.1
"6.1 A temporary object bound to a reference parameter in a function call persists until the completion of the full-expression containing the call."
So, while the parameter's (the reference "int const &a") lifetime is implementation-defined, the temporary to which that parameter refers to is the full expression of the call. Sanity is restored.
const int& f(int parameter) { return parameter; }
const int i = f(42);
Is strictly undefined behavior under the simpler C++03 rules/wording of 5.2.2/4: "The lifetime of a parameter ends when the function in which it is defined returns.")P0135R0 would loosen the requirements on parameter's lifetime as a means of enabling guaranteed copy elision, in such a way that an implementation could technically decide to make this to have defined behavior (not that I suggest relying on it!)
Another drawback of colored backgrounds is that readers wanting to print out the pdf will consume ink/toner to render the non-white background.
I'm not a PhD in Colorology but choosing higher contrast colors completely eliminates that problem I think. It's an easy fix that opens up many new markets to your software.