I think that you have already solved this problem for most practical purposes by typing Boost :: Uuid, except for your requirement to dispose of already generated identifiers.
From the documentation related to in question:
When UUIDs are generated by one of certain mechanisms, they are either guaranteed to be unique, different from all other generated UUIDs (which is, it has never been created before and it will never be created again), or it will most likely be unique (depending from the mechanism).
If you are struggling to recycle and reuse existing identifiers, I suppose you could maintain the UUID assembly over time, generating new ones only when you need it, and find that the pool is empty. But I can’t imagine a scenario where it would be preferable to generate a new UUID.
EDIT . You commented that you need a guarantee of uniqueness. In fact, you will never get it when programmatically creating a unique identifier. In practice, you are going to store the generated identifier in a data type that has a finite size, and therefore the possible set of identifiers that you can generate is also finite. IMHO, the best you can achieve is to simulate uniqueness within the tolerance threshold.
You can do it with
Using a technique that makes it possible to get a duplicate UUID very remotely (Boost :: UUID will do this);
A wrapper for generating a highly vulnerable UUID may be unique in some other logic that looks for a newly created UUID in the list of already created UUIDs to eliminate this tiny possibility that the new one is a duplicate. Obviously, the practicality of this decreases as you approach the large number of UUIDs on your list. How much do you expect generation?
If you want a really huge number of unique identifiers, more than suitable for the native type, you can implement a type that manages memory and performs the necessary mathematical calculations, and just create sequential identifiers, or you could use something like the GNU Bignum Library , to do it for you.
source share