简体   繁体   中英

Boost::UUID setting specific value

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM