Warning on template parameter identifier shadowing?
H. S. Teoh
hsteoh at quickfur.ath.cx
Wed Aug 5 16:57:43 UTC 2020
On Wed, Aug 05, 2020 at 04:35:12PM +0000, Paul Backus via Digitalmars-d wrote:
> [...] there's no reliable way to generate unique identifiers at
> compile time.)
Sure there is. Well, depending on what you need said identifiers for,
that is. Given a unique tuple, a template instantiated with that tuple
is a unique identifier for that tuple.
I've used this fact to generate unique identifiers for temporaries in
static foreach loops, since static foreach does not introduce a new
scope, so all iterations share the same namespace, and any temporaries
you might declare will collide with other iterations and fail with a
compile error. To work around this, I wrap the desired declaration(s) in
an auxiliary template that gets instantiated with the index variable of
the static foreach, thus guaranteeing uniqueness:
struct TmpStruct(int iter) {
...
}
int someAcc;
switch (someVar) {
static foreach (i; 0 .. 99) {
case i:
auto tmp = TmpStruct!i(...);
doSomething(tmp);
break;
}
}
T
--
Long, long ago, the ancient Chinese invented a device that lets them see through walls. It was called the "window".
More information about the Digitalmars-d
mailing list