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

不完整类型作模板参数:GCC编译通过,Clang/MSVC报错,谁合规?

不完整类型作为模板参数的编译器行为分歧问题

我最近了解到不完整类型(例如void、仅声明未定义的struct incomplete;)在特定条件下可作为模板参数使用。我编写了如下代码,它能通过GCC编译,但无法通过Clang和MSVC编译:

struct incomplete;
template<typename T> struct C
{
    static constexpr T t{};
};

template<class T>
struct myClass {
    C<T> new_t() { return {}; }
};

int main() {
    myClass<incomplete> d;
    d.new_t();    
}

上述代码在C17和C20标准下均可被GCC接受,但Clang和MSVC均报错。我想明确哪种编译器的行为符合C++标准的技术规范。

Clang的错误信息:

<source>:4:24: error: constexpr variable cannot have non-literal type 'const incomplete'
    static constexpr T t{};

MSVC的错误信息:

<source>(4): error C2027: use of undefined type 'incomplete'
<source>(1): note: see declaration of 'incomplete'

请问哪个编译器的行为是正确的?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 13:05:18