C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Preprocessor macro __COUNTER__

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Fri, 19 Aug 2022 10:02:09 +0200
czw., 18 sie 2022 o 23:47 Frederick Virchanza Gotham via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> On Tue, Aug 16, 2022 at 5:38 PM Lénárd Szolnoki via Std-Proposals wrote:
> >
> > Yeah, I would like a placeholder name proposal. A way to introduce an entity that can't be referred to, but otherwise behaves identically to a named entity of the same kind. It would be useful for structured bindings, lock guards, macros. It also doesn't share the ODR-violation footgun with __COUNTER__ .
>
>
> __COUNTER__ is handy when you want to instantiate a template. So let's
> say the template is:
>
> template<unsigned i>
> SomeClass &Func(void)
> {
> static SomeClass obj;
> return obj;
> }
>
> and then I can do this:
>
> int main(void)
> {
> SomeClass &objA = Func< __COUNTER__ >();
> SomeClass &objB = Func< __COUNTER__ >();
> SomeClass &objC = Func< __COUNTER__ >();
> SomeClass &objD = Func< __COUNTER__ >();
> SomeClass &objE = Func< __COUNTER__ >();
>
> assert( &objA != &objE );
> }

Would better be using lambda as it is unique but deterministic? Like
`Func<[]{}>()`.


> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-08-19 08:02:21