不适用于嵌套类型的C模板特化

不适用于嵌套类型的C模板特化,第1张

概述以下代码编译,但不起作用: template<typename T>struct Nesting{ template<typename U> struct _Nested { }; template<typename U> using Nested = _Nested<U>;};template<typename T>struct F 以下代码编译,但不起作用:

template<typename T>struct nesting{    template<typename U>    struct _nested    {    };    template<typename U>    using nested = _nested<U>;};template<typename T>struct F{    static constexpr bool is_my_nested_class = false;};template<typename T,typename U>struct F<typename nesting<T>::nested<U>>{    static constexpr bool is_my_nested_class = true;};

我创建了这些嵌套和嵌套类型,并尝试在其上使用类型特征模式.它编译(使用MSVC 2014 w / CPP11),但是

F<nesting<int>::nested<long>>::is_my_nested_class

返回false.

标准是禁止还是未定义?它破坏了什么规则?任何解决方法?

非常感谢你!

解决方法 您的嵌套别名可以引用任何类型,特别是在专业化中:

template<typename T>struct nesting{    template<typename U>    struct _nested    {    };    template<typename U>    using nested = _nested<U>;};// ConsIDer this specialisation:template<>struct nesting<int>{    template<typename U>    using nested = float;};

现在,显然F< nesting< int> :: nested< int>> :: is_my_nested_class应该与F< float> :: is_my_nested_class相同,但是,编译器如何在后一种情况下推导出这个?也就是说,如果我写道:

static_assert(F<float>::is_my_nested_class,"not nested");

编译器需要看到F< float>与F< nesting< int> :: nested< int>>相同,即使后者尚未实例化.由于无法合理地预期这样做,因此该案件是不允许的.

总结

以上是内存溢出为你收集整理的不适用于嵌套类型的C模板特化全部内容,希望文章能够帮你解决不适用于嵌套类型的C模板特化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1228863.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存