Example of UUID generation using Boost in C++
Asked Answered
A

2

105

I want to generate just random UUID's, as it is just important for instances in my program to have unique identifiers. I looked into Boost UUID, but I can't manage to generate the UUID because I don't understand which class and method to use.

I would appreciate if someone could give me any example of how to achieve this.

Acuity answered 14/7, 2010 at 15:39 Comment(0)
S
190

A basic example:

#include <boost/uuid/uuid.hpp>            // uuid class
#include <boost/uuid/uuid_generators.hpp> // generators
#include <boost/uuid/uuid_io.hpp>         // streaming operators etc.

int main() {
    boost::uuids::uuid uuid = boost::uuids::random_generator()();
    std::cout << uuid << std::endl;
}

Example output:

7feb24af-fc38-44de-bc38-04defc3804de

Skilful answered 14/7, 2010 at 15:51 Comment(8)
And how would you assign it to a string? Because I have a common base for every instance and I would need to concatenate UUID to a base. Thanks again!Acuity
@nik: Use the streaming support - there is a stringstream example. Or let boost::lexical_cast<std::string>(uuid) do that for you.Skilful
As for the double parantheses: The first constructs an instance of random_generator, the second uses operator() on that instance. You should save the generator and call operator() on it if you want to generate more than one uuid: random_generator rg; uuid ui = rg();Skilful
@Acuity : use boost::uuids::to_string(uuid) for stringifying uuidsBechuana
What is the generated UUID in the example based on? I want my UUID to include the network address and time.Astonishing
@danijar: That's a random one. Why do you need to include them? Can't you use/store those two values additionally if you really need them?Skilful
@GeorgFritzsche Together, exact time and machine's individual network address is uniquely. I though therefore it might be good key for the hash function. I don't need the clear values later on. But your idea has brought me an idea. It might be a good to use time and network address as seed for the random number generator or so.Astonishing
@danijar: For practical purposes UUIDs can be considered unique - isn't that enough? Sounds like you really want to ask the question if the random generator that boost::uuids defaults to here is good enough for your usecase. I'd ask about that specifically and just use another one if it isn't.Skilful
M
43

The answer of Georg Fritzsche is ok but maybe a bit misleading. You should reuse the generator if you need more than one uuid. Maybe it's clearer this way:

#include <iostream>

#include <boost/uuid/uuid.hpp>            // uuid class
#include <boost/uuid/uuid_generators.hpp> // generators
#include <boost/uuid/uuid_io.hpp>         // streaming operators etc.


int main()
{
    boost::uuids::random_generator generator;

    boost::uuids::uuid uuid1 = generator();
    std::cout << uuid1 << std::endl;

    boost::uuids::uuid uuid2 = generator();
    std::cout << uuid2 << std::endl;

    return 0;
}
Macule answered 24/4, 2015 at 14:12 Comment(6)
Why should you re-use the generators? Is this a performance optimization or a safety tip?Goldagoldarina
It wouldn't be a very good universally unique ID if using a new generator caused uniqueness problems.Gounod
@Gounod Do you have any documentation regarding the uniqueness problem when creating new generator.Underproof
@Saneeshkumar It's a "universally unique identifier" not a "this generator unique identifier" for a reason.Gounod
Learn from me and make generators thread local. They're very expensive to seedHarcourt
The official documentation only encourage to reuse a random_generator for performance sake, not safetiness. "Depending on the platform there may be a setup cost in initializing the generator so plan to reuse it if you can" Source: Boost uuid 1.70.0Collier

© 2022 - 2024 — McMap. All rights reserved.