簡體   English   中英

為什么模板參數包必須放在最后?

[英]Why must the template parameter pack be last?

我在gcc10.2clang11之間面臨以下不一致:

template<typename ... Args, typename T>
static constexpr int fuu = sizeof(T);

clang抱怨模板包必須是模板聲明中的最后一件事:

error: template parameter pack must be the last template parameter
template<typename ... Args, typename T>  

但是gcc很酷。
https://godbolt.org/z/v9KeW6

這“最后一件事”實際上是規則嗎? 在一個函數中,這適用於兩個編譯器。

template<typename... Args, typename T>
int foo(T) {
    return sizeof(T);
}

我看不出第一個片段不應該工作的原因,模板參數是明確可推論的。

變量模板聲明格式錯誤,這是一個 gcc 錯誤,無法診斷它。

temp.param#14

... 如果主類模板、主變量模板或別名模板的模板參數是模板參數包,則它應是最后一個模板參數。 ...

函數模板很好,因為函數模板的規則不同。 如果參數包后面的模板參數可以通過調用點提供的參數推導出來,那么模板就ok了。

暫無
暫無

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

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