C++ 17 feature: if statements with initializers

83 views
Skip to first unread message

Stefano Duo

unread,
Aug 12, 2025, 4:13:55 PMAug 12
to blink-dev, mme...@chromium.org, ba...@chromium.org, Etienne Dechamps
Hi blink-dev,

Matt gave me advice to mail this list.

We are discussing a C++ 17 feature: if statements with initializers. It is recommended when a variable is used within the if statement, but not outside of it.

Pros: reduced variables scope.
Cons: new syntax, currently not common in Chromium.

How does the community feel about this? Should its usage be allowed?

Thank you,
Stefano

Daniel Cheng

unread,
Aug 12, 2025, 4:21:07 PMAug 12
to Stefano Duo, blink-dev, mme...@chromium.org, ba...@chromium.org, Etienne Dechamps
It's been allowed in Chrome C++ for some time now, so it's allowed in Blink.

In general, https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++-features.md tries to default allow features in older C++ versions, with specific callouts to things that are banned.

Daniel

--
You received this message because you are subscribed to the Google Groups "blink-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blink-dev+...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/blink-dev/f8f46d20-0fe9-4a8d-b8a8-30d99a39a635n%40chromium.org.

Matt Menke

unread,
Aug 12, 2025, 4:24:16 PMAug 12
to Daniel Cheng, Stefano Duo, blink-dev, ba...@chromium.org, Etienne Dechamps
The question wasn't so much whether it's allowed, but whether it's a reasonable style choice, due to the loss in readability. If there was a widespread shift to prefer the pattern, that's one thing, but if one or two developers introduce it only in their corners of the code, that's potentially a significant loss in readability when other Chrome devs work on that corner of the world.

Giovanni Ortuño

unread,
Aug 12, 2025, 4:31:28 PMAug 12
to Matt Menke, Daniel Cheng, Stefano Duo, blink-dev, ba...@chromium.org, Etienne Dechamps

K. Moon

unread,
Aug 13, 2025, 2:28:27 AMAug 13
to Giovanni Ortuño, Matt Menke, Daniel Cheng, Stefano Duo, blink-dev, Kenichi Ishibashi, Etienne Dechamps
I agree this seems more like a cxx question.

Note that one reading of the Google C++ style guide actually requires use of if-with-initializer when applicable:

"Place a function's variables in the narrowest scope possible, and initialize variables in the declaration."

Reasonable engineering judgment on the "narrowest scope possible" is fine, but this C++ feature isn't even particularly obscure in 2025.

Dirk Pranke

unread,
Aug 13, 2025, 12:16:43 PMAug 13
to Matt Menke, Daniel Cheng, Stefano Duo, blink-dev, ba...@chromium.org, Etienne Dechamps
By "loss of readability" you mean that "this is uncommon, and so I'm not used to reading it and it's harder to understand", right? And not "using initializers is less readable than not using initializers in general"?

If you followed the first argument, then you would never be able to evolve the codebase to start using new features, so I don't think we should use that as a hard and fast rule.

But, I do think it's reasonable to have a broader discussion (perhaps this one) to raise awareness of the feature.

I think the purpose of cxx@ is "is using initializers less readable in general, so should we ban them" (or do they have other problems)? Once cxx@ has said that they're okay (and it seems they have), then you have the discussion that says "hey, we're going to be using these in Blink now", etc.

-- Dirk

Reply all
Reply to author
Forward
0 new messages