放置 new 和 uninitialized_fill() 的行为
Behavior of placement new and uninitialized_fill()
我看到以下uninitialized_fill
实现中使用了新放置。 有谁知道放置 new 的底层实现或在 g++ 中在哪里可以找到它?
我假设运算符在调用复制构造函数后获取指向对象应放置位置的指针以及对Value(value)
哪个是右值的引用?
谢谢
template<class ForwardIt, class T>
void uninitialized_fill(ForwardIt first, ForwardIt last, const T& value)
{
typedef typename std::iterator_traits<ForwardIt>::value_type Value;
ForwardIt current = first;
try {
for (; current != last; ++current) {
::new (static_cast<void*>(std::addressof(*current))) Value(value);
}
} catch (...) {
for (; first != current; ++first) {
first->~Value();
}
throw;
}
}
placement-new
指定地址构造一个对象,是的。
在表达式::new (static_cast<void*>(std::addressof(*current))) Value(value);
中,*current
返回对指定迭代器范围内元素的引用,然后将该元素的地址转换为void*
并传递给placement-new
,然后 在该地址构造一个新的Value
类型(迭代器的value_type
(,将输入value
传递给该类型的复制构造函数。
此代码中没有使用右值。Value(value)
不是传递给placement-new
的右值。Value
是placement-new
构造的类型,value
作为Value's
构造函数的输入。 如果Value
被命名为更像ValueType
或TypeToConstruct
的东西,也许代码会更有意义。
new (address) Type(params)
表达式类似于new Type(params)
表达式,只是使用额外的address
参数作为输入。
相关文章:
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 重载运算符new[]的行为取决于析构函数
- 过载'operator new'如何导致无限循环?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- Codelite C++ new project
- 在类c++中使用new声明数组
- g++用户定义的动态链接库上的全局new和delete运算符
- 如果整个应用程序是虚拟映射的,为什么 new 会进行系统调用?
- 在将 new 与指针一起使用时,创建数组的指定长度
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- 为什么我的全局 new() 覆盖被绕过了?
- 您可以使用 setfill() 来设置 fill 2 位数字吗?
- 声明C++数组(带或不带 "new" 关键字)
- 在C++中,如果我可以直接将整数分配给指针而不使用"new",为什么要使用"new"?
- 是否可以使用 new 指定具有宏常量的动态分配数组的元素?
- constexpr new 如何分配内存?
- 放置 new 和 uninitialized_fill() 的行为
- 使用 new 和 值进行数组初始化,但没有显式数量的元素
- different between int **arr =new int [ n]; and int a[i][j]?