skypjack skypjack - 1 year ago 69
C++ Question

CRTP and unique persistent identifiers

Consider the following code:

#include <iostream>
#include <cstdlib>
#include <ctime>

struct BaseClass {
static int identifier() {
static int identifier_counter = 0;
return identifier_counter++;

template <class D>
struct Class: public BaseClass {
static int identifier() {
static int class_identifier = BaseClass::identifier();
return class_identifier;

struct A: public Class<A> { };
struct B: public Class<B> { };

int main() {
int r = std::rand()%2;

if(r) {
std::cout << "A: " << A::identifier() << std::endl;
std::cout << "B: " << B::identifier() << std::endl;
} else {
std::cout << "B: " << B::identifier() << std::endl;
std::cout << "A: " << A::identifier() << std::endl;

It's a reduced, but still plausible representation of the problem.

Any derived class will have a specific, different identifier on runtime and two instances of the same type will share the same identifier. Surely a good solution for such a problem.

Unfortunately, those identifiers depend on the order on which the
members are invoked (we can see it easily by running multiple times the example). In other words, given two classes
, if it happens that running twice the software their
members are invoked in different order, they have different identifiers.

My problem is that, for some reasons, I need to store those identifiers somewhere and let them survive the single execution, so that I can reason on the original types once the application runs once more and decide to read those values from the storage.

An alternative would be to use hash_code from
, but it suffers from other problems. Another solution would be to force the calls to the
members during the bootstrap of the application, but this one also has several drawbacks.

I'd like to know if there is so far an easy to implement though still elegant solution that is completely transparent to the developer to identify types over several executions, as the one above is for the single run of the application.

Answer Source

The problem of having unique persistent identifier for every class is unsolvable with C++. Sorry. You will either depend on the order of calling your initializaer functions, or, if you call them from initializers of static objects, on the order of static initializer (which will usually depend on the order of your object files in your link line).

And of course, there is no guarantee that hash will be unique.

You will have to use external script for this. In particular, something like this might be used:

// when class is first created
class Foo {
  static int class_id = ?CLASS_ID?;

// after class is process by the script 
class Foo {
  static int class_id = 123; // Autogenerated by ''

You might have a perl script running as part of the compilation (the very first thing) which opens all .h files in the project directory, reads all of them, counts all instances of Autogenerated by '' and than stamps all ?CLASS_ID? with incremented counter (starting from the number of already generated ids). To add some safety, you might want a better pattern than simple ?...?, but I think, you got the idea.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download