部分专注于错误类型的非类型模板参数
Partially specializing on non-type template parameter of the wrong type
考虑以下几点:
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 的行为是不一致的——所以一个或另一个编译器对一个或两个示例都有错误。哪个编译器是正确的?
海湾合作委员会是正确的。 [temp.deduct.type]/17:
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.
考虑以下几点:
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 的行为是不一致的——所以一个或另一个编译器对一个或两个示例都有错误。哪个编译器是正确的?
海湾合作委员会是正确的。 [temp.deduct.type]/17:
If
P
has a form that contains<i>
, and if the type of the corresponding value ofA
differs from the type ofi
, deduction fails.