rvalue parameters

219 views
Skip to first unread message

Evan Stade

unread,
Apr 23, 2025, 3:34:23 PM4/23/25
to Chromium-dev
Hi all,

It was to my surprise to recently learn that the Google C++ style guide bans rvalue references in function/method parameters. I have been happily using them in Chromium code for years, and I see I'm not the only one (here are two random examples, not to pick on anyone).

The style guide's justification for banning them appears to rest on two cons, the first being "Rvalue references are not yet widely understood", which surely is less true now, a decade later?

My intention (and I think others') for rvalue params is to enforce move semantics instead of making it easy to accidentally copy, when copying is expensive, such as with large STL containers (where we don't have the choice of enforcing move-only at the class level).

Is there any appetite for adding some nuance (i.e. exceptions) to this ban, or some additional color that can help explain why even this use of rvalues is ill-advised?

-Evan


K. Moon

unread,
Apr 23, 2025, 9:10:06 PM4/23/25
to Evan Stade, Chromium-dev
It's not actually a ban, it's just a limited set of allowed uses. Do you have an example of a worthwhile usage that doesn't fall under the existing exemptions?

One of the ideas behind the guidance is that it's better for callees to accept parameters by value, since that allows the caller to pass by value or move. Rvalue references remove that choice from the caller.

The style guide mentions performance is an acceptable reason to use rvalue references, but it must be justified, not just sprinkled everywhere without measurement.

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/ad7f03ba-9087-4dd5-98dc-5ea12d87d06fn%40chromium.org.

Evan Stade

unread,
Apr 23, 2025, 9:27:12 PM4/23/25
to Chromium-dev, K. Moon, Chromium-dev, Evan Stade
>  Do you have an example of a worthwhile usage 

What do you think of the two examples in my last email? I think they are worthwhile, but they fall outside the bounds of the style guide. None of the four exceptions apply in either case, AFAICT.

>  that allows the caller to pass by value or move. Rvalue references remove that choice from the caller.

I think the intention usually is exactly that --- to influence the caller's usage --- in cases where the callee is opinionated about how it should be used. It is useful to prevent callers from accidentally and silently doing the wrong thing. When there is no good reason to pass by value, it can be assumed that doing so is a mistake and not a choice --- see the ReleaseSoon example. But it actually doesn't remove the choice completely, as callers could still pass a temporary if they really want to make a copy. (i.e. `PassFoo(std::vector<char>(buffer))`)`

>  it's just a limited set of allowed uses

Right --- my suggestion would not be to remove the guidance altogether, but to consider expanding the set of allowed uses to, say, "types that are copiable for the sake of other use cases but in this case really should be discouraged from being copied".

If this suggestion is not a good one, then the fallback purpose of this thread is to raise awareness that rvalue reference misuse is rampant in Chromium.

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

K. Moon

unread,
Apr 28, 2025, 12:39:29 PM4/28/25
to Evan Stade, Chromium-dev
Apologies, but I only sporadically have time to pay attention to Chromium stuff, so haven't been able to circle back to this yet.

My quick take is that the most alarming about the original e-mail was the implication that the style guide banned rvalue references, rather than merely restricting them. I think where the conversation is now is a good starting point for further discussion.

I think it's fine to discuss relaxing the rvalue restrictions, but as the style guidance stands today, I would say some of these uses are in violation of the style guide, and would need to justify why an exception is warranted. I'll take that up the specific examples more when I have free time, if nobody else weighs in first.

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
Reply all
Reply to author
Forward
0 new messages