::std
中的类模板命名空间通常可以由程序专门用于用户定义的类型。我没有发现 std::allocator
的这条规则有任何异常(exception)。 .
那么,我可以专攻 std::allocator
对于我自己的类型?如果允许,我是否需要提供 std::allocator
的所有成员?的主要模板,因为其中许多可以由 std::allocator_traits
提供(因此在 C++17 中被弃用)?
考虑this program
#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();
}
该程序使用 libc++(带有
-std=c++17 -Wall -Wextra -pedantic-errors -O2 -stdlib=libc++
的 Clang)的输出是: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
并且 libstdc++ 的输出(带有
-std=c++17 -Wall -Wextra -pedantic-errors -O2 -stdlib=libstdc++
的 Clang )是:Allocating for 2
Allocating for 4
Constructing one
Constructing one
Allocating for 8
Constructing one
Constructing one
Constructing one
Constructing one
8
如您所见,libstdc++ 并不总是支持
construct
的重载。我提供的,如果我删除 construct
, destroy
或 max_size
成员,那么程序甚至不能用 libstdc++ 编译并提示这些缺失的成员,尽管它们是由 std::allocator_traits
提供的。 .程序是否有未定义的行为,因此两个标准库都是正确的,或者程序的行为是否定义明确并且使用我的特化所需的标准库?
请注意,有一些成员来自
std::allocator
的主要模板,我仍然在我的专业中遗漏了。我还需要添加它们吗?准确地说,我遗漏了
using is_always_equal = std::true_type
由
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:
For the components affected by this subclause that declare an
allocator_type
, objects stored in these components shall be constructed using theallocator_traits<allocator_type>::construct
function
此外,根据 17.6.4.2.1:
The program may add a template specialization for any standard library template to namespace
std
only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
我不认为标准禁止特化
std::allocator
, 因为我通读了 std::allocator
上的所有部分它没有提到任何东西。我还查看了禁止特化的标准是什么样子的,但我没有找到与 std::allocator
类似的内容。 .Allocator
的要求是 here ,并且你的专长满足他们。因此,我只能得出结论,libstdc++实际上违反了标准(也许我在某处犯了错误)。我发现如果一个人只是专攻
std::allocator
,libstdc++ 将通过对构造函数使用placement new 来响应,因为它们具有专门针对这种情况的模板特化,同时将指定的分配器用于其他操作;相关代码为here (这在 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); }
std::__uninitialized_default_n
来电std::_Construct
它使用放置新。这解释了为什么在输出中的“分配 4”之前看不到“构造一个”。编辑:
正如 OP 在评论中指出的那样,
std::__uninitialized_default_n
来电__uninitialized_default_n_1<__is_trivial(_ValueType)
&& __assignable>::
__uninit_default_n(__first, __n)
如果
__is_trivial(_ValueType) && __assignable
实际上有一个特化是 true
,即 here .它使用 std::fill_n
(其中 value
构造简单)而不是调用 std::_Construct
在每个元素上。由于A
是微不足道的并且可以复制分配,它实际上最终会调用这个特化。当然,这里不使用std::allocator_traits<allocator_type>::construct
任何一个。
关于c++ - 用户提供的 std::allocator 特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61151170/