久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

當我們有模板模板參數時,為什么需要 allocator

Why is allocator::rebind necessary when we have template template parameters?(當我們有模板模板參數時,為什么需要 allocator::rebind ?)
本文介紹了當我們有模板模板參數時,為什么需要 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 中文字幕在线第一页 | 日韩在线视频一区 | 久久精品视频在线观看 | 欧美激情精品久久久久久 | aaa在线| 欧美福利三区 | 国产欧美日韩一区二区三区 | 精品久久国产 | 啪视频在线 | 成人三级av | 国产99久久精品一区二区300 | 一级少妇女片 | 国产区在线观看 | 久久最新 | 精品视频导航 | 在线一区 | 91精品久久久久久久 | 欧美老少妇一级特黄一片 | 中文字幕在线观看日韩 | 永久www成人看片 | 欧美亚洲高清 | 亚洲精品视频在线观看免费 | 久久久久一区二区三区四区 | 国产资源在线观看 | 人人人人干 | 九九九视频在线观看 | 激情欧美日韩一区二区 | 四虎在线观看 | 亚洲区视频 | av性色全交蜜桃成熟时 | va精品 | 婷婷成人在线 | 成人在线视频网站 | 99久久精品国产一区二区三区 | 超碰成人av| 国产一级影片 | 91精品国产综合久久久久久漫画 | 99热在线免费 | 国产探花在线精品一区二区 | 99亚洲精品视频 | 国产精品99久久久久久宅男 |