相关疑难解决方法(0)

自定义指针类型和容器/分配器typedef

C++标准容器和分配器为容器使用的指针类型提供typedef,即:

typename std::vector<T>::pointer
typename std::vector<T>::const_pointer
Run Code Online (Sandbox Code Playgroud)

用于创建typedef的实际指针类型通过确定 std::allocator_traits

typedef typename std::allocator_traits<Allocator>::pointer pointer;
Run Code Online (Sandbox Code Playgroud)

由于每个容器也有一个value_type类型定义,想必目的pointer的typedef是一些奇怪的情况下使用的指针类型的东西其他比value_type*.我从来没有亲自看到类似的用例,但我认为标准委员会希望提供使用容器的自定义指针类型的可能性.

问题是,这似乎与为其中的函数提供的定义不一致std::allocator_traits.具体来说,std::allocator_traits我们有construct函数,定义为:

template <class T, class... Args>
static void construct(Alloc& a, T* p, Args&&... args);
Run Code Online (Sandbox Code Playgroud)

...只是打电话 a.construct(p, std::forward<Args>(args)...)

但请注意,此函数不对自定义指针类型进行任何规定.该参数p是一个普通的本机指针.

那么,为什么这个函数的定义不是这样的:

template <class... Args>
static void construct(Alloc& a, typename Alloc::pointer p, Args&&... args);
Run Code Online (Sandbox Code Playgroud)

似乎没有这个,std::allocator_traits<Alloc>::construct如果与定义了一些自定义指针类型的Allocator一起使用,那么使用的容器将会失败.

那么,这里发生了什么?或者我是否误解pointer了首先使用typedef 的目的?

c++ c++11

9
推荐指数
1
解决办法
833
查看次数

标签 统计

c++ ×1

c++11 ×1