简体   繁体   English

具有超过1个typename的模板函数

[英]template function with more than 1 typename

It seems C++ template can automatically deduct the type, when there is only one template parameter. 似乎只有一个模板参数时,C ++模板可以自动推断类型。 For example, 例如,

template<typename T> 

void f(const T &t)
{
  cout << t << endl;
}

f(1);

is fine. 很好

However, 然而,

template<typename T1, typename T2> 

void f(const T1 &t1, const T2 &t2)
{
  cout << t1 << t2 << endl;
}

f(1, 2);

the above code can not pass compilation. 上面的代码无法通过编译。 Is this because of the ability of the compiler or it is against the standard? 是因为编译器的能力还是违反标准?

The second function and call is perfectly valid. 第二个函数和调用是完全有效的。 Your compiler is bugged or flat out old rejecting it. 您的编译器有错误或被旧版拒绝了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM