C++中為什么不要過度參數(shù)化

這篇文章主要介紹“C++中為什么不要過度參數(shù)化”,在日常操作中,相信很多人在C++中為什么不要過度參數(shù)化問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++中為什么不要過度參數(shù)化”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供沈陽網(wǎng)站建設(shè)、沈陽做網(wǎng)站、沈陽網(wǎng)站設(shè)計(jì)、沈陽網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、沈陽企業(yè)網(wǎng)站模板建站服務(wù),10年沈陽做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

T.61:不要過度參數(shù)化成員(SCARY)

Reason(原因)

A member that does not depend on a template parameter cannot be used except for a specific template argument. This limits use and typically increases code size.

不依賴于模板參數(shù)的成員無法使用,特定的模板參數(shù)除外。這會(huì)限制使用并通常會(huì)增加代碼大小。

Example, bad(反面示例)

template<typename T, typename A = std::allocator{}>
   // requires Regular<T> && Allocator<A>
class List {
public:
   struct Link {   // does not depend on A
       T elem;
       T* pre;
       T* suc;
   };

   using iterator = Link*;

   iterator first() const { return head; }

   // ...
private:
   Link* head;
};

List<int> lst1;
List<int, My_allocator> lst2;

This looks innocent enough, but now Link formally depends on the allocator (even though it doesn't use the allocator). This forces redundant instantiations that can be surprisingly costly in some real-world scenarios. Typically, the solution is to make what would have been a nested class non-local, with its own minimal set of template parameters.

代碼看起來足夠正確,但是現(xiàn)在Link形式上依賴于分配器(即使它沒有使用分配器)。這會(huì)引發(fā)多余的例示,而這種例示在某些現(xiàn)實(shí)流程中可能會(huì)帶來令人驚訝的高成本。通常的解決方案是讓本來的嵌套類別弄成非局部的,同時(shí)它的成員只擁有最少的模板參數(shù)。

template<typename T>
struct Link {
   T elem;
   T* pre;
   T* suc;
};

template<typename T, typename A = std::allocator{}>
   // requires Regular<T> && Allocator<A>
class List2 {
public:
   using iterator = Link<T>*;

   iterator first() const { return head; }

   // ...
private:
   Link* head;
};

List<int> lst1;
List<int, My_allocator> lst2;

Some people found the idea that the Link no longer was hidden inside the list scary, so we named the technique SCARY. From that academic paper: "The acronym SCARY describes assignments and initializations that are Seemingly erroneous (appearing Constrained by conflicting generic parameters), but Actually work with the Right implementation (unconstrained bY the conflict due to minimized dependencies)."

有些人會(huì)發(fā)現(xiàn)Link不再被list隱藏,因此我們稱這種技術(shù)為SCARY。根據(jù)大學(xué)論文:“SCARY這個(gè)縮寫描述了一些看起來錯(cuò)誤(看起來被沖突的參數(shù)約束),但實(shí)際上可以和正確的實(shí)現(xiàn)一起工作(由于最小化的依賴關(guān)系而不會(huì)被沖突所限制)的賦值和初始化。”

Enforcement(實(shí)施建議)

  • Flag member types that do not depend on every template argument

  • 標(biāo)記不依賴于任何模板參數(shù)的成員

  • Flag member functions that do not depend on every template argument

  • 標(biāo)記不依賴于任何模板參數(shù)的成員的成員函數(shù)。

到此,關(guān)于“C++中為什么不要過度參數(shù)化”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

網(wǎng)站標(biāo)題:C++中為什么不要過度參數(shù)化
當(dāng)前路徑:http://bm7419.com/article30/iihiso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、微信小程序、靜態(tài)網(wǎng)站、品牌網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司