frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

A prvalue is not a temporary

https://blog.knatten.org/2025/10/31/a-prvalue-is-not-a-temporary/
11•ingve•2h ago

Comments

dzdt•6d ago
[Edited] For anyone like me stuck on his language: the phrase "move from" should be understood as a technical term loosely related to the English language meaning of the words. I think the post would be better if he explained this terminology; as it is you have to know an awful lot about the topic he is writing about to even parse what he is saying.

There is a pretty good stack overflow post that quuxplusone linked below. How they explain it:

  Moving from lvalues

  Sometimes, we want to move from lvalues. That is, sometimes we want the compiler to treat an lvalue as if it were an rvalue, so it can invoke the move constructor, even though it could be potentially unsafe. For this purpose, C++11 offers a standard library function template called std::move inside the header <utility>. This name is a bit unfortunate, because std::move simply casts an lvalue to an rvalue; it does not move anything by itself. It merely enables moving. Maybe it should have been named std::cast_to_rvalue or std::enable_move, but we are stuck with the name by now.
quuxplusone•5d ago
"Move" in the sense of https://stackoverflow.com/questions/3106110/what-is-move-sem...

Now, if you don't know what "move semantics" is, then "lvalues can't be moved from" isn't terribly helpful, and if you do then it's tautological, so I'm not saying you're wrong to criticize. :) But in a C++ context, "move" does have a single specific meaning — the one he's using properly if opaquely-to-non-C++ers.

Conscat•7h ago
"move" means to pass into an r-value reference function parameter, for instance a move constructor, move assignment operator, or forwarding reference.
cjensen•7h ago
He has a good article on that at [1]

But here's the gist: sometimes you have an object you want to copy, but then abandon the original. Maybe it's to return an object from a function. Maybe it's to insert the object into a larger structure. In these cases, copying can be expensive and it would be nice if you could just "raid" the original object to steal bits of it and construct the "copy" out of the raided bits. C++11 enabled this with rvalue references, std::move, and rvalue reference constructors.

This added a lot of "what the hell is this" to C++ code and a lot of new mental-model stuff to track for programmers. I understand why it was all added, but I have deep misgivings about the added complexity.

[1] https://blog.knatten.org/2018/03/09/lvalues-rvalues-glvalues...

neonz80•6h ago
I find that this can reduce overall complexity. It makes it possible to use objects that can not be copied (such as a file descriptor wrapper) and moving can in most cases not fail. Without move semantics you'd have to use smart pointers to get similar results but with extra overhead.
Night_Thastus•7h ago
Interesting stuff! I knew about lvalues and rvalues but I never knew about concepts like "glvalue" or "prvalue" or "xvalue" that the linked page talks about.

It makes sense that C++ avoids unnecessary copying or object creation whenever possible, that's pretty much C++'s M.O.

im3w1l•7h ago
> People sometimes call this “a temporary”, but, as is the main point of this article, that’s not necessarily true.

Old habits die hard? It used to always create a temporary right?

gpderetta•6h ago
> It used to always create a temporary right?

Temporaries could still be elided in some cases before, but the semantics were still understood in terms of temporary objects.

Now some forms of elision are mandatory and elision+RVO semantics are understood as objects being directly created into the final named location.

p0w3n3d•7h ago
C++ is so complicated that I had to almost fail my exam and few years later I had to relearn C, get some experience in a real business project, and then I could start learning C++.

I find that understanding how memory is layed out in executable, how the C works in terms of stack, symbols etc is the introductory knowledge I had to obtain to even think about C++. Not sure what's there now, because I saw recently some great compiler warnings, but I'm pretty sure that I did convert a prvalue to a pointer reference (&) at least once in my life and later failed with memory problems, but no compiler errors

saghm•6h ago
Getting failures later after coercing something to a reference is even easier than that; just deference a null pointer when passing to an argument that takes a reference; no warnings or errors! https://godbolt.org/z/xf5d9jKeh
TuxSH•5h ago
Interestingly, GCC (but not Clang) detects but doesn't warn the UB and emits "ud2" with -O2: https://godbolt.org/z/61aYox7EP
actionfromafar•4h ago
If you are working with C++ in this day and age, regardless of which compiler you use to output your actual binaries, you really owe it to yourself to compile as many source files as possible with other diagnostic tools, foremost clang-tidy.

It will catch that and a lot of iffy stuff.

If you want to go deeper, you can also add your own diagnostics, which can have knowledge specific to your program.

https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChec...

steveklabnik•6h ago
When people say "Rust is just as complex as C++," I think value categories are a great example of why it's actually simpler, even if it also seems complex. C++ has three primary categories: prvalue, xvalue, and lvalue. There's also glvalue and rvalue. Rust has two: a place (which is a glvalue) and a value (which is a prvalue).

C++ needs those extra categories, they exist for good reasons. But it is more complex.

Sharlin•6h ago
To be fair, though, Rust really needs something morally like prvalues, to solve the heap initialization problem aka `Box::new([42; 10_000_000])`
steveklabnik•6h ago
Yes, it is possible that Rust will add more complexity here specifically, but also just in general. Just how it goes :)
okanat•5h ago
I am not so sure. A purely type-system-based API (in place initialization + MaybeUninit ?) or a new "static" Vec type can already solve most of the problems with Box::new([val; N]).
tialaramex•4h ago
I think what you'd most likely do here is something like:

    const HUGE: usize = 10_000_000;
    let mut values = Box::<[i32]>::new_uninit_slice(HUGE);
    for k in 0..HUGE {
         values[k].write(42);
    }

    let values = values.assume_init();

Edited to fix early enter oops, typo in loop length that'd be caught if I'd tried this.
koito17•6h ago
Additionally, the "r" and "l" may lead one to incorrectly guess that rvalues and lvalues are related to their position in an expression. But alas, they aren't; there are lvalues that cannot appear in the left-hand side of an expression.
mzajc•5h ago
Are you referring to consts? Besides those, I can't really think of a counterexample.
tonyarkles•58m ago
https://en.cppreference.com/w/cpp/language/value_category.ht...

There’s some weird examples here involving functions, function pointers, template parameters, and a few other things.

nutjob2•4h ago
> cannot appear in the left-hand side of an expression

Do you mean assignment?

stonemetal12•6h ago
This is like saying 1 + 2 isn't addition because the compiler will optimize it away. It isn't an addition instruction in the emitted code but logically speaking it is addition.

Similarly just because a compiler may optimize a prvalue away doesn't change the fact that a prvalue by definition of the language is a temp.

Sesse__•6h ago
The article specifically points out that this isn't about optimization. A temporary will not be created even with -O0 (you can observe this by putting logging into the copy and move constructors).
quuxplusone•5h ago
Or even =delete'ing them or (carefully) putting static_asserts inside them. They're not called, not instantiated, not nothing.
nixpulvis•5h ago
Being able to accidentally use a value after moving it is tragic.
malkia•5h ago
Yup - I wish that was not the case, I've learned relatively recently that the state the variable becomes (after moving) is "valid, but unspecified" - e.g. you can still destroy it (I guess how would RAII work otherwise), you can still assign to it, and even check some basic properties (!) - but you can't know anything about the actual value (contents) it carries (weird)

It's like a tombstone for a deleted file, object - something that tells you - "Here lived Obj B Peterson, was nice folk, but moved away to a higher place"

vitus•4h ago
> "valid, but unspecified"

Annoyingly, it depends on the type, sometimes with unintuitive consequences.

Move a unique_ptr? Guaranteed that the moved-from object is now null (fine). Move a std::optional? It remains engaged, but the wrapped object is moved-from (weird).

Move a vector? Unspecified.

senderista•4h ago
Those semantics are entirely on you, the implementer, to enforce. Move semantics is basically a contract between move ctor/assignop impls and dtor impls: the former must somehow signal moved-out status (typically by nulling out a ptr or setting a flag), and the latter must respect it. And of course the client shouldn't use a moved-from object in invalid ways. All of this is completely ad-hoc and unenforceable.
malkia•4h ago
Well, it's not on me, because I'm often the user of someones library, framework, etc, and then it's up to whether it was concious or unconcious decision that the type behaved that way, and then you have to mix things to work together and you end up with this really "unspecified" way, hence you put some rules - "Don't do this" - don't use object after std::move, even if it might be allowed.

I'm still baffled.

senderista•2h ago
Well they couldn't do much better without supporting destructive moves. Rust shows how simple move semantics can be when they're designed into the language.
jandrewrogers•4h ago
It may seem inelegant but it pretty cleanly addresses real edge cases in systems-y code where the object necessarily has a shorter lifetime than its associated memory for strict safety.

In these cases, something resembling "use after free" is unavoidable if the move is destructive even though the object is semantically dead. Putting the object in a sentinel state where it is alive but not usable captures the semantics of deferred destruction pretty well.

nixpulvis•4h ago
In practice it just means a lot of sentinel values and unuseful potential accesses.
jandrewrogers•3h ago
Not really. In the vast majority of cases this is all elided. There is no cost, either in lines of code or runtime.

When you are decoupling memory lifetimes from object lifetimes it is pretty explicit. It isn't like this sneaks into your code on its own. You have to manage the implications of it yourself.

nixpulvis•3h ago
It's not about cost of correct code, it's about accidental wrong code.
jandrewrogers•1h ago
That’s really not a thing. By default it is correct, you have to go out of your way to make it incorrect.
malkia•5h ago
To this day, I'm still having trouble remembering these names: lvalue, rvalue, prvalue, xvalue, glvalue.

While there was only lvalue, and rvalue - it was easy - LEFT and RIGHT - gives you the right intuition.

But now - if it has identity - "glvalue" - why not "ivalue" or "idvalue"?

And then can be moved - "rvalue" ? Why "rvalue", why not "mvalue", or "move-value"? Why the language have to be so constricted when comes to such an important bit - I mean we spell "value" fully, but miss the important bit...

Anyway just a rant... I'm having the same issue understanding "math" sometimes because of all the cryptic notations, and archaic symbols used.

kccqzy•5h ago
I agree the names are confusing. But it's just a simple matter of having a diagram nearby to remember which is which. (I think I first saw that diagram on Stack Overflow.)

There have been a lot of times in science when I have trouble remembering names but have no trouble at all understanding the concepts behind these names. I just keep an index card nearby. I noticed this tendency of mine as early as high school. For example, in chemistry I sometimes couldn't remember which is dextro and which is levo, but I understand chirality and know how they are different. In physics I sometimes forget which is the magnetic B field and which is the magnetic H field, though I understand the difference between them. I don't use these concepts often so I haven't internalized the names. I think it's totally alright to have a name–concept dissociation for these.

malkia•5h ago
I completely get it, that the language science has developed works great for it's own practicioners, but not really well for outsiders.

Good luck explaining these "*values" to someone that hasn't touched C++ in a while. Then again other languages have the same peculiarities though. meh.

spacechild1•4h ago
I think they wanted to preserve the meaning of the existing two value types. The naming is still bad, though, because it's inconsistent. For example, why should 'xvalue' belong to 'generalized lvalue' when it also belongs to 'rvalue'? Aren't 'l' and 'r' supposed to be opposites on the same dimension (movability)?

Here's a suggestion in retrospect:

         | ivalue  | pvalue
         ---------------------
  lvalue | ilvalue | (plvalue)
  rvalue | irvalue | prvalue
The current naming is confusing exactly because 'lvalue' should be a supertype in one dimension, just like 'rvalue', and not a subtype (here: 'ilvalue'). I think they took this shortcut because 'plvalue' doesn't really exist, but it's still inconsistent. Let's not even talk about 'xvalue' (here: 'irvalue').
spacechild1•5h ago
This is actually a very nice explanation!

I also enjoyed the linked article about the 5 value types: https://blog.knatten.org/2018/03/09/lvalues-rvalues-glvalues.... For some reason I never bothered to look up these terms as they sounded so obscure. Turns out the taxonomy is pretty clear and it's just a refinement of the existing two value types (lvalues and rvalues).

EDIT: I do think the naming is rather confusing and inconsistent, though.

gnabgib•2h ago
Discussion (40 points, 6 days ago, 38 comments) https://news.ycombinator.com/item?id=45770577
tomhow•1h ago
Not exactly. This was originally submitted 6 days ago, and was put in the SCP but took till now to get on the front page. Just as the discussion was developing, the original submission "expired" and disappeared from the front page.

Given that it's an active discussion with all comments posted in the past few hours, I've created a new copy to give it its full opportunity for visibility and discussion.

gnabgib•1h ago
Ah, well that's a strange maneuver (it wasn't a vote-less or discussion-less post before the SCP and you moving the comments)
tomhow•1h ago
Only two of the upvotes and one of the comments was from before today, and it had no front page time until today. It's a lot stranger for a post to disappear completely from the rankings, just as the discussion is developing.

Japan deploys its military to stop bears

https://www.npr.org/2025/11/06/nx-s1-5600582/japan-bear-attacks
1•halamadrid•3m ago•0 comments

What happens when you mix live faculty with generative AI?

http://www.sampaddock.com/blog/2025/10/27/1-week-five-faculty-130-students-ai-tutor-paski-nps-78-...
1•ryanjshaw•4m ago•0 comments

China launches 18 day Arctic shipping route

https://www.highnorthnews.com/en/china-launches-18-day-arctic-express-containership-route-europe-...
1•bharbr•5m ago•0 comments

End of The Line: how Saudi Arabia's Neom dream unravelled

https://ig.ft.com/saudi-neom-line/
1•zekrioca•14m ago•0 comments

A 400-Year-Old Ring That Unfolds to Track the Movements of the Heavens

https://www.openculture.com/2025/11/a-400-year-old-ring-that-unfolds-to-track-movements-of-the-he...
1•MaysonL•18m ago•0 comments

Primitive Technology: Wood ash and crushed terracotta mortar [video]

https://www.youtube.com/watch?v=UqJHfTkvVY4
1•thunderbong•19m ago•0 comments

EU Explained in 10 Minutes

https://www.lesswrong.com/posts/88CaT5RPZLqrCmFLL/eu-explained-in-10-minutes
1•surprisetalk•19m ago•0 comments

Allow me to introduce the two-sentence journal

https://alexanderbjoy.com/two-sentence-journal/
1•surprisetalk•19m ago•0 comments

Standard Model – Part 2: The Big Ideas [video]

https://www.youtube.com/watch?v=tGCUjsF7Dgg
1•surprisetalk•20m ago•0 comments

JermCAD: Browser-Based CAD Software

https://github.com/jeremyaboyd/jerm-cad
1•azhenley•24m ago•0 comments

Array Programming the Mandelbrot Set

https://jcmorrow.com/mandelbrot/
1•jcmorrow•24m ago•0 comments

Bridging the gap between fast-evolving code libraries and AI models

1•siddythings•26m ago•0 comments

Show HN: I built a browser extension to help turn ideas into well-crafted tweets

https://tweetbl.ink
1•thanhdongnguyen•27m ago•0 comments

Possible effects of cheap fentanyl on drug markets, use and harm

https://www.tandfonline.com/doi/full/10.1080/17440572.2025.2561658
1•PaulHoule•28m ago•0 comments

Building an Accessible Before/After Slider in React

https://jsdev.space/react-before-after-slider/
1•javatuts•29m ago•0 comments

What Leaders Carry

https://nikrad.io/blog/what-leaders-carry/
2•nikradm•31m ago•0 comments

China's Shenzhou-20 return mission delayed due to space debris impact

https://www.nbcnews.com/world/asia/chinas-shenzhou-20-return-mission-delayed-due-space-debris-imp...
1•gmays•36m ago•0 comments

Molten-Salt Reactor

https://en.wikipedia.org/wiki/Molten-Salt_Reactor_Experiment
2•felineflock•37m ago•0 comments

Show HN: FlashVSR – High-Speed 4K Video Super-Resolution

https://www.aiupscaler.net/flashvsr
1•lu794377•38m ago•0 comments

Apple TV's colorful new branding was built with glass and captured in-camera

https://twitter.com/avstorm/status/1986419403253035080
4•johnhamlin•40m ago•2 comments

A Tiny 29-Pound Electric Motor Just Beat Everyone in Power Density

https://www.popularmechanics.com/cars/hybrid-electric/a65490281/tiny-electric-motor/
1•nobody9999•41m ago•0 comments

Show HN: Practice your captcha skills with Google's weirdest Street Views

https://street-captcha.netlify.app/
1•SantiDev•45m ago•0 comments

Show HN: Lyrics to Song AI

https://lyricstosong.io/
1•derek39576•47m ago•0 comments

Goldman Sachs Announces Managing Director Class of 2025

https://www.goldmansachs.com/pressroom/press-releases/2025/managing-director-announcement-06-nov-...
1•vismit2000•51m ago•0 comments

German nurse convicted of murdering 10 patients to reduce his workload

https://www.cnn.com/2025/11/06/europe/germany-nurse-killer-convicted-scli-intl
1•nothrowaways•54m ago•0 comments

The Electrotech Revolution

https://ember-energy.org/latest-insights/the-electrotech-revolution/
2•toomuchtodo•57m ago•0 comments

Just Trust the Autopilot and AI Augmenting Air Traffic Control

https://medium.com/@tobrien/just-trust-the-autopilot-ab18307dfadf
1•tmo9d•59m ago•0 comments

Most hated budget site in 2026

https://test-ruby-eta-12.vercel.app/
1•murderszn•1h ago•1 comments

Meta's Broken API: How Facebook Is Killing Small Developer Innovation

1•Jvit•1h ago•1 comments

Lenovo puts the 'cloud' in cloud computing, proposes mid-air datacenters

https://www.theregister.com/2025/11/06/lenovo_datacenter_vision/
1•sipofwater•1h ago•1 comments