加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:21:24 所属栏目:百科 来源:网络整理
导读:以下代码编译,但不起作用: templatetypename Tstruct Nesting{ templatetypename U struct _Nested { }; templatetypename U using Nested = _NestedU;};templatetypename Tstruct F{ static constexpr bool is_my_nested_class = false;};templatetypename
以下代码编译,但不起作用:

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>>相同,即使后者尚未实例化.由于无法合理地预期这样做,因此该案件是不允许的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读