3 ms·
Why do you need to use pointers for string and vector? Aren't they already heap allocated internally?
by b169118 11y ago
Why do you need to use pointers for string and vector? Aren't they already heap allocated internally?
- loup-vaillant 11y agoMy code compiles as is, so you can try yourself if you want. If I didn't use pointers, I would have 2 errors. One would be about the use of an incomplete data type (Expression), and the other about trying to put something that doesn't have a "trivial constructor" in a union (String, Value, and std::vector<Expression>). I can't use references either, because I would have to initialise them in the initialisation list of my constructor, which I can't do until I know the tag. I could still initialise the tag first, but that's specified by the order in which the data members are defined, rather than the order of the initialisation list —a rather dangerous idiom to rely on, should anyone modify the code.
- ksherlock 11y agoIt's extra work, but you could use std::aligned_union<1, std::string, std::vector<Expression>, ...> to declare space for the union and then use placement new/manual destructor calls to initialize and destroy.
- gpderetta 11y agoNote that in C++11 is perfectly fine to put a type with a non trivial constructor in an union. You are right about the issue with std::vector and incomplete types. It will be possibly resolved in the next standard. In the meantime you can use something like boost::container::vector which explicitly support incomplete types.
- deleted 11y ago[deleted]