Compile time unique identifier
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Compile time unique identifier

  1. #1
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    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 LITE 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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: Compile time unique identifier

    Could you use something involving the __DATE__ and __TIME__ macros?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    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.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    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 LITE 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.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,168

    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 ...

  6. #6
    Join Date
    Nov 2003
    Posts
    1,825

    Re: Compile time unique identifier

    There's __LINE__ and __COUNTER__ with MSVC.
    http://msdn.microsoft.com/en-us/library/b0084kay.aspx

    gcc has __COUNTER__ as well.
    http://gcc.gnu.org/onlinedocs/cpp/Co...defined-Macros

    gg

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    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 LITE 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center