为什么不允许嵌套类模板的部分专业化,而不允许完全专业化呢? [英] Why is partial specialization of a nested class template allowed, while complete isn't?

查看:109
本文介绍了为什么不允许嵌套类模板的部分专业化,而不允许完全专业化呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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 ).

这篇关于为什么不允许嵌套类模板的部分专业化,而不允许完全专业化呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆