Search code examples
c++perfect-forwarding

C++ std::vector::emplace_back doesn't compile with string-value class


/* Item.h */
struct Item {
    std::string name;

    Item(std::string _name) : name( std::move(_name) ) { }
};

/* main.cpp */
/* ... */
const int amount_of_items = val.size();
std::vector<Item> items(amount_of_items);

for( Json::Value::const_iterator itr = val.begin() ; itr != val.end() ; ++itr ) {
    items.emplace_back( "item_name" );
}

Results in:

/usr/include/c++/8/bits/stl_construct.h:75:7: error: no matching function for call to ‘Item::Item()’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from main.cpp:8: Item.h:8:2: note: candidate: ‘Item::Item(std::__cxx11::string)’   Item(std::string _name) : name( std::move(_name) ) { }   ^~~~ Item.h:8:2: note:   candidate expects 1 argument, 0 provided

I don't know why this wouldn't work - any ideas?


Solution

  • The issue is that you need a default constructor of Item for this sequence of code. The default constructor is disabled because you have a custom constructor for Item.

    You can enable it by adding Item()=default;.

    Besides that, you have a logical bug: std::vector<Item> items(amount_of_items); initiates items to amount_of_items of default constructible elements. It isn't what you want according to the next sequence as by the end you'll have double the number of elements.

    You should've written

    std::vector<Item> items;
    items.reserve(amount_of_items);