[英]What is wrong with this template partial specialization code?
我正在使用以下代碼:
template<typename t>
struct foo
{
foo(t var)
{
std::cout << "Regular Template";
}
};
template<typename t>
struct foo<t*>
{
foo(t var)
{
std::cout << "partially specialized Template " << t;
}
};
int main()
{
int* a = new int(12);
foo<int*> f(a); //Since its a ptr type it should call specialized template
}
但是我得到了錯誤
Error 1 error C2664: 'foo<t>::foo(int)' : cannot convert parameter 1 from 'int *' to 'int'
這兩個模板的構造函數都按值乘t
,在您的示例中為int
,而不是int*
。 進行編譯使用
template<typename t>
struct foo<t*>
{
foo(t* var)
{
std::cout << "partially specialized Template " << var;
}
};
如果這符合您的邏輯,則將int
傳遞給構造函數。 (生活)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.