C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Std-Proposals Digest, Vol 41, Issue 25 (__COUNTER__)

From: Alexander Christensen <alex_c007_at_[hidden]>
Date: Wed, 17 Aug 2022 10:08:30 +0000
Regarding __COUNTER__ :

I have never heard about it, but as mostly everything else that involves macros it sounds brittle and/or superfluous. Out of my head:
* thread safety?
* what if someone redefines it or, as you mention - use it in headers (especially in non-inline code sections)?

Also, seeing as it is a macro, and thus probably (?) compile-time defined (how can we know?), perhaps the order of compilation could change the output. In other ways - it is probably useless unless applied only inside the source of a single translation unit.

I would have a global function instead:

int counter() {
    static int _cnt = 0;
    return _cnt++;
}

That way we could also spinlock the insides of that function. But I'm curious to hear if someone knows more.

/Alexander

Received on 2022-08-17 10:08:32