C++ template that used to work in old gcc results in 'shadows template parameter' error in clang++

StackOverflow https://stackoverflow.com//questions/22056659

  •  22-12-2019
  •  | 
  •  

Question

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 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>
                          ^
...
Was it helpful?

Solution

This seems to work:

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

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.

Update: This seems to be a known GCC bug.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top