The stype constructor allocates memory, if via push_back then it is deleted by the destructor, this is understandable, emplace - cannot understand what type (?), emplace_back(stype({..,..})) has to be added and because of this I think it is called 2 times operator stype(stype& s) and destructor ..
#include <vector> #include <iostream> struct variant { size_t size; void* p; variant(variant& v) { p = new char[v.size];//этого не надо, и вообще этот оператор size = v.size, memcpy(p, vp, size); } template<typename T> variant(T v) { p = new T(v); size = sizeof(T); } template<typename T> operator T&() { return *(T*)p; } template<typename T> T operator->() const { return *(T*)p; } ~variant() { delete p; } }; typedef enum { GUMBO_TAG_HTML, GUMBO_TAG_HEAD } GumboTag; class tlist { public: int axis; variant node; }; GumboTag f() { return GUMBO_TAG_HEAD; } int main(int argc, const char * argv[]) { std::vector<tlist> tl; int axis = 0; // tl.push_back({ axis, f() });//работает без tlist() tl.emplace_back(tlist({ axis, f() })); std::cout << static_cast<GumboTag>(tl[0].node); } you can also ask how best to do it, boost :: variant does not understand enum for some reason, I didn’t like any either (I don’t remember what it’s like, the types of chtoli would have to be stored separately :)
struct tlist { int axis; variant node; template<typename T> tlist(int a, T n) : axis(a), node(n) {} }; This is how it seems to work as it should :), as I thought, templator tlist constructor (I have not checked it in the main program yet).
emplace, and without it, no? Trying to refine your file to a compiled state with a file is too hard a task ... - Harry