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).

Closed due to the fact that the essence of the question is not clear to the participants Vlad from Moscow , Harry , aleksandr barakin , user194374, Alex Nov 27 Nov '16 at 11:58 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And what does not work? ideone.com/jKd8iB - what is wrong here? - Harry
  • one
    Can't you give a minimal example? Would you compile with your type in emplace , and without it, no? Trying to refine your file to a compiled state with a file is too hard a task ... - Harry
  • one
    Well, excuse me too ... There will be a minimal example - I will look, and so I give way to more enlightened comrades :) - Harry
  • what compiler? - ixSci
  • @ixSci - vs2015, on the other compiler it turned out like this: ideone.com/DLEshF - J. Doe

1 answer 1

VC ++ 2015 "understands everything, understands everything ..." (c) :)

 #include <vector> #include <iostream> #include <iomanip> #include <typeinfo> using namespace std; struct stype { stype(stype&& s):ptr(s.ptr) { s.ptr = 0; cout << "stype(const stype&&)\n"; } stype(const stype& s):ptr(s.ptr) { cout << "stype(const stype&)\n"; } ~stype(){ cout << "~stype()\n"; } void* ptr; template<typename T> stype(T v) { ptr = new T(v); cout << "stype(" << typeid(v).name() << " " << v << ")\n"; } }; int main(int argc, const char * argv[]) { vector<stype> v; v.reserve(10); v.emplace_back(5); v.emplace_back(3.6); v.emplace_back(4.1f); v.emplace_back("Hello"); } 

does not give anything extra:

 stype(int 5) stype(double 3.6) stype(float 4.1) stype(char const * Hello) ~stype() ~stype() ~stype() ~stype() 

So - if possible, explain, what is the problem?

Update

Worth doing so:

 class tlist { public: tlist(int a, variant n):axis(a),node(n){} ... tl.emplace_back(axis, f()); 

how it works ...

  • the result is the same ..), maybe there is no difference between emplace or push ?, even push is less than 1 copy cycle, it is not clear, and how {list} works for push and does not work for emplace - J. Doe
  • push receives an object — accordingly, it creates it from the passed arguments, as if push_back(tlist(...)); had been called push_back(tlist(...)); And emplace is a variadically-patterned thing, and it needs parameters for the designer alone. - Harry
  • By the way, here is a minimal example: std::vector<int> il; il.emplace_back({2}); std::vector<int> il; il.emplace_back({2}); - Harry
  • it does not work for me =) (example). The usual int (not a sheet) emplace creates as it is written right on the spot, I have already tried to move and break apart. In any cases, it creates a variant, creates 2 times the tlist copies to the 2nd var and deletes - J. Doe