Compile time unique identifier
Would there be anyway for the compiler, or the language, to provide a unique ID during compilation?
I've been using UUID generators, but I've always found the approach of copy pasting from a program to code to be kind of... limiting. If I want a random number, can't the compiler guarantee this for me?
It already does the same thing for anonymous namespaces, so...
Any ideas?
Re: Compile time unique identifier
Could you use something involving the __DATE__ and __TIME__ macros?
Re: Compile time unique identifier
Yeah, I think the only standard way to do it is __DATE__ and __TIME__ as laserlight suggested. It's not a number though. You can use a regex an sprintf to create a static number out of it though.
Re: Compile time unique identifier
Well, __DATE__ and __TIME__ are just numbers that change between each compilation, but they aren't unique within a same cpp.
I'm basically trying to do something like this:
Code:
class one
{
static const int id = COMPILE_TIME_UNIQUE_ID;
};
class two
{
static const int id = COMPILE_TIME_UNIQUE_ID;
};
or
Code:
class one : base<COMPILE_TIME_UNIQUE_ID>
{
};
class two : base<COMPILE_TIME_UNIQUE_ID>
{
};
but where COMPILE_TIME_UNIQUE_ID is different for both classes.
I'm wondering if maybe using some meta programming way to do it. Maybe something that increments every time it is evaluated?
Re: Compile time unique identifier
there's boost preprocessor BOOST_PP_COUNTER/BOOST_PP_UPDATE_COUNTER macros to do that portably. But the resulting ids will be unique across all compilation units only if those classes are included in the same order ...
Re: Compile time unique identifier
Re: Compile time unique identifier
Thanks for your replies,
I've learned from your links, but I do not think I'll be able to do what I'm trying. No big deal though.