-
January 18th, 2012, 10:09 AM
#1
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?
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
January 18th, 2012, 10:24 AM
#2
Re: Compile time unique identifier
Could you use something involving the __DATE__ and __TIME__ macros?
-
January 18th, 2012, 10:59 AM
#3
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.
-
January 18th, 2012, 11:48 AM
#4
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?
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
January 18th, 2012, 12:24 PM
#5
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 ...
-
January 19th, 2012, 12:15 AM
#6
Re: Compile time unique identifier
-
January 19th, 2012, 03:27 AM
#7
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|