r/cpp • u/javascript • May 09 '26
Any good tech talks leveraging statement expressions?
Lambdas do a great job in a lot of cases but sometimes you need a statement expression. Any good content on youtube?
14
Upvotes
View all comments
Show parent comment
1
u/javascript May 09 '26
There's a few different things you can do when you return a value from a function.
If you just name it directly...
```
return my_value;
```
...then it implicitly moves and potentially leverages triviality.
Or you can call `std::move(...)` on it... DO NOT DO THAT!
```
return std::move(my_value); // NO! BAD!
```
Or you can call a method on it...
```
return my_value.foo();
```
...which could potentially consume the object, but you didn't tell the API enough information!
In order to select the rvalue reference qualified method overload (which is a property of the `this` object), you have to first call `std::move(...)` on the object and then invoke the method on the result. This tells the library what it needs to know to make the most use of expiring values.
```
return std::move(my_value).foo();
```