为什么允许嵌套类模板的部分特殊化,而完成不是? [英] Why is partial specialziation of a nested class template allowed, while complete isn't?

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

问题描述

    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天全站免登陆