曾经在旧版gcc中使用的C ++模板在clang ++中导致“阴影模板参数”错误

w

我使用gcc 3.x编写了大约08年的代码。我现在正在尝试使用clang 3.4进行编译,但遇到了我不理解的模板错误。这个想法是声明任意维数和精度的固定维数vec类型,然后根据这些定义vecPair类型。我不了解“ a.convert()”内部的模板类型名称S的用法如何遮盖了模板参数;它的意思是使用参数,而不是重新声明它。任何信息将不胜感激!

typedef unsigned int Uns;

template <typename T>
inline const T& min(const T& a, const T& b) {
  return a <= b ? a : b;
}

template <Uns N, typename T>
struct vec {

  T comp[N];

  template <Uns M, typename S>
  inline vec<M, S> convert() const {
    vec<M, S> converted;
    for (Uns i = 0; i < min(M, N); ++i) converted[i] = comp[i];
    for (Uns i = N; i < M; ++i) converted[i] = 0;
    return converted;
  }
};

template <Uns N, typename T>
struct vecPair {

  vec<N, T> a;
  vec<N, T> b;

  inline vecPair(const vec<N, T>& _a, const vec<N, T>& _b) : a(_a), b(_b) {}

  template <Uns M, typename S>
  inline vecPair<M, S> convert() const {
    vec<M, S> ca = a.convert<M, S>();
    vec<M, S> cb = b.convert<M, S>();
    return vecPair<M, S>(ca, cb);
  }
};

clang 3.4提供以下输出:

$ clang++ -fsyntax-only vec-bug.cpp 
vec-bug.cpp:30:33: error: declaration of 'S' shadows template parameter
    vec<M, S> ca = a.convert<M, S>();
                                ^
vec-bug.cpp:28:29: note: template parameter is declared here
  template <Uns M, typename S>
                            ^
vec-bug.cpp:30:34: error: expected ';' at end of declaration
    vec<M, S> ca = a.convert<M, S>();
                                 ^
                                 ;
vec-bug.cpp:31:12: error: template argument for template type parameter must be a type
    vec<M, S> cb = b.convert<M, S>();
           ^
vec-bug.cpp:11:27: note: template parameter is declared here
template <Uns N, typename T>
                          ^
...
克瑞克(Kerrek SB)

这似乎可行:

vec<M, S> ca = a.template convert<M, S>();
vec<M, S> cb = b.template convert<M, S>();

我认为,a并且b具有依赖类型,因此您需要消除歧义,这convert是一个模板。我不确定为什么GCC不介意。

更新:这似乎是一个已知的GCC错误。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

模板模板参数会导致Clang下的编译器错误,但不会导致GCC

模板参数阴影的C ++声明模板参数错误

模板参数类型成员上的运算符<<仅在clang中导致错误

GCC 5 的模板参数阴影

C ++:以模板对象为参数的模板。导致链接错误

C ++ 11:模板参数中的SFINAE,GCC与Clang

c ++模板递归双链表错误gcc(但clang ++接受)

使用GCC而不是clang进行模板友谊错误编译

使用命名空间中的模板调用模板模板方法时出现错误的clang错误

在派生的模板类中使用typedef时,C ++虚拟模板参数导致错误

gcc失败,模板递归,而clang不

由于删除了模板化的左值转换运算符,可能导致clang或gcc错误?

带嵌套的“嵌套”类模板参数推导:GCC与clang

gcc可以编译可变参数模板,而clang不能

clang不使用可变参数推断可变参数模板函数中的模板参数

当模板template-parameter的模板参数是包扩展时,gcc失败,clang成功

使用变量模板的递归计算-gcc vs clang

模板模板和CRTP:编译器错误以及GCC和clang不同意

在Clang中使用enable_if进行模板专业化失败,可在GCC中使用

使用lambdas的可变参数模板:g ++错误,但与clang ++一起运行

对于曾经在gcc5中工作的情况,无法在gcc6的部分专业化中推导模板参数

模板中的Typedef导致阴影模板parm错误

clang vs gcc模板子类在父级中使用前向声明的类

使用clang的显式C ++模板实例化

没有关于gcc但带有Clang的模板函数中未使用参数的警告

使用可变参数模板(gcc,clang)的成员函数指针包装器

gcc 和 clang 是否不能正确推断模板别名类型?

GCC中的离线构造函数模板在Clang中失败

这是 Clang 的 C++20 概念实现中的错误吗?不必要的约束检查导致无限模板递归