C++ 函数模板调用未解析
在下面的代码段中,对C++ 函数模板调用未解析,c++,templates,C++,Templates,在下面的代码段中,对max(2,3)的第二个示例调用没有调用该函数。并且没有编译时/运行时错误。 有人能解释一下这是怎么回事吗 template<class T1> void max(int a,int b) { cout<<"max() called\n"; } max<int>(2,3); //this calls max(2,3) max(2,3); //surprisingly no error and max() isn't cal
max(2,3)
的第二个示例调用没有调用该函数。并且没有编译时/运行时错误。
有人能解释一下这是怎么回事吗
template<class T1>
void max(int a,int b)
{
cout<<"max() called\n";
}
max<int>(2,3); //this calls max(2,3)
max(2,3); //surprisingly no error and max() isn't called here
模板
最大无效值(整数a、整数b)
{
cout您没有使用该模板。使用该模板时会出现如下情况:
template<class T1>
void max(T1 a,T1 b)
#include<iostream>
// because the std namespace is not included there will be no shadowing
template<class T1>
void max(T1 a, T1 b)
{
std::cout<<"max() called\n";
}
模板
最大空隙率(T1 a,T1 b)
在其他一切都相同的情况下,还有另一个函数在std名称空间中隐藏它,将函数名更改为max1将更正此问题或删除名称空间。我希望这会有所帮助
它看起来像这样:
template<class T1>
void max(T1 a,T1 b)
#include<iostream>
// because the std namespace is not included there will be no shadowing
template<class T1>
void max(T1 a, T1 b)
{
std::cout<<"max() called\n";
}
#包括
//因为没有包含std名称空间,所以不会有阴影
样板
最大空隙率(T1 a,T1 b)
{
std::coutRemove使用名称空间std;
,您将看到原因(或者不将函数命名为std
名称空间中存在的函数)应该有一个错误,因为没有定义cout
。显示真实代码;可以编译的最小代码显示问题。对。这是关于std命名空间的。谢谢