Boost::UUID setting specific value

581 Views Asked by At

In Java UUID Class there is a way to set upper and lower longs to create a specific value for UUID.

UUID test(-1, -1);

will generate UUID: ffffffff-ffff-ffff-ffff-ffffffffffff

Is there a similar method to create one with BOOST UUID in C++ other then creating a string version of it and converting it back to UUID?

1

There are 1 best solutions below

8
On

Passing integers to constructor would be ambiguous because you don't know what UUID they would generate as integers can have different byte representations on different platforms. Sure, all-ones (as in -1) and all-zeros make it more or less obvious, but what about other integer values?

The solution is either construct UUID from string or from bytes.

boost::uuids::uuid u1{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }};

boost::uuids::uuid u2 = boost::uuids::string_generator()("ffffffff-ffff-ffff-ffff-ffffffffffff");

std::istringstream strm("ffffffff-ffff-ffff-ffff-ffffffffffff");
boost::uuids::uuid u3;
strm >> u3;