int max(一个变量)和 int max(int ,int)之间没有冲突,这是 C++ 中的一个函数

no conflict between int max,a variable and int max(int ,int),a function in c++

本文关键字:int 一个 max C++ 函数 这是 变量 之间 冲突      更新时间:2023-10-16
这是一个

有效的代码,即使全局变量max命名空间std中的函数max(int,int(之间存在冲突。

为什么没有错误?

using namespace std;
int max;
int main()
{
  int c;
  c=max;
  //c=max(5,3);
}

您没有包含 <algorithm> 或任何其他可能声明std::max的标头,因此没有冲突。程序仅声明一个名为 max 的实体。

如果我包含该标头,则由于歧义,我会收到预期的错误:

test.cpp: In function ‘int main()’:
test.cpp:8:5: error: reference to ‘max’ is ambiguous
   c=max;

解决方案是不要将std转储到全局命名空间中,从而使命名空间的整个用途无效。

相关文章: