部分专注于错误类型的非类型模板参数 [英] Partially specializing on non-type template parameter of the wrong type

查看:129
本文介绍了部分专注于错误类型的非类型模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

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不会.

clang compiles the code, gcc does not.

但是,在以下高度相关的示例中:

However, in the following highly related example:

template <unsigned >
struct uint_ { };

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

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

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

both compilers reject with no matching function call to foo. gcc's behavior is consistent, clang's is not - so one or the other compiler has a bug for one or both examples. Which compiler is correct?

推荐答案

GCC是正确的. [temp.deduct.type]/17 :

GCC is correct. [temp.deduct.type]/17:

如果P具有包含<i>的形式,并且 A的对应值不同于i的类型,推导失败.

If P has a form that contains <i>, and if the type of the corresponding value of A differs from the type of i, deduction fails.

这篇关于部分专注于错误类型的非类型模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆