Reputation: 1301
I would love to use boost variant with boost IPC shared memory. I'm aware that one should only use pointer free items in shared memory, so was wondering if its safe to use a boost variant as a data type for shared memory.
My understanding of boost variant is that it's size is the size of the largest possible type that the variant covers. From my testing, reading and writing a variant item appears to work with no issues. However I'm unsure of the exact implementation of boost variant, and would appreciate if someone more knowledgeable of boost variant could confirm whether it is safe to use a boost variant in shared memory.
typedef boost::variant< int, double> POSSIBLETYPES;
Server
boost::interprocess::managed_shared_memory memory(boost::interprocess::create_only, "Memory", sizeof(POSSIBLETYPES));
memory.construct<POSSIBLETYPES>("Variant")(1.2);
Client
boost::interprocess::managed_shared_memory memory(boost::interprocess::open_only, "Memory");
std::cout << *memory.find<POSSIBLETYPES>("Variant") << std::endl; // outputs 1.2
Upvotes: 2
Views: 186