I was using emplace_back on a vector of my class A
that has a const reference member variable of class Handle
with some success. However I needed to change it to use emplace
instead to be able to insert at certain index. But that doesn't seem to compile. Looking at cppreference, I can't tell what the difference between the two methods is, except that one takes an explicit iterator
#include <vector>
class Handle
{
int do_something();
};
class A
{
public:
A(int a, float d, const Handle& h) : a_(a), d_(d), h_(h) {}
private:
int a_;
float d_;
const Handle& h_;
};
int main()
{
Handle h;
std::vector<A> foo;
foo.reserve(10);
foo.emplace_back(3, 4.5, h); // OK
// foo.emplace(foo.begin() + 3, 3, 4.5, h); // NOT OK
}
I get an error on using emplace
:
In file included from main.cpp:7:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/vector:1759:18: error: object of type 'A' cannot be assigned because its copy assignment operator is implicitly deleted
*__p = _VSTD::move(__tmp.get());
^
main.cpp:41:6: note: in instantiation of function template specialization 'std::vector<A>::emplace<int, double, Handle &>' requested here
foo.emplace(foo.begin() + 3, 3, 4.5, h);
^
main.cpp:22:16: note: copy assignment operator of 'A' is implicitly deleted because field 'h_' is of reference type 'const Handle &'
const Handle& h_;
Do I need to change my class design to accommodate using emplace
?
I found this question but it doesn't seem to provide an alternative
Aucun commentaire:
Enregistrer un commentaire