简体   繁体   English

c的模板阴影错误

[英]Template shadow error with clang

As the comment in the following code snippet says, this is a workaround for a gcc 4.4. 如以下代码片段中的注释所述,这是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. 有关此背景,请参见带有gcc 4.4的模板模板参数和可变参数模板

In any case, this gives an error on Debian Wheezy with clang 3.4.2-4, backported from unstable. 在任何情况下,这都会给从Debian Wheezy传来clang 3.4.2-4的错误,并从不稳定版本中反向移植。 This works fine with gcc 4.9 also backported from unstable (and 4.7) on Debian Wheezy. 这在gcc 4.9上也能正常工作,并且在Debian Wheezy上也从不稳定(和4.7)反向移植。

// 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. 我至少在SO上看到几个表面上类似的问题,即Clang VS VC ++:“错误:'T'阴影模板参数的声明”用于旧gcc的C ++模板导致clang ++中的“阴影模板参数”错误但是对于我来说是不同的问题还是相同的问题对我来说并不明显。

Clarifications appreciated. 感谢澄清。 I don't write C++ regularly, and it has been a while since I looked at template template parameters. 我不定期编写C ++,自从我看过模板模板参数以来已经有一段时间了。

The names S , T , Args in the template template argument C 模板模板参数C的名称STArgs

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

are superfluous AND have the same names as the S , T , Args from maptype . 是多余的,并且与maptypeSTArgs具有相同的名称。 The fact that the names are identical produces the shadow error on clang. 名称相同的事实在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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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