c ++同时使用time()和< time.h>和< ctime>包含-哪一个优先? [英] c++ using time() with both <time.h> and <ctime> included - which one takes precedence?

查看:207
本文介绍了c ++同时使用time()和< time.h>和< ctime>包含-哪一个优先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Zsh 5.0.2,Vim 7.3和GCC 4.8.0的GNU/Linux发行版来学习C ++.

I'm using a GNU/Linux distribution with Zsh 5.0.2, Vim 7.3, and GCC 4.8.0 to learn C++.

由于重新定义了功能foo,以下代码将无法编译:

The following code will not compile due to redefinition of the function 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>,而没有明确指示我这样做.在一个程序中,我写了使用std::time()的地方,但是忘记了#include <ctime>并使用了std::前缀,这导致使用<time.h>中的time()而不是使用<ctime>中的相应功能.我很好奇-当同时包含和时,将使用哪个time()?据我所知,它们都具有该名称的功能(并且它们都以相似或相同的方式工作),请参见:

However, I noticed that GCC (g++, to be specific) automatically includes <time.h> without me explicitly instructing it to do so. In a program I wrote where std::time() is used but in which I forgot to #include <ctime> and use the std:: prefix this led to time() from <time.h> to be used instead of the corresponding function from <ctime>. I am curious - which time() will be used when both and are included? As far as I can tell, both have a function by that name (and both of them work in a similar or identical fashion), see:

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>变体之一.

It may not be perfect but I hope my point gets across. Here, I explicitly included <time.h> for the sake of clarity. The first three do in the ways I'm aware of declare that the std is to be used. The fourth function simply calls time() - which to me seems like either one of the <time.h> and <ctime> variants could be called.

问题1:为什么由于歧义性,此(功能4)为什么不会导致错误或警告?

Question 1: Why doesn't this (function4) result in an error or a warning due to ambiguity?

问题2:使用了哪种变体,是什么决定了哪个变体优先于另一个变体?

Question 2: Which variant is used and what determines that one takes precedence over the other?

问题3:,是否有一种方法可以在运行或编译过程中完整输出函数名称,以查看使用了什么库?

Question 3: Is there a way to, say, output the function name in its entirety during a run or compilation process to see what library is used?

推荐答案

请注意,不建议在C ++中使用C标头;并且不能保证C ++标头将定义转储到全局名称空间以及std中.任何答案都将完全取决于您的实现.

Note that using the C headers in C++ is deprecated; and that there's no guarantee that the C++ headers dump the definitions into the global namespace as well as std. Any answer will be fairly specific to your implementation.

为什么(功能4)由于歧义性而不会导致错误或警告?

Why doesn't this (function4) result in an error or a warning due to ambiguity?

有两个原因:

  • 您可以随意声明函数.在此实现中,大多数C函数仅在标头中具有在预编译库中具有定义的声明.
  • 标头包含保护程序,因此即使它们定义了函数,也可以安全地多次包含标头.

使用了哪种变体,是什么决定了哪个优先于另一个?

Which variant is used and what determines that one takes precedence over the other?

在全局名称空间中,它将是两个标头中声明的版本;两者都声明相同的功能.通常,尚不确定C ++头文件是否也声明该函数以及std namepsace中的函数.在您的实施中确实如此.

In the global namespace, it will be the version declared in the both headers; both declare the same function. In general, it's unspecified whether the C++ header also declares that function as well as the one in the std namepsace; in your implementation, it does.

是否有办法在运行或编译过程中完整输出函数名称,以查看使用了什么库?

Is there a way to, say, output the function name in its entirety during a run or compilation process to see what library is used?

也许;但是没有意义,因为无论标头声明了哪个函数,它都将是同一个函数.

Maybe; but there's no point since it will be the same function whichever header declares it.

这篇关于c ++同时使用time()和&lt; time.h&gt;和&lt; ctime&gt;包含-哪一个优先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆