I'm using BGL to build a graph storing bundled vertices where one type of vertex stores a reference to the other vertex type. Both types are handled using std::variant:
struct simple_node_t {
size_t enabled;
};
struct complex_node_t {
bool foo1;
size_t foo2;
simple_node_t& control;
};
using vertex_t = std::variant<simple_node_t, complex_node_t>;
using netListGraph_t = boost::adjacency_list<
boost::vecS,
boost::vecS,
boost::undirectedS,
vertex_t>;
Vertices of type complex_node_t are created and stored like this:
// Create node
complex_node_t cpx = {
true,
0xDEADBEEF,
std::get<simple_node_t>(mGraph[desc_to_simple_vertex])
};
// Add complex_node_t to graph
vertex_t vtx(cpx);
auto vertex = boost::add_vertex(vtx, mGraph);
Now the problem:
auto pVal = std::get_if<complex_node_t>(&mGraph[vertex]);
assert(pVal->foo1 == true); //OK
assert(pVal->foo2 == 0xDEADBEEF); //OK
But accessing the reference fails (invalid object)!
**pVal->control.enabled -> GARBAGE**
Storing the data by value works - but storing by reference does not.
What am I doing wrong?
PS: My example is very reduced of course... that means the vertices I want to store via reference are much bigger.
EDIT
I now changed my code:
struct complex_node_t {
bool foo1;
size_t foo2;
std::reference_wrapper<simple_node_t> control;
};
and try to access elements:
if (pVal->control.get().enabled) -> **STILL GARBAGE**