How to Generate a UUID in C++
The most portable way to generate a UUID in C++ is Boost.UUID's random_generator, which produces a random, RFC 4122-compliant UUID v4 without depending on any OS-specific API. The r uuid reference explains how to use uuid::UUIDgenerate() for random v4 identifiers in R.
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iostream>
int main() {
boost::uuids::random_generator gen;
boost::uuids::uuid my_uuid = gen();
std::cout << my_uuid << std::endl;
// Output: 550e8400-e29b-41d4-a716-446655440000
return 0;
}
Explanation
#include <boost/uuid/uuid.hpp>and#include <boost/uuid/uuid_generators.hpp>pull in theuuidtype and therandom_generatorthat builds one.boost::uuids::random_generator gen;creates a callable generator object seeded from Boost's cryptographically secure random source.gen()returns aboost::uuids::uuid— calling the generator like a function produces a new random v4 UUID on every call.#include <boost/uuid/uuid_io.hpp>is what makesstd::cout << my_uuidwork; without it the compiler has nooperator<<for theuuidtype.
Convert a String to a UUID in C++
When a UUID arrives as a string — from a config file, command-line argument, or network payload — Boost's string_generator parses it back into a proper boost::uuids::uuid object. For uuid in rust in async web services, the page includes complete Actix-web and Axum handler examples.
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/string_generator.hpp>
#include <iostream>
int main() {
boost::uuids::string_generator gen;
boost::uuids::uuid u = gen("550e8400-e29b-41d4-a716-446655440000");
std::cout << u << std::endl;
return 0;
}
Explanation
boost::uuids::string_generator gen;builds a reusable parser object — construct it once and call it for as many strings as you need.gen("550e8400-...")parses the hyphenated string form and returns aboost::uuids::uuid; it also accepts uppercase and unhyphenated 32-character hex strings.- Malformed input makes
string_generatorthrowstd::runtime_error, so wrap the call in atry/catchblock when parsing untrusted data. - Two
boost::uuids::uuidvalues compare equal with==whenever they represent the same identifier, regardless of how each one was created.
Other Options for Generating UUIDs in C++
Boost.UUID covers most projects, but if you'd rather skip the dependency, both major platforms expose a native UUID/GUID generator you can call directly instead.
#ifdef _WIN32
#include <objbase.h>
#pragma comment(lib, "ole32.lib")
GUID my_guid;
CoCreateGuid(&my_guid);
#endif
#ifdef __linux__
#include <uuid/uuid.h>
uuid_t my_uuid;
char uuid_str[37];
uuid_generate(my_uuid);
uuid_unparse(my_uuid, uuid_str);
#endif
Explanation
CoCreateGuid()(Windows,<objbase.h>, requires linkingole32.lib) fills aGUIDstruct with a random 128-bit identifier using the OS's COM-based generator.uuid_generate()(Linux,<uuid/uuid.h>, from theutil-linux-dev/uuid-devpackage) fills a 16-byteuuid_tarray;uuid_unparse()formats it into the standard hyphenated string.- Both avoid the Boost dependency entirely, at the cost of portability — you'll need
#ifdefguards, or a small wrapper, to support both platforms from one codebase. - Neither exposes namespace-based (v5) generation the way Boost's
name_generator_sha1does, so reach for Boost.UUID when you need deterministic, content-addressed IDs.