C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Dummy names for dummy objects

From: Justin Cooke <jgc_at_[hidden]>
Date: Sat, 10 Jun 2023 18:53:52 +0200
You don't need a __dummy keyword.

The following syntax could be used to define anonymous objects:

type = initializer;

type = { initializer };

type = { initializer1, initializer2 };

etc.

The key ingredient is the = sign.  Without that, a temporary object would be created which is destroyed immediately.




A type name  follwoed by an equal initializar

On 10 Jun 2023 at 15:27, Frederick Virchanza Gotham via Std-Propos wrote:

> Sometimes I have code like this:
>
> void Func(void)
> {
>     OnScopeExit dummy( [](){ ::close(global_fd); } );
>
>     // Do more stuff here
> }
>
> If I later amend this function so that further down there's another
> 'OnScopeExit', then I have to name the second one "dummy1", and the
> third one "dummy2" and so on.
>
> For the sake of making it easier to patch source files, I propose that
> we can give an object a dummy name as follows:
>
> void Func(void)
> {
>     OnScopeExit __dummy( [](){ /* Do Something */ } );
>
>     // Do more stuff here
>
>     OnScopeExit __dummy( [](){ /* Do Something */ } );
>
>     // Do more stuff here
>
>     OnScopeExit __dummy( [](){ /* Do Something */ } );
> }
>
> These objects don't have a name clash. If you try to access an object
> by the name '__dummy', it accesses the most recently defined dummy
> object:
>
> void Func(void)
> {
>     OnScopeExit __dummy( [](){ /* Do Something */ } );
>
>     // Do more stuff here
>     _dummy.SomeMethod();   // refers to the object defined 3 lines above
>
>     OnScopeExit __dummy( [](){ /* Do Something */ } );
>
>     // Do more stuff here
>     _dummy.SomeMethod();   // refers to the object defined 3 lines above
>
>     OnScopeExit __dummy( [](){ /* Do Something */ } );
> }
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

  

Received on 2023-06-10 16:53:58