部分专门针对错误类型的非类型模板参数

2023-11-23

考虑以下:

template <unsigned >
struct uint_ { };

template <class >
struct X {
    static constexpr bool value = false;
};

template <int I> // NB: int, not unsigned
struct X<uint_<I>> {
    static constexpr bool value = true;
};

int main() {
    static_assert(X<uint_<0>>::value, "!");
}

clang 编译代码,gcc 不编译。

然而,在以下高度相关的示例中:

template <unsigned >
struct uint_ { };

template <int I> // NB: int, not unsigned
void foo(uint_<I> ) { }

int main() {
    foo(uint_<0>{} );
}

两个编译器都拒绝,没有匹配的函数调用foo。 gcc 的行为是一致的,而 clang 的行为则不然 - 因此一个或另一个编译器对于一个或两个示例都有错误。哪个编译器是正确的?


海湾合作委员会是正确的。[温度扣除类型]/17:

If P有一个包含以下内容的表格<i>,并且如果的类型 对应的值A与类型不同i,推演失败。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

部分专门针对错误类型的非类型模板参数 的相关文章

随机推荐