6 ms·
C++ value category cheat-sheet [pdf]
- fytch 10y agoReddit 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?
- partycoder 10y agoIt is great, but regarding the format I would suggest some options to PDF: a) markdown with embedded graphviz. ex: https://github.com/TLmaK0/gravizo 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 https://github.com/minrk/clingkernel/blob/master/cling.ipynb Both seem more performant than in-browser PDF rendering.
- Jeaye 10y agoI looked for a number of options and I didn't find anything which could provide me with automatic syntax highlighting, non-html layouts, and quick iteration. That doesn't mean that such a thing doesn't exist! Thanks for the suggestions.
- partycoder 10y agoJupyter notebooks are probably what you are looking for. I highly recommend them. Embedded REPL makes iteration very fast and results can be shared directly through a URL, exported to various formats or shared in websites that support it such as github.
- jasode 10y agoA comment on the document's design: The low contrast of light gray text on yellow background makes it difficult to read. Black text on white, or dark gray text on white is perfectly acceptable. Another drawback of colored backgrounds is that readers wanting to print out the pdf will consume ink/toner to render the non-white background.
- saagarjha 10y agoI'm pretty sure the author was trying to use the Solarized color scheme: http://ethanschoonover.com/solarized http://ethanschoonover.com/solarized
- Jeaye 10y agohttps://github.com/jeaye/value-category-cheatsheet/blob/master/src/value_categories/css.clj#L4 https://github.com/jeaye/value-category-cheatsheet/blob/mast... You bet. :)
- clishem 10y agoRelevant: http://contrastrebellion.com http://contrastrebellion.com
- gravypod 10y agoThat site is great but it leaves out something else that is very important. Color blind people often have trouble with seeing colors that are "near" to each other in color-space. 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.
- 7ewis 10y agoAre there cheat sheets like this for other languages too?
- SolarNet 10y agoMost of them aren't as insane.
- smitherfield 10y agoQuestion: it is ok to initialize a string_view from a string literal, right? e.g. std::string_view sv{ "lvalue" }; or auto sv{ "lvalue"sv };
- Jeaye 10y agoYeah. String literals are lvalues and they live for the lifetime of the program. Both of those snippets would be fine.
- thecpphorse 10y agoYes, but never use {} initialization with auto.
- clishem 10y agoNever give advice without telling why, because it's very likely that people will forget it if they don't understand your advice.
- thecpphorse 10y agoOk. auto i{42}; // std::initializer_list<int>
- david-given 10y agoBut why is that bad?
- lmitchell 10y agoBecause `i` isn't an int, it's an initializer_list, and you almost never want to actually create an initializer_list instead of an int? Seems pretty understandable to me.
- thecpphorse 10y agoIt's unexpected. Nobody ever* wanted to assign an initializer list to a named variable. [*] pretty much
- valarauca1 10y agoI think the biggest issue in C++ is that you don't have to understand this whole mess to be productive in it. This is also perhaps its greatest strength. Where you have Rust which won't even compile a basic toy program without demonstrating a full understanding of its Ownership model.
- jjnoakes 10y agoIt isn't a strength to allow ill-defined programs to silently corrupt your process. 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.
- greydius 10y agoWhich is it? C++ programmers are smart enough to not need the features that Rust provides? Or C++ programmers aren't smart enough to learn basic concepts that they should already know considering their compiler doesn't help them write correct code?
- jewbacca 10y agoSofter entry to the whole concept of lvalues/rvalues in general: http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c http://eli.thegreenplace.net/2011/12/15/understanding-lvalue...
- mpweiher 10y agoAnd of course there's Strachey's Fundamental Concepts in Programming Languages (1967), which introduced the LValue/RValue distinction. It is very readable and relevant today. http://www.cs.cmu.edu/~crary/819-f09/Strachey67.pdf http://www.cs.cmu.edu/~crary/819-f09/Strachey67.pdf