C++ Logo

std-proposals

Advanced search

Re: [std-proposals] __COUNTER__

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 23 Aug 2024 08:08:00 -0700
On Friday 23 August 2024 02:09:39 GMT-7 Marcin Jaczewski via Std-Proposals
wrote:
> I think we have XY problem here, why do I even need `__COUNTER__` in
> my C++ code?
> Unique local variable? Soon we will have `_`.
> Unique global variable? Add new keywords that will be placeholder for
> name like `__unique_name__`.
>
> There is any other case where I would use `__COUNTER__`?

Having a __unique_name__ identifier generator may replace __COUNTER__, but
given that the likely only use is still through macros, I don't see the value.
Plus it wouldn't solve the problem of ODR: unlike a preprocessor thing like
__COUNTER__, it would be hard to standardise a language construct that knows
which file it came from, so if the same file is #include'd from another TU, it
generates the same identifier.

There are two places in Qt code where we currently need unique identifiers, but
neither currently need __COUNTER__. One of them is in the Q_GLOBAL_STATIC
macro, which creates a global (namespace-scope) static variable of a unique
type and the other is in the moc-generated code associated with each meta
object's meta type table (a hackish solution to prevent ODR violations in case
of types only forward-declared are fully declared elsewhere). In both cases,
it actually turns out we have a unique identifier we could use to build another
upon, so we could avoid __COUNTER__, respectively the name of the variable and
the type of the class whose meta object we're creating.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2024-08-23 15:08:05