簡體   English   中英

c的模板陰影錯誤

[英]Template shadow error with clang

如以下代碼片段中的注釋所述,這是gcc 4.4的一種解決方法。 錯誤,我現在應該刪除。 有關此背景,請參見帶有gcc 4.4的模板模板參數和可變參數模板

在任何情況下,這都會給從Debian Wheezy傳來clang 3.4.2-4的錯誤,並從不穩定版本中反向移植。 這在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 ++:“錯誤:'T'陰影模板參數的聲明”用於舊gcc的C ++模板導致clang ++中的“陰影模板參數”錯誤但是對於我來說是不同的問題還是相同的問題對我來說並不明顯。

感謝澄清。 我不定期編寫C ++,自從我看過模板模板參數以來已經有一段時間了。

模板模板參數C的名稱STArgs

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

是多余的,並且與maptypeSTArgs具有相同的名稱。 名稱相同的事實在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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM