当我们有模板模板参数时,为什么需要 allocator

时间:2023-04-23
本文介绍了当我们有模板模板参数时,为什么需要 allocator::rebind ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

每个分配器类都必须有一个类似于以下的接口:

template类分配器{...模板<类其他>struct rebind { typedef allocator<Other>其他;};};

使用分配器的类做了一些像这样多余的事情:

template>类向量 { ... };

但为什么需要这样做?

换句话说,他们不能说:

template类分配器 { ... };模板<类T,模板<类>类 Alloc = std::allocator>类向量 { ... };

哪个更优雅、更少冗余,并且(在某些类似情况下)可能更安全?
为什么他们走 rebind 路线,这也会导致更多的冗余(即你必须说 T 两次)?

(类似的问题是 char_traits 和其余的...虽然它们并不都具有 rebind,但它们仍然可以从模板模板参数中受益.)


<块引用>

但是如果您需要 1 个以上的模板参数,这将不起作用!

实际上,效果很好!

template结构池{模板结构分配器{T池[池大小];...};};

现在如果 vector 仅以这种方式定义:

template类分配>类向量 { ... };

那么你可以说:

typedef vector::allocator>int_vector;

它会工作得很好,不需要你(重复地)说两次 int.

vector 中的 rebind 操作将变成 Alloc 而不是 Alloc::template rebind::其他.

解决方案

引用自 C++11 中的算法基础,第 1 卷,第 4 章,p.35 :

template 结构分配器{模板使用 rebind = allocator;};

示例用法:

allocator::rebindX;

<小时>

C++ 编程语言,第 4 版,第 34.4.1 节,p.998,评论默认分配器类中的经典"重新绑定成员:

templatestruct rebind { using other = allocator;};

Bjarne Stroustrup 写道:

<块引用>

奇怪的重新绑定模板是一个古老的别名.应该是:

template使用 other = allocator;

但是,分配器是在 C++ 支持此类别名之前定义的.

Every allocator class must have an interface similar to the following:

template<class T>
class allocator
{
    ...
    template<class Other>
    struct rebind { typedef allocator<Other> other; };
};

And classes that use allocators do something redundant like this:

template<class T, class Alloc = std::allocator<T> >
class vector { ... };

But why is this necessary?

In other words, couldn't they have just said:

template<class T>
class allocator { ... };

template<class T, template<class> class Alloc = std::allocator>
class vector { ... };

which is both more elegant, less redundant, and (in some similar situations) potentially safer?
Why did they go the rebind route, which also causes more redundancy (i.e. you have to say T twice)?

(Similar question goes to char_traits and the rest... although they don't all have rebind, they could still benefit from template template parameters.)


Edit:

But this won't work if you need more than 1 template parameter!

Actually, it works very well!

template<unsigned int PoolSize>
struct pool
{
    template<class T>
    struct allocator
    {
        T pool[PoolSize];

        ...
    };
};

Now if vector was only defined this way:

template<class T, template<class> class Alloc>
class vector { ... };

Then you could just say:

typedef vector<int, pool<1>::allocator> int_vector;

And it would work perfectly well, without needing you to (redundantly) say int twice.

And a rebind operation inside vector would just become Alloc<Other> instead of Alloc::template rebind<Other>::other.

解决方案

A quoted text from Foundations of Algorithms in C++11, Volume 1, chap 4, p. 35 :

template <typename T> 
struct allocator 
{  
   template <typename U>  
   using  rebind = allocator<U>; 
}; 

sample usage :

allocator<int>::rebind<char> x;


In The C++ Programming Language, 4th edition, section 34.4.1, p. 998, commenting the 'classical' rebind member in default allocator class :

template<typename U>
     struct rebind { using other = allocator<U>;};

Bjarne Stroustrup writes this:

The curious rebind template is an archaic alias. It should have been:

template<typename U>
using other = allocator<U>;

However, allocator was defined before such aliases were supported by C++.

这篇关于当我们有模板模板参数时,为什么需要 allocator::rebind ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!