简体   繁体   中英

Template shadow error with clang

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, ie 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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM