Annoyance: 'Shadowing declaration is deprecated'+ mixins

BCS ao at pathlink.com
Tue Jul 24 11:48:00 PDT 2007


Reply to Pragma,

> You have to either adopt some template warts to explicitly pass this
> state to every template/CTFE or use something outside D for all
> generated symbols to be unique.  BCS (I think) 

guilty

> suggested a rather
> elegant hack that uses a C preprocessor to work around this
> limitation.

actually I couldn't get it working because I couldn't fake ++ under the preprocessor

> Another suggestion was to couple __LINE__ and __FILE__ to
> get *some* measure of uniqueness per-line.  So it can be done, but it
> won't necessarily be pretty.
> 

The final solution that worked used that (and some other entropy sources 
as well)

|module uniq;
|
|const char[] prefix = "";
|pragma(msg, prefix~"module uniq;")
|
|/** use this template as a unique uint
|
|  make sure that each usage has a unique string (__FILE__":"~itoa!(__LINE__) 
is a good starting point)
|
|  compile the program once, filter the output for lines that start with 
prefix and then recompile with that output replacing this file.
|*/
|template UniqID(char[] str)
|{
|  const uint UniqID = 0;
|  pragma(msg, "template UniqID(char[] str : \""~str~"\") {const uint UniqID 
= __LINE__;}")
|}


> It kinda makes me wish D had a __UNIQUE__ symbol (reliably unique for
> a given module/compilation unit?) for this kind of stuff.
> 
> const char[] sym = __UNIQUE__;
> mixin("for (int " ~ sym ~ "=0; " ~ sym ~ "<10; ++" ~ sym ~ ") { func("
> ~ sym ~ "); }");

make it a GUID... and have fun debugging _e4f74de910de478b8a8c7f79b92f8459(int 
_53e2b8bd63534fafbf3a0fe5caa955b4, float _f1e57ba407ba4b4185cb41f6b902f1cd)





More information about the Digitalmars-d mailing list