wal*_*nut 8 c++ allocator c++17
::std命名空间中的类模板通常可以由程序专门用于用户定义的类型。我没有发现此规则的任何例外情况std::allocator。
那么,我可以专攻std::allocator我自己的类型吗?如果我被允许,我是否需要提供std::allocator的主模板的所有成员,因为其中许多成员可以由std::allocator_traits(因此在 C++17 中被弃用)?
考虑这个程序
#include<vector>
#include<utility>
#include<type_traits>
#include<iostream>
#include<limits>
#include<stdexcept>
struct A { };
namespace std {
template<>
struct allocator<A> {
using value_type = A;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using propagate_on_container_move_assignment = std::true_type;
allocator() = default;
template<class U>
allocator(const allocator<U>&) noexcept {}
value_type* allocate(std::size_t n) {
if(std::numeric_limits<std::size_t>::max()/sizeof(value_type) < n)
throw std::bad_array_new_length{};
std::cout << "Allocating for " << n << "\n";
return static_cast<value_type*>(::operator new(n*sizeof(value_type)));
}
void deallocate(value_type* p, std::size_t) {
::operator delete(p);
}
template<class U, class... Args>
void construct(U* p, Args&&... args) {
std::cout << "Constructing one\n";
::new((void *)p) U(std::forward<Args>(args)...);
};
template<class U>
void destroy( U* p ) {
p->~U();
}
size_type max_size() const noexcept {
return std::numeric_limits<size_type>::max()/sizeof(value_type);
}
};
}
int main() {
std::vector<A> v(2);
for(int i=0; i<6; i++) {
v.emplace_back();
}
std::cout << v.size();
}
Run Code Online (Sandbox Code Playgroud)
这个程序使用 libc++ (Clang with -std=c++17 -Wall -Wextra -pedantic-errors -O2 -stdlib=libc++) 的输出是:
Allocating for 2
Constructing one
Constructing one
Allocating for 4
Constructing one
Constructing one
Allocating for 8
Constructing one
Constructing one
Constructing one
Constructing one
8
Run Code Online (Sandbox Code Playgroud)
和 libstdc++ (Clang with -std=c++17 -Wall -Wextra -pedantic-errors -O2 -stdlib=libstdc++)的输出是:
Allocating for 2
Allocating for 4
Constructing one
Constructing one
Allocating for 8
Constructing one
Constructing one
Constructing one
Constructing one
8
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,libstdc++ 并不总是尊重construct我提供的重载,如果我删除construct,destroy或max_size成员,那么程序甚至不会使用 libstdc++ 编译抱怨这些缺失的成员,尽管它们是由std::allocator_traits.
程序是否有未定义的行为,因此两个标准库都是正确的,还是程序的行为定义明确并且标准库需要使用我的专业化?
请注意,有一些来自std::allocator的主模板的成员在我的专业领域中仍然被遗漏了。我还需要添加它们吗?
准确地说,我遗漏了
using is_always_equal = std::true_type
Run Code Online (Sandbox Code Playgroud)
这是由提供的,std::allocator_traits因为我的分配器是空的,但将是std::allocator的接口的一部分。
我还留出pointer,const_pointer,reference,const_reference,rebind和address,所有这些都通过提供std::allocator_traits和在C ++ 17弃用std::allocator的接口。
如果您认为需要定义所有这些来匹配std::allocator的接口,那么请考虑将它们添加到代码中。
根据 23.2.1 [container.requirements.general]/3:
对于受本子条款影响且声明 的组件,应使用以下函数
allocator_type构造存储在这些组件中的对象allocator_traits<allocator_type>::construct
另外,根据 17.6.4.2.1:
std仅当声明依赖于用户定义的类型并且专门化满足原始模板的标准库要求且未明确禁止时,程序才可以将任何标准库模板的模板专门化添加到命名空间。
我不认为该标准禁止专门化std::allocator,因为我通读了所有章节,std::allocator但它没有提到任何内容。我还查看了禁止专业化的标准,但没有找到类似的内容std::allocator。
的要求Allocator在这里,您的专业满足这些要求。
因此,我只能得出结论,libstdc++实际上违反了标准(也许我在某个地方犯了错误)。我发现,如果只是专门化std::allocator,libstdc++ 将通过对构造函数使用放置 new 来响应,因为它们有专门针对这种情况的模板专门化,同时使用指定的分配器进行其他操作;相关代码在这里(这是在namespace std;allocator这里是::std::allocator):
// __uninitialized_default_n_a
// Fills [first, first + n) with n default constructed value_types(s),
// constructed with the allocator alloc.
template<typename _ForwardIterator, typename _Size, typename _Allocator>
_ForwardIterator
__uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
_Allocator& __alloc)
{
_ForwardIterator __cur = __first;
__try
{
typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
for (; __n > 0; --__n, (void) ++__cur)
__traits::construct(__alloc, std::__addressof(*__cur));
return __cur;
}
__catch(...)
{
std::_Destroy(__first, __cur, __alloc);
__throw_exception_again;
}
}
template<typename _ForwardIterator, typename _Size, typename _Tp>
inline _ForwardIterator
__uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
allocator<_Tp>&)
{ return std::__uninitialized_default_n(__first, __n); }
Run Code Online (Sandbox Code Playgroud)
std::__uninitialized_default_nstd::_Construct使用新布局的调用。这解释了为什么您在输出中的“分配 4”之前看不到“构造一个”。
编辑:
正如OP在评论中指出的那样,std::__uninitialized_default_n调用
__uninitialized_default_n_1<__is_trivial(_ValueType)
&& __assignable>::
__uninit_default_n(__first, __n)
Run Code Online (Sandbox Code Playgroud)
它实际上有一个专业化,如果__is_trivial(_ValueType) && __assignable是true,就在这里。它使用std::fill_n(wherevalue是简单构造的) 而不是调用std::_Construct每个元素。由于A是微不足道的并且可复制分配,因此实际上最终会调用此专业化。当然,这个std::allocator_traits<allocator_type>::construct也没有用。
| 归档时间: |
|
| 查看次数: |
301 次 |
| 最近记录: |