我正在使用带有 Zsh 5.0.2、Vim 7.3 和 GCC 4.8.0 的 GNU/Linux 发行版来学习 C++。
由于重新定义了函数,以下代码将无法编译foo
:
#include <iostream>
int foo()
{
return 0;
}
int foo()
{
return 0;
}
int main()
{
foo();
return 0;
}
尝试编译它:
» g++ -g -pedantic -std=c++11 -Wall -Wextra -Weffc++ foo.cpp -o foo
fail.cpp: In function ‘int foo()’:
fail.cpp:8:5: error: redefinition of ‘int foo()’
int foo()
^
fail.cpp:3:5: error: ‘int foo()’ previously defined here
int foo()
^
但是,我注意到 GCC(g++
具体来说是 )会自动包含<time.h>
,而无需我明确指示它这样做。在一个程序中,我写了 where std::time()
is used 但我忘记了#include <ctime>
并使用std::
前缀 this 导致time()
from<time.h>
被使用,而不是相应的函数 from <ctime>
。我很好奇 - 当两者都包含时将使用哪个 time() ?据我所知,两者都具有该名称的功能(并且它们都以相似或相同的方式工作),请参阅:
cppreference.com: time.h time()
cppreference.com: ctime time()
考虑以下代码:
#include <ctime>
#include <time.h>
#include <iostream>
int function1()
{
using namespace std;
cout << "function4: " << time(NULL) << endl;
return 0;
}
int function2()
{
using std::time;
std::cout << "function3: " << time(NULL) << std::endl;
return 0;
}
int function3()
{
std::cout << "function2: " << std::time(NULL) << std::endl;
return 0;
}
int function4()
{
std::cout << "function1: " << time(NULL) << std::endl;
return 0;
}
int main()
{
function1();
function2();
function3();
function4();
return 0;
}
它可能并不完美,但我希望我的观点得到传达。<time.h>
在这里,为了清楚起见,我明确包括在内。前三个以我知道的方式声明std
要使用。第四个函数只是调用time()
- 在我看来,这似乎可以调用<time.h>
和<ctime>
变体中的任何一个。
问题1:为什么this(function4)不会因为歧义而导致错误或警告?
问题 2:使用哪种变体,是什么决定了其中一个优先于另一个?
问题 3:有没有办法,比如说,在运行或编译过程中输出完整的函数名称,以查看使用了什么库?