C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal Idea: with Block for C++ – Extending Object Scope

From: Simon Schröder <dr.simon.schroeder_at_[hidden]>
Date: Sat, 22 Mar 2025 14:02:37 +0100
The idea looks nice in these tiny example. Ultimately, I don’t think it is a good idea.

On the plus side there is already implicit this inside member functions. If ‘with’ would only allow a single object instead of multiple, this would be the same concerning overload resolution rules.

That being said, you only write the code once and then read it multiple times. I advocate for readability/maintainability instead of saving a few keystrokes. It will be really hard to figure out what is happening, especially if there is more than one object inside the ‘with’ block. I don’t think it increases readability at all. So, my opinion is ‘No’.

> On Mar 22, 2025, at 7:55 AM, Vaibhav Parate via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> 
> Hello,
> Before I spend time on a formal paper for this idea, I want some feedback and suggestions.
>
> Idea:
> I'd like to propose a with block for C++ that allows extending the scope of objects, making field access easier and reducing redundant qualifiers.
>
> Motivation (while working on some personal projects):
> Currently, in C++, working with objects often requires repeatedly qualifying fields with object-> or object. inside functions and lambdas. This leads to unnecessary verbosity, especially when dealing with deeply nested structures.
>
> Example:
> C++ Currently (It is readable but very quickly gets messy, specially when you are the author of the entire codebase):
> void foo (A a, B* b, C& c) { // let's assume A, B, and C are types
> a.doSomething();
> b->doSomethingElse();
> // some other calles
> c.trySomethingElse();
> }
>
> C++ with "with" blocks:
> void foo (A a, B* b, C& c) {
> with (a, *b, c) {
> doSomething();
> doSomethingElse();
> // some other calles
> trySomethingElse();
> }
> }
>
> Or if you want to use them for the entire function? Consider this:
> void foo (A a, B* b, C& c) with (a, *b, c) {
> doSomething();
> doSomethingElse();
> // some other calles
> trySomethingElse();
> }
>
> What about field name collisions?
> They can be easily solved using existing syntax of using object. or object->.
>
> Maybe it will be harder for people to port old code that uses "with" as an identifier (variable name etc.) what about that?
> It's not necessary to use this exact keyword, it can be something else like "using" which is already a keyword.
>
> Why?
> I always find it annoying to use qualifiers while initializing objects, yeah there are constructors (and other initializers), but there are sometimes where constructors aren't enough and the object needs to be initialized in different ways. I may be wrong here and maybe the only one facing these issues, but does it hurt to have this feature?
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-03-22 13:02:54