C++ to D: __COUNTER__ & #define alternatives

Dominic Jones dominic.jones at gmx.co.uk
Fri Nov 30 15:27:07 UTC 2018


Hello,

I would like to transliterate the following code, below, to D, 
but it seems to be more difficult than I expected.

I cannot find a keyword, like __COUNTER__, that would enable the 
construction of unique types each time UQ is used. Even if such a 
keyword did exist, I cannot find a way wrap it such that I can 
have statements like "auto c0 = UQ(3);".

Any suggestions would be appreciated.

(This question is related to 
https://forum.dlang.org/thread/gqzlchurxylkvwiirrxb@forum.dlang.org)


template<std::size_t ID, typename T>
struct Unique { T value; };

// Would prefer something like "__CPU_CLOCK_TICKS__"
// instead of __COUNTER__, if it were available
#define UQ(v) Unique<__COUNTER__, decltype(v)>{v}

int main()
{
   auto c0 = UQ(3);
   auto c1 = UQ(4);
   static_assert(!std::is_same<decltype(c0), decltype(c1)>::value);
}

(at: https://godbolt.org/z/O7lt5f)



More information about the Digitalmars-d mailing list