3

As the comment in the following code snippet says, this is a workaround for a gcc 4.4. bug, which I probably should remove now. See Template template parameters and variadic templates with gcc 4.4 for the background to this.

In any case, this gives an error on Debian Wheezy with clang 3.4.2-4, backported from unstable. This works fine with gcc 4.9 also backported from unstable (and 4.7) on Debian Wheezy.

// Workaround for gcc 4.4 bug. See https://stackoverflow.com/q/8514633/350713
template <typename S, typename T,
      template <typename S, typename T, typename... Args> class C,
      typename... Args>
struct maptype
{
  typedef C<S, T, Args...> type;
};

int main(void){}

The error is

clang++ -o shadow.ocl -c -ftemplate-depth-100 -fno-strict-aliasing -fno-common -ansi -Wextra -Wall -Werror -Wno-unused-function -Wc++0x-compat -Wpointer-arith -Wcast-qual -Wcast-align -std=c++11 -mtune=native -msse3 -O3 shadow.cc
shadow.cc:3:23: error: declaration of 'S' shadows template parameter
          template <typename S, typename T, typename... Args> class C,
                             ^
shadow.cc:2:20: note: template parameter is declared here
template <typename S, typename T,
                   ^
shadow.cc:3:35: error: declaration of 'T' shadows template parameter
          template <typename S, typename T, typename... Args> class C,
                                         ^
shadow.cc:2:32: note: template parameter is declared here
template <typename S, typename T,
                               ^
2 errors generated.

I see at least a couple of superficially similar questions on SO, i.e. Clang VS VC++:"error: declaration of 'T' shadows template parameter" and C++ template that used to work in old gcc results in 'shadows template parameter' error in clang++ but is is not obvious to me whether they are a different problem or the same problem.

Clarifications appreciated. I don't write C++ regularly, and it has been a while since I looked at template template parameters.

Community
  • 1
  • 1
Faheem Mitha
  • 6,096
  • 7
  • 48
  • 83

1 Answers1

5

The names S, T, Args in the template template argument C

template <typename S, typename T, typename... Args> class C

are superfluous AND have the same names as the S, T, Args from maptype. The fact that the names are identical produces the shadow error on clang.

So you may write

template <typename S,
          typename T,
          template <typename, typename, typename...> class C,
          typename... Args>
struct maptype;

or give different names (for documentation purposes as they can't be used)

template <typename S,
          typename T,
          template <typename S_Type, typename T_Type, typename... Args_Type> class C,
          typename... Args>
struct maptype;
Faheem Mitha
  • 6,096
  • 7
  • 48
  • 83
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 2
    Hi @Jarod42, thanks for the helpful reply. I have some followup questions. (1) Can you refer me to the standard, or something similar, preferably online, that addresses this issue? (2) If clang is correct to complain, why doesn't g++? Is this a g++ bug? – Faheem Mitha Jul 02 '14 at 16:17
  • 1
    Oh, and if you can't refer me to something online, could you quote it in the answer? – Faheem Mitha Jul 02 '14 at 17:09
  • 1
    Sorry, I can't quote standard, I suggest you to ask similar question (You may create a shorter example just with `template class C> class Test;`) with *language-lawyer* tag – Jarod42 Jul 02 '14 at 19:01