C 巢狀類類模板特化 error explicit specialization?

時間 2021-06-06 20:46:10

1樓:夏之幻想

特化需要在命名空間裡做,所以你不能在類內直接特化乙個類模板,你可以放到類外來做。

PS. VS2015是允許這麼做的,這是微軟私貨,如果你注重可移植性就不要依賴這個特性。

n4618

14.7.3 (2) An explicit specialization shall be declared in a namespace enclosing the specialized template.

An explicit specialization whose declarator-id or class-head-name is not qualified shall be declared in the nearest enclosing namespace of the template, or, if the namespace is inline (7.3.1), any namespace from its enclosing namespace set.

Such a declaration may also be a definition. If the declaration is not a definition, the specialization may be defined later (7.3.

1.2).

2樓:SuperSodaSea

模板(全?)特化應該只能在命名空間內進行(也就是不能在class裡面特化),所以大概只能改成這樣:

namespace

Impl

;template

<>struct

B;}

classA;

似乎這樣也行?(疑似編譯器擴充套件)

classA;

template

X>struct

B

X>;B

>dvalue;B

ivalue;};

C 繼承模板類 引數是子類 替代虛函式。模板類只能訪問子類公共成員,受保護的不行?

Jon Lee 這是CRTP curiously recurring template pattern,https en.m.wikipedia.org wiki Curiously recurring template pattern 通過把建立虛函式表vtable的過程搬到編譯時間來提高效率。對...

C 中的模板類宣告標頭檔案和實現檔案分離後,如何能實現正常編譯?

孫嘉成 把模板的宣告放在.h檔案中,實現放在.cpp檔案中,在main.cpp檔案中 include XX.cpp 同樣可以正常使用模板函式,但是這並不能實現您說的隱藏實現,只是表面上將宣告和實現分離了,沒什麼實際用處,只是看著好看一些,不建議使用。 zaoru 1.包含模型 常規寫法將實現寫在標頭...

C 抽象類作為模板引數,當其為指標時,如何析構?

小明 別總想著只要是物件就要new,多用RAII解決資源問題。首先你的linklist初始化就不要new.你要做的是過載 operator 接著linklist繼承的是linerlist是如何實現,為啥抽象基類要具體化,你只是使用他的指標。多用智慧型指標,如果嫌棄智慧型指標,就學一下智慧型指標的思路...