Tech News
← Back to articles

A prvalue is not a temporary

read original related products more articles

This is part one of a series of blog posts on temporaries, copies, return value optimization, and passing by value vs. reference.

A good place to start, and the point of this first article, is how a prvalue isn’t necessarily a temporary.

If you’re entirely new to value categories (lvalues, rvalues etc.), you might want to read lvalues, rvalues, glvalues, prvalues, xvalues, help! first.

lvalues vs rvalues

An lvalue is an expression that can not be moved from. An rvalue is an expression that can be moved from.

Let’s first have a look at lvalues. Given this variable v :

std::vector v{1,2,3};

If I now write the expression v somewhere, v is referring to an actual variable. I can’t just move from it, as it would mess up an existing object that someone else could still be using. We call an expression like this an lvalue.

For instance, if I pass my existing vector to a function useVector :

useVector(v);

... continue reading