   avoiding unneeded copies
  I want to store a large number of structs in an std::set. The problem is I don't want it to create a copy egMyStruct MakeStruct(){...do something...   return NewStruct;}...MySet.insert(MakeStruct());So if my understanding is right I just copied that struct 2 times...1- A copy is made to be returned by the MakeStruct function. The origenal then goes out of scope and is deleted2- The insert method then makes it's own copy of the struct. The first copy goes out of scope and is deletedIs there someway I can just make the final struct straight off?I'm aware I could store a pointer to the struct but I'd rather not if I don't have too...   
  No, although an optimizing compiler might optimize this out (for instance, the first function might undergo NRVO and thus contain no structure copy). If copying your objects into a set proves to be a bottleneck (which I seriously doubt), the key is to optimize their copy constructor.   ;  Examine the assembly first. STL containers get reasonably well optimized under current compilers.And with various optimization techniques (NRVO), generalizing about this is impractical.For trivial structs and certain types of values, insert may indeed expand into in-place insertion.I spent quite some time studying the behaviour of containers for the use in messaging system, and sometimes I encountered problems due to amount of code that wasn't generated since compiler proved it redundant.The biggest obstacles to this aggressive optimization I've found to be virtual functions, function pointers under gcc (refuses to do even perfectly obvious optimizations) and "code bloat" due to exception handlers (the extra exception handlers would add just enough code so that the call would not be inlined, but would result in function call).But there are no rules of any kind, compile, loot at assembly.   