I wonder if I understood emplace_back correctly
#include <vector>
using namespace std;
struct Hero {
Hero(const string&) {}
Hero(const char*) {}
Hero(int) {}
// forbid a clone:
Hero(const Hero&) = delete;
Hero& operator=(const Hero&) = delete;
};
int main() {
vector<Hero> heros1 = { "Bond", "Hulk", "Tarzan" }; // ERR: copies?
vector<Hero> heros;
heros.emplace_back( 5 ); // ERR: copies
heros.emplace_back( string("Bond") ); // ERR: copies
heros.emplace_back( "Hulk" ); // ERR: copies
}
Thus, I am really wondering If I understood emplace_back incorrectly: I though it would prevent to make a copy of Hero, because it creates the Item in-place.
Or is it a implementation error in my g++-4.7.0?
You need to define a move constructor and move-assignment operator, like this:
This allows values of type Hero to be moved into the function. Move is usually faster than copy. If the type is neither copyable nor movable then you cannot use it in a
std::vector.