[英]How can we use a variable of the type of the template parameter?
在下面的代碼中,我同時傳遞了類類型R
和該類型的常量表達式作為模板參數。 但是clang不接受:
#include <iostream>
template<class T, T t>
void foo(){ std::cout << "foo()" << std::endl; }
class R
{
public:
int f;
constexpr R(): f(15){ }
};
constexpr R r;
int main(){ foo<R, r>(); } //note: candidate template ignored:
//invalid explicitly-specified argument
//for template parameter 't'
在N4296::14.3.2 [temp.arg.nontype]
除了非類型模板參數應為常量表達式外,我找不到任何限制。
§14.1[temp.param] / p4:
非類型模板參數應具有以下(可選,具有cv限定)類型之一:
- 整數或枚舉類型,
- 指向對象或函數的指針,
- 對對象的左值引用或對函數的左值引用,
- 指向成員的指針,
std::nullptr_t
。
類類型不是這些。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.