3

正如以下代码片段中的注释所说,这是 gcc 4.4 的解决方法。错误,我现在可能应该删除它。有关这方面的背景,请参阅带有 gcc 4.4 的模板模板参数和可变参数模板。

在任何情况下,这都会在带有 clang 3.4.2-4 的 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){}

错误是

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.

我在 SO 上看到至少几个表面上相似的问题,即 Clang VS VC++:"error: declaration of 'T' shadows template parameter"C++ template that used to work in old gcc results in 'shadows template parameter' error in clang++ 但对我来说,它们是不同的问题还是相同的问题并不明显。

澄清赞赏。我不经常写C++,也有一段时间没看模板模板参数了。

4

1 回答 1

5

模板模板参数中的名称S, T,ArgsC

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

是多余的并且与S, T, Argsfrom同名maptype。名称相同的事实会在 clang 上产生阴影错误。

所以你可以写

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

或给出不同的名称(出于文档目的,因为它们不能使用)

template <typename S,
          typename T,
          template <typename S_Type, typename T_Type, typename... Args_Type> class C,
          typename... Args>
struct maptype;
于 2014-07-02T11:37:19.600 回答