boostorg / container

STL-like containers from Boost
http://www.boost.org/libs/container/
Boost Software License 1.0
103 stars 113 forks source link

static_vector: Can't emplace a non-movable non-copyable type. #78

Closed jdf98 closed 5 years ago

jdf98 commented 6 years ago

The following code fails to compile, because it tries to move X instead of constructing it in-place. Note that using emplace_back is fine. Tested on godbolt.org with no additional compiler options on:

#include <boost/container/static_vector.hpp>

struct X {
  X() = delete;
  X(X const&) = delete;
  X(X&&) = delete;
  X& operator=(X const&) = delete;
  X& operator=(X&&) = delete;

  int a;
  bool b;
  X(int a, bool b) {
  }
};

int main() {
    boost::container::static_vector<X, 1> v;
    v.emplace(v.cend(), 4, true);
    // v.emplace_back(4, true); // ok
    return 0;
}
igaztanaga commented 5 years ago

The implementation of "emplace", in the general case, needs to move elements (if a value is emplaced in the middle). Passing an end() iterator is a runtime feature, so it's impossible to know at compile time that no element needs to be moved (whereas in "emplace_back", code knows at compile time no element needs to be moved around). I'm closing this as an invalid bug report.