Boost logo

Boost :

From: Sid Sacek (ssacek_at_[hidden])
Date: 2007-12-04 00:54:04

Here's a dumb idea, but perhaps somebody here (smarter than me) can turn it into one that is actually a useful idea.

MSVC compilers have an extra predefined macro named __COUNTER__. This is Microsoft-specific macro that might perhaps be what you're
looking for to help you create those unique identifiers. The MSDN description of the macro is as follows:


Expands to an integer starting with 0 and incrementing by 1 every time it is used in a compiland. __COUNTER__ remembers its state
when using precompiled headers. If the last __COUNTER__ value was 4 after building a precompiled header (PCH), it will start with 5
on each PCH use.


Other compilers don't have this built-in macro, but it seems to me that you can create something useful with compiler arguments. For

g++ main.cpp -D_FILE_ORDINAL_=10000

You also need these macros somewhere that use the ordinal value as follows:

#define _COUNTER3( O, L ) O##L
#define _COUNTER2( O, L ) _COUNTER3( O, L )

If every compiled file gets its own large ordinal number, you should never have the same value for _COUNTER twice. Of course the
implication is that it's not handled purely by the C++ language. But who knows, if one needs to use boost code that relies on this
sort of contraption, maybe they'd be willing to modify their Makefiles. The macros could also be injected directly into the source
code itself like this:

#define _FILE_ORDINAL_ 10000
#include <boost/some-boost-file.h>

int main()
    printf( "Value = %d \n", _COUNTER );
    printf( "Value2 = %d \n", _COUNTER );

-Sid Sacek

-----Original Message-----
From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]] On Behalf Of Mathias Gaunard
Sent: Sunday, December 02, 2007 1:04 PM
To: boost_at_[hidden]
Subject: [boost] Generating unique identifiers at compile-time

Meta-Programming, as used in Boost.MPL and others, allows to compute
arbitrary values at compile-time, in a Turing-complete manner.

However, something like generating unique identifiers at compile-time,
which would be fairly useful, requires some kind of entropy source to
create a random seed.

One can maybe extract some entropy from macros such as __DATE__ and
__TIME__, using both preprocessing and templates.

Among you boosters, which are well known for your interesting in
meta-programming, is there someone who ever tried to look into this?

Unsubscribe & other changes:

Boost list run by bdawes at, gregod at, cpdaniel at, john at