为什么这个 std::vector::emplace_back 失败了?
Why does this std::vector::emplace_back fail?
我遇到了一个编译器错误:
attempting to reference a deleted function
#include <iostream>
#include <vector>
template <typename T>
struct Container
{
Container() = default;
Container(const Container& other) = delete;
Container(T* ptr) : ptr(ptr) {}
T* ptr;
~Container() { delete ptr; }
};
struct Foo { Foo(int a, int b) {} };
int main()
{
std::vector<Container<Foo>> myvector;
myvector.push_back(new Foo(1, 2)); // I understand why this doesn't work.
myvector.emplace_back((new Foo(1, 2))); // I don't understand why this fails
}
我理解为什么在我执行 std::vector::push_back()
时它说尝试引用 已删除的构造函数 ,因为它会复制并需要调用 复制构造函数,我删除了。
但是 std::vector::emplace_back()
应该采用它持有的类型的构造函数参数。当我放回去时,我给它一个指向 Foo
的指针,这应该被转发给 Container::Container(T* ptr)
构造函数。
我错过了什么?
声明一个User-Defined复制构造函数将不会定义一个隐式移动构造函数; T
必须有一个 复制构造函数 或 移动构造函数 到 push_back
或 emplace_back*
一个对象一个std::vector<T>
。
来自docs,参见T
上的要求来实例化一个std::vector<T>
。 (这里没有限制,请继续阅读)..强调我的
The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements.
来自 std::vector<...>::push_back
:
Type requirements
T
must meet the requirements of CopyInsertable in order to use overload (1).
T
must meet the requirements of MoveInsertable in order to use overload (2).
来自 std::vector<...>::emplace_back
:
Type requirements
T
(the container's element type) must meet the requirements of MoveInsertable and EmplaceConstructible.
对于此处的 emplace_back
,您的代码将满足 EmplaceConstructible 标准,但是,由于可能会发生重新分配,因此您 必须 同样实现MoveInsertable.
我遇到了一个编译器错误:
attempting to reference a deleted function
#include <iostream>
#include <vector>
template <typename T>
struct Container
{
Container() = default;
Container(const Container& other) = delete;
Container(T* ptr) : ptr(ptr) {}
T* ptr;
~Container() { delete ptr; }
};
struct Foo { Foo(int a, int b) {} };
int main()
{
std::vector<Container<Foo>> myvector;
myvector.push_back(new Foo(1, 2)); // I understand why this doesn't work.
myvector.emplace_back((new Foo(1, 2))); // I don't understand why this fails
}
我理解为什么在我执行 std::vector::push_back()
时它说尝试引用 已删除的构造函数 ,因为它会复制并需要调用 复制构造函数,我删除了。
但是 std::vector::emplace_back()
应该采用它持有的类型的构造函数参数。当我放回去时,我给它一个指向 Foo
的指针,这应该被转发给 Container::Container(T* ptr)
构造函数。
我错过了什么?
声明一个User-Defined复制构造函数将不会定义一个隐式移动构造函数; T
必须有一个 复制构造函数 或 移动构造函数 到 push_back
或 emplace_back*
一个对象一个std::vector<T>
。
来自docs,参见T
上的要求来实例化一个std::vector<T>
。 (这里没有限制,请继续阅读)..强调我的
The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements.
来自 std::vector<...>::push_back
:
Type requirements
T
must meet the requirements of CopyInsertable in order to use overload (1).T
must meet the requirements of MoveInsertable in order to use overload (2).
来自 std::vector<...>::emplace_back
:
Type requirements
T
(the container's element type) must meet the requirements of MoveInsertable and EmplaceConstructible.
对于此处的 emplace_back
,您的代码将满足 EmplaceConstructible 标准,但是,由于可能会发生重新分配,因此您 必须 同样实现MoveInsertable.