一些开发人员显式调用构造函数和析构函数以用于某些解决方法。我知道,这不是一个好的做法,但似乎这样做是为了实现一些场景。
例如,在这篇文章中,美丽的原生库,作者使用了这种技术。
在下面的代码中,最后可以看到构造函数被显式调用:
#include <limits>
template <class T>
struct proxy_allocator {
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T *pointer;
typedef const T *const_pointer;
typedef T& reference;
typedef const T &const_reference;
typedef T value_type;
template <class U>
struct rebind {
typedef proxy_allocator<U> other;
};
proxy_allocator() throw() {}
proxy_allocator(const proxy_allocator &) throw() {}
template <class U>
proxy_allocator(const proxy_allocator<U> &) throw() {}
~proxy_allocator() throw() {}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
pointer allocate(size_type s, void const * = 0) {
return s ? reinterpret_cast<pointer>(yl_malloc(s * sizeof(T))) : 0;
}
void deallocate(pointer p, size_type) {
yl_free(p);
}
size_type max_size() const throw() {
return std::numeric_limits<size_t>::max() / sizeof(T);
}
void construct(pointer p, const T& val) {
new (reinterpret_cast<void *>(p)) T(val);
}
void destroy(pointer p) {
p->~T();
}
bool operator==(const proxy_allocator<T> &other) const {
return true;
}
bool operator!=(const proxy_allocator<T> &other) const {
return false;
}
};
对于像这样的某些场景,可能需要显式调用构造函数和析构函数,但标准是怎么说的:它是未定义的行为,是未指定的行为,是实现定义的行为,还是定义良好的行为?
是的,它受支持且定义良好,它是安全的。
new (reinterpret_cast<void *>(p)) T(val);
称为放置新语法,并用于在特定内存位置构造对象,默认行为;如在分配器中发布时需要的。如果放置 new 针对特定类型重载 T
,则将调用它而不是全局放置 new。
此类构造对象的唯一方法是显式调用析构函数p->~T();
。
使用放置 new 和显式销毁确实需要/允许实现的代码控制对象的生存期 - 在这种情况下,编译器提供的帮助很少;因此,在对齐良好且分配充分的位置构造对象很重要。它们的使用经常出现在分配器中,例如在 OP 和 std::allocator
中。
是的,它是完全安全的。事实上,所有像std::vector
这样的标准容器都默认使用该技术,因为它是将内存分配与元素构造分开的唯一方法。
更准确地说,标准容器模板有一个默认为 std::allocator
的 Allocator
模板参数,并且std::allocator
在其 allocate
成员函数中使用放置新。
例如,这允许std::vector
实现push_back
,这样内存分配就不必一直发生,而是在当前容量不再足够时分配一些额外的内存,为将来添加的元素准备空间push_back
。
这意味着,当您在循环中调用push_back
一百次时,std::vector
实际上足够聪明,不会每次都分配内存,这有助于提高性能,因为重新分配现有容器内容并将其移动到新的内存位置的成本很高。
例:
#include <vector>
#include <iostream>
int main()
{
std::vector<int> v;
std::cout << "initial capacity: " << v.capacity() << "n";
for (int i = 0; i < 100; ++i)
{
v.push_back(0);
std::cout << "capacity after " << (i + 1) << " push_back()s: "
<< v.capacity() << "n";
}
}
输出:
initial capacity: 0
capacity after 1 push_back()s: 1
capacity after 2 push_back()s: 2
capacity after 3 push_back()s: 3
capacity after 4 push_back()s: 4
capacity after 5 push_back()s: 6
capacity after 6 push_back()s: 6
capacity after 7 push_back()s: 9
capacity after 8 push_back()s: 9
capacity after 9 push_back()s: 9
capacity after 10 push_back()s: 13
capacity after 11 push_back()s: 13
capacity after 12 push_back()s: 13
capacity after 13 push_back()s: 13
capacity after 14 push_back()s: 19
(...
capacity after 94 push_back()s: 94
capacity after 95 push_back()s: 141
capacity after 96 push_back()s: 141
capacity after 97 push_back()s: 141
capacity after 98 push_back()s: 141
capacity after 99 push_back()s: 141
capacity after 100 push_back()s: 141
但是,当然,您不想为潜在的未来元素调用构造函数。对于int
来说这无关紧要,但是我们需要为每个T
提供一个解决方案,包括没有默认构造函数的类型。这就是放置 new 的强大功能:首先分配内存,然后使用手动构造函数调用将元素放入分配的内存中。
作为旁注,所有这些都是不可能的 new[]
.事实上,new[]
是一种非常无用的语言功能。
PS:仅仅因为标准容器在内部使用放置new,这并不意味着你应该在自己的代码中疯狂地使用它。这是一种低级技术,如果您没有实现自己的通用数据结构,因为没有标准容器提供您需要的功能,您可能永远不会找到它的任何用途。