不匹配的 `int` 和 `size_t` 不编译的部分模板特化
Partial template specialization with mismatching `int` and `size_t` not compiling
参考以下代码
#include <utility>
#include <cassert>
template <typename T>
struct Wot;
template <int... ints>
struct Wot<std::index_sequence<ints...>> {};
int main() {
assert(sizeof(Wot<std::index_sequence<1, 2, 3>>) == 1);
}
这适用于 clang 但不适用于 gcc,当我更改部分专业化的类型以在索引序列中接受 std::size_t
但它有效时。
谁是对的? Clang 还是 gcc?
gcc 是对的。这正是 [temp.deduct.type]/18:
If P
has a form that contains <i>
, and if the type of i
differs from the type of the corresponding template parameter of the template named by the enclosing simple-template-id, deduction fails. If P
has a form that contains [i]
, and if the type of i
is not an integral type, deduction fails. [ Example:
template<int i> class A { /* ... */ };
template<short s> void f(A<s>);
void k1() {
A<1> a;
f(a); // error: deduction fails for conversion from int to short
f<1>(a); // OK
}
template<const short cs> class B { };
template<short s> void g(B<s>);
void k2() {
B<1> b;
g(b); // OK: cv-qualifiers are ignored on template parameter types
}
— end example ]
镜像示例并简化原始问题:
template <class T> struct Wot { };
template <int... ints>
void foo(Wot<std::index_sequence<ints...>> ) { }
int main() {
foo(Wot<std::index_sequence<1, 2, 3>>{}); // error
foo<1, 2, 3>({}); // ok
}
我认为这是 clang 错误 16279
参考以下代码
#include <utility>
#include <cassert>
template <typename T>
struct Wot;
template <int... ints>
struct Wot<std::index_sequence<ints...>> {};
int main() {
assert(sizeof(Wot<std::index_sequence<1, 2, 3>>) == 1);
}
这适用于 clang 但不适用于 gcc,当我更改部分专业化的类型以在索引序列中接受 std::size_t
但它有效时。
谁是对的? Clang 还是 gcc?
gcc 是对的。这正是 [temp.deduct.type]/18:
If
P
has a form that contains<i>
, and if the type ofi
differs from the type of the corresponding template parameter of the template named by the enclosing simple-template-id, deduction fails. IfP
has a form that contains[i]
, and if the type ofi
is not an integral type, deduction fails. [ Example:template<int i> class A { /* ... */ }; template<short s> void f(A<s>); void k1() { A<1> a; f(a); // error: deduction fails for conversion from int to short f<1>(a); // OK } template<const short cs> class B { }; template<short s> void g(B<s>); void k2() { B<1> b; g(b); // OK: cv-qualifiers are ignored on template parameter types }
— end example ]
镜像示例并简化原始问题:
template <class T> struct Wot { };
template <int... ints>
void foo(Wot<std::index_sequence<ints...>> ) { }
int main() {
foo(Wot<std::index_sequence<1, 2, 3>>{}); // error
foo<1, 2, 3>({}); // ok
}
我认为这是 clang 错误 16279