我已经告诉别人,编写using namespace std;代码是错误的,我应该用std::cout和std::cin直接代替.
为什么被using namespace std;认为是不好的做法?是低效还是冒着声明模糊变量(与名称std空间中的函数具有相同名称的变量)的风险?它会影响性能吗?
我正在学习C++,而且我遇到了模板的用法.
所以我尝试使用模板实现以下两个函数,如下所示:
template <typename T>
T max(T a, T b){
return (a > b) ? a : b;
}
template <typename T>
T max(T a, T b, T c){
return max( max(a, b), c);
}
Run Code Online (Sandbox Code Playgroud)
好吧,上面的实现在编译过程中会抛出一些错误.
这就是错误的样子:
templateEx.cpp:13:14: error: call to 'max' is ambiguous
return max( max(a, b), c);
^~~
templateEx.cpp:17:22: note: in instantiation of function template specialization
'max<int>' requested here
cout<<"2, 3, 4 : "<<max(2,3,4);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:2654:1: note:
candidate function [with _Tp = int]
max(const _Tp& __a, const _Tp& …Run Code Online (Sandbox Code Playgroud)