You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CRTP模式下MSVC模板编译异常:C++17/20编译失败问题

C++17/20下MSVC继承依赖类型别名的编译兼容性问题

请参考以下代码:

template <class Derived>
class KFBase
{
public:
    template <typename Nom>
    struct State {
        Nom nominal_state;
    };
    
};

template <typename A>
class PFilter
    : public KFBase<PFilter<A>>
{
public:
    using Base = KFBase<PFilter<A>>;
    // 注释此行则编译正常
    using State = typename Base::State<int>;
};

int main(int, char**){
    PFilter<int>{};
}

问题现象:

  • 在MSVC中,注释掉PFilter类内的using State = typename Base::State<int>;后,C17或C20标准下均可正常编译;
  • 保留该行别名声明时,MSVC在C17/20标准下会出现编译错误,但切换到C14标准时可正常编译;
  • 该代码在GCC的C++20标准环境中可正常编译。

疑问:

此写法是否符合C标准?这是否是MSVC的C17实现引入的bug?直觉认为这与CTAD有关,但不确定,恳请解惑。

内容的提问来源于stack exchange,提问作者SupAl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 21:33:21