Click here to Skip to main content
Click here to Skip to main content

Tagged as

Unique Identifier for Class in C++

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

Introduction

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
{
.....
ADD_UNIQUE_ID
};
class B: public A
{
.....
// for this class we don't need the unique ID, so don't put anything here
};
class C: public B
{
.....
ADD_UNIQUE_ID
};
// ... 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); 
     UNREFERENCED_PARAMETER(val2);} \
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.

License

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

Share

About the Author

Kosta Cherry

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberPony2791-Nov-12 20:43 
QuestionAlternative PinmemberYiannis Spyridakis1-Nov-12 7:34 
AnswerRe: Alternative PinmemberKosta Cherry1-Nov-12 9:42 
GeneralRe: Alternative PinmemberYiannis Spyridakis1-Nov-12 12:02 
You're right Type2Type won't do if you need to store objects in a map (what type would the key be? Smile | :) )
 
As far as I know there's no guarantee of uniqueness of strings returned by type_info objs, so I wouldn't trust it that much (especially if you're going for portability). But in terms of performance, I don't know how much slower that would be, I guess it depends on your specifications. One thing to keep in mind thought, is that the virtual method will add 4 bytes to each object if it is the class's first (virtual method), so there might be some space overhead there.
Yiannis

GeneralRe: Alternative PinmemberKosta Cherry1-Nov-12 12:36 

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

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

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