C++ Logo


Advanced search

Re: [std-proposals] Preprocessor macro __COUNTER__

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 18 Aug 2022 22:47:28 +0100
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 );

Received on 2022-08-18 21:47:40