問題描述
template<int x> struct A {
template<int y> struct B {};.
template<int y, int unused> struct C {};
};
template<int x> template<>
struct A<x>::B<x> {}; // error: enclosing class templates are not explicitly specialized
template<int x> template<int unused>
struct A<x>::C<x, unused> {}; // ok
那么,如果外部類也不是專門化的,那么為什么不允許對內部嵌套類(或函數)進行顯式專門化?奇怪的是,如果我只部分通過簡單地添加一個虛擬模板參數來專門化內部類,我就可以解決這個問題.讓事情變得更丑陋和更復雜,但它有效.
So why is the explicit specialization of a inner, nested class (or function) not allowed, if the outer class isn't specialized too? Strange enough, I can work around this behaviour if I only partially specialize the inner class with simply adding a dummy template parameter. Makes things uglier and more complex, but it works.
我會將完全特化視為部分特化的子集 - 特別是因為您可以通過添加虛擬參數將每個完全特化表示為部分特化.因此,對部分專業化和完全專業化之間的這種消歧對我來說并沒有什么意義.
I would consider complete specializations as a subset of partial specializations - especially because you can express every complete specialization as a partial with adding a dummy parameter. So this disambiguation between partial and full specialization doesn't really make sense for me.
不幸的是,comp.std.c++ 中沒有人敢回答,所以我懸賞再次將其放在這里.
Unfortunatly nobody at comp.std.c++ dared to answer, so I am putting it up here again with a bounty.
注意:對于一組外部類的內部類的遞歸模板,我需要此功能,并且內部參數的特化確實取決于外部模板參數.
Note: I need this feature for recursive templates of the inner class for a set of the outer class and the specialization of the inner parameter does depend on the outer template parameter.
推薦答案
我猜測為什么會發生這種情況:完整的專業化不再是模板類/函數",它們是真正的"類/方法,并且有真實的(鏈接器可見的)符號.但是對于部分專業模板中的完全專業模板,這將不是真的.可能做出這個決定只是為了簡化編譯器編寫者的生活(并在此過程中讓編碼人員的生活更加艱難:P).
My guess as to why this happens: complete specializations are no longer "template classes/functions", they are are "real" classes/methods, and get to have real (linker-visible) symbols. But for a completely-specialized template inside a partially-specialized one, this would not be true. Probably this decision was taken just to simplify the life of compiler-writers (and make life harder for coders, in the process :P ).
這篇關于為什么允許嵌套類模板的部分特化,而不允許完全特化?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!