为什么有两个std::allocator::construct函数?
why are there two std::allocator::construct functions?
标准在std::allocator<T>
中给出了两个构造函数,用于在下面放置新语法:
void construct( pointer p, const_reference val ); (1) (until C++11)
template< class U, class... Args >
void construct( U* p, Args&&... args ); (2) (since C++11)
1) Calls new((void *)p) T(val)
2) Calls ::new((void *)p) U(std::forward<Args>(args)...)
1)和2)除了将所有参数转发给2)中的构造函数之外,还有什么区别?我们为什么需要1)?
假设我们只有签名2),那么从(不存在的)第一个签名传递参数将导致调用:
::new((void *)p) T(std::forward<const_reference>val)
应该调用复制构造函数T(val)吗?在这里,我要问的是,多签一个字有什么意义?不同的是一个叫new
另一个叫全局函数::new
谢谢你的帮助:-)
答案在您发布的摘录中:一个是c++ 11之前的,一个是c++ 11之后的。construct
最好通过完全转发参数来表达,而不是调用复制构造函数,但是完全转发仅在c++ 11中可用,因此我们必须在第一个选项成为现实之前使用第一个选项。
相关文章:
- std::set<Key,Compare,Allocator>::find() 函数使用"<"运算符而不是"=="运算符背后的直觉是什么?
- 未定义模板"std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator&
- 我的类中应该有一个Allocator作为成员变量吗
- std::allocator_traits::construct调用了错误的构造函数
- 将赋值运算符实现为"destroy + construct"是否合法?
- Construct unordered_map with a value_type of unique_ptr
- 返回的向量定义为 "std::vector<<error-type>, std::allocator<<error-type>>>
- 为什么 std::optional 不允许"move construct and copy assign only"类型的移动分配?
- ASIO UDP:类 std::分配器<void>没有名为"construct"的成员
- C++编译器是否允许用一个构造来代替construct+moveconstruct
- basic_string allocator
- gcc 未给出的 Clang 错误"attempted to construct a reference element in a tuple with an rvalue"
- 错误:对BSTLink::construct(BST*、BST*和BST*)的调用没有匹配的函数
- 应该分配器 construct() 默认初始化而不是值初始化
- C++对原始STL实现中"construct"方法逻辑的理解
- boost:spirit::qi 解析器使用多种语法和 phoenix::construct
- Clang 输出错误"no matching construct for initialization"
- 向量(容器)需要使用"allocator"吗?
- 如果对象是可构造/可析构的,STL容器是否允许跳过调用allocator::construct和allocator::d
- 为什么有两个std::allocator::construct函数?