用户提供的std::allocator专业化

8

位于::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();
}

该程序使用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++ (带有Clang的-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

正如您所见,libstdc ++并不总是使用我提供的 construct 的重载函数。如果我删除 construct , destroy 或 max_size 成员,则该程序甚至无法编译,因为libstdc++会抱怨缺少这些成员,尽管它们由 std :: allocator_traits 提供。

该程序是否具有未定义的行为,因此两个标准库都是正确的,还是程序的行为被明确定义,并且需要标准库使用我的专业化?


请注意,在我的专业化中仍然留有 std :: allocator 主模板的某些成员。我是否也需要添加它们?

要精确,我省略了:

<code>using is_always_equal = std::true_type
</code>

由于我的分配器是空的,所以需要使用 std::allocator_traits 提供的功能,但这些功能本应该是 std::allocator 接口的一部分。

我还省略了 pointer, const_pointer, reference, const_reference, rebindaddress 这些内容,它们都由 std::allocator_traits 提供,并在 C++17 中被标记为过时,因为它们本应该属于 std::allocator 接口。

如果您认为必须定义所有这些内容来匹配 std::allocator 接口,请考虑将它们添加到代码中。


我收回之前的说法。我在Visual Studio 2019中尝试了一下,它包含了所有构造函数调用(甚至是复制构造函数调用)。然而,我猜测libstdc++以某种方式实现了它,使得优化器认为可以将它们删除。 - Spencer
1个回答

3
根据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); }

std::__uninitialized_default_n 调用 std::_Construct,后者使用就地构造(placement new)方式。这解释了为什么你在输出中看不到 "Constructing one",而是先看到 "Allocating for 4"。

编辑: 正如楼主在评论中指出,std::__uninitialized_default_n 调用。

__uninitialized_default_n_1<__is_trivial(_ValueType)
                             && __assignable>::
__uninit_default_n(__first, __n)

如果 __is_trivial(_ValueType) && __assignable 为真,则实际上存在一种专业化,这是 here。它使用 std::fill_n(其中 value 是平凡构造的)而不是在每个元素上调用 std::_Construct。由于 A 是平凡的且可复制赋值的,因此它实际上会调用此专业化。当然,这也不使用 std::allocator_traits<allocator_type>::construct


1
在我的特定情况下,据我所知,它实际上并没有使用std::_Construct调用,因为A是平凡可复制的和平凡可复制赋值的,因此选择了__uninitialized_default_n_1的另一个专业化,该专业化调用std::fill_n,然后再次分派到memcpy/memset。我知道这是libstdc++进行的一种优化,但我没有意识到对于非平凡类型也会跳过std::allocator::construct调用。因此,这可能只是libstdc++在识别使用库提供的std::allocator时的疏忽。 - walnut

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接