Click here to Skip to main content
13,147,956 members (88,834 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 27 Oct 2012

Unique Identifier for Class in C++

, 30 Oct 2012
Rate this:
Please Sign up or sign in to vote.
Unique identifier for the class


Recently I stumbled upon a task of adding a unique identifier to classes (not instances!) in my project (why I needed that is a different question with too long an answer to discuss here). Those identifiers didn't need to be consistent from run to run, nor did they need to have any specific values - just uniqueness within one run was enough.

There are quite a few ways to achieve that, but I wanted the one that requires the least amount of coding, provides ID of an integer type (for performance reasons, because it was later used as a key in the map), and doesn't clutter my classes too much with extra data or code.

Quick Googling gave me an elegant solution. The key idea is that functions should have some entry address, which means - this is unique, integer information. So, the original code looked like this:

#define ADD_UNIQUE_ID \ 
private: \ 
    static const size_t getUniqueIdPrivate() 
    { return reinterpret_cast<size_t>(&getUniqueIdPrivate); } \
public: \ 
    virtual const size_t getUniqueId() const {return getUniqueIdPrivate();};  

And usage looked like this:

class A
class B: public A
// for this class we don't need the unique ID, so don't put anything here
class C: public B
// ... etc ... 

Well, this worked perfectly in debug mode. Happy, I switched to release mode, and all of a sudden, getUniqueId() started to return the very same value. What happened?!

Few hours later, and looking at assembler code, I found the answer: "COMDAT folding". Linker was too smart - it figures out that function body is the same all the time, so it moves all getUniqueIdPrivate() function to the same address. Bummer.

So, the solution was to add different code to each of those static functions. Note that we never call this function directly, we just take its address, so code inside can be inefficient enough, we just need to make sure that final COMDAT code is different. And, here is the final code:

#define ADD_UNIQUE_ID \
private: \
     static void getUniqueIdPrivate() { std::string val(_T(__FILE__)); 
     volatile int val2 = __LINE__ ; UNREFERENCED_PARAMETER(val); 
public: \
     virtual inline const size_t getUniqueId() const 
     {return reinterpret_cast<size_t>(getUniqueIdPrivate);}; 

It was tested in debug/release x32/x64 on VC9, VC10 and VC11. If someone will bother to test it in other compilers, please comment.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kosta Cherry
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Pony2791-Nov-12 19:43
memberPony2791-Nov-12 19:43 
nice work!
QuestionAlternative Pin
Yiannis Spyridakis1-Nov-12 6:34
memberYiannis Spyridakis1-Nov-12 6:34 
AnswerRe: Alternative Pin
Kosta Cherry1-Nov-12 8:42
memberKosta Cherry1-Nov-12 8:42 
GeneralRe: Alternative Pin
Yiannis Spyridakis1-Nov-12 11:02
memberYiannis Spyridakis1-Nov-12 11:02 
GeneralRe: Alternative Pin
Kosta Cherry1-Nov-12 11:36
memberKosta Cherry1-Nov-12 11:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 31 Oct 2012
Article Copyright 2012 by Kosta Cherry
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid