不匹配`int`和`size_t`不匹配的部分模板专业化 [英] Partial template specialization with mismatching `int` and `size_t` not compiling

查看:142
本文介绍了不匹配`int`和`size_t`不匹配的部分模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考以下代码

#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时,它却起作用.

This works on clang but does not work on gcc, when I change the type of the partial specialization to accept std::size_t in the index sequence however it works.

谁是对的? lang还是gcc?

Who is right? Clang or gcc?

请在此处 https://wandbox.org/permlink/5YkuimK1pH3aKJT4

推荐答案

gcc是正确的.这正是 [temp.deduct.type]/18 :

gcc is right. This is exactly [temp.deduct.type]/18:

如果P具有包含<i>的形式,并且i的类型不同于由随附的 simple-template-id命名的模板的相应模板参数的类型,推导失败.如果P具有包含[i]的形式,并且i的类型不是整数类型,则推论失败. [示例:

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
}

-举个例子]

镜像示例并简化原始问题:

Mirroring the example and simplifying the original question:

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

I think this is clang bug 16279

这篇关于不匹配`int`和`size_t`不匹配的部分模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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