曾经在旧版gcc中使用的C ++模板在clang ++中导致“阴影模板参数”错误 [英] C++ template that used to work in old gcc results in 'shadows template parameter' error in clang++

查看:292
本文介绍了曾经在旧版gcc中使用的C ++模板在clang ++中导致“阴影模板参数”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote this code circa '08 using gcc 3.x. I'm now trying to compile with clang 3.4 and I'm getting a template error that I don't understand. The idea is to declare fixed-dimension vec types of arbitrary dimension and precision, and then define vecPair types based on those. I do not understand how the usage of the template typename S inside of "a.convert()" is shadowing the template parameter; it is meant to use the parameter, not redeclare it. Any information would be greatly appreciated!

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 3.4 gives the following output:

$ 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>
                          ^
...


推荐答案

这似乎可行:

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

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

I think that a and b have dependent types, and so you need to disambiguate that convert is a template. I'm not sure why GCC doesn't mind.

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

这篇关于曾经在旧版gcc中使用的C ++模板在clang ++中导致“阴影模板参数”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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