问题描述
我正在重新阅读“ C ++ Primer,第5版”。在关于模板的第16章中,有一个“模板非类型参数”示例:
template<unsigned N,unsigned M>
int compare(const char (&p1)[N],const char (&p2)[M])
{
return strcmp(p1,p2);
}
int main()
{
cout << compare("hi","mom") << endl;
cout << strcmp("hi","mom") << endl;
std::cout << "\ndone!\n";
}
-
我们知道,
strcmp()
比较两个字符串并返回0
相等,如果str1
大于str2
则返回正值,而返回负数如果str1
小于str2
的值,这就是我在main()
内调用strcmp()
的结果。 -
问题出在书籍示例中,该示例在模板函数内调用
strcmp()
,所以当我运行程序时,我得到:
输出:
-5
-1
代码中的问题是什么?为什么两个参数都为相同的参数赋予不同的值?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)