title: c++ using time() with both <time.h> and <ctime> included - which one takes precedence?
body: <p>I'm using a GNU/Linux distribution with Zsh 5.0.2, Vim 7.3, and GCC 4.8.0 to learn C++.</p>
  
  <p>The following code will not compile due to redefinition of the function <code>foo</code>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int foo()
  {
      return 0;
  }
  
  int foo()
  {
      return 0;
  }
  
  int main()
  {
      foo();
      return 0;
  }
  </code></pre>
  
  <p>Attempting to compile it:</p>
  
  <pre><code>» 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()
       ^
  </code></pre>
  
  <p>However, I noticed that GCC (<code>g++</code>, to be specific) automatically includes <code>&lt;time.h&gt;</code> without me explicitly instructing it to do so. In a program I wrote where <code>std::time()</code> is used but in which I forgot to <code>#include &lt;ctime&gt;</code> and use the <code>std::</code> prefix this led to <code>time()</code> from <code>&lt;time.h&gt;</code> to be used instead of the corresponding function from <code>&lt;ctime&gt;</code>. 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:</p>
  
  <p><a href="http://en.cppreference.com/w/c/chrono/time" rel="nofollow" title="cppreference.com: time.h time&#40;&#41;">cppreference.com: time.h time()</a></p>
  
  <p><a href="http://en.cppreference.com/w/cpp/chrono/c/time" rel="nofollow" title="cppreference.com: ctime time&#40;&#41;">cppreference.com: ctime time()</a></p>
  
  <p>Consider the following code:</p>
  
  <pre><code>#include &lt;ctime&gt;
  #include &lt;time.h&gt;
  #include &lt;iostream&gt;
  
  int function1()
  {
      using namespace std;
      cout &lt;&lt; "function4: " &lt;&lt; time(NULL) &lt;&lt; endl;
      return 0;
  }
  
  int function2()
  {
      using std::time;
      std::cout &lt;&lt; "function3: " &lt;&lt; time(NULL) &lt;&lt; std::endl;
      return 0;
  }
  
  int function3()
  {
      std::cout &lt;&lt; "function2: " &lt;&lt; std::time(NULL) &lt;&lt; std::endl;
      return 0;
  }
  
  int function4()
  {
      std::cout &lt;&lt; "function1: " &lt;&lt; time(NULL) &lt;&lt; std::endl;
      return 0;
  }
  
  int main()
  {
      function1();
      function2();
      function3();
      function4();
      return 0;
  }
  </code></pre>
  
  <p>It may not be perfect but I hope my point gets across. Here, I explicitly included <code>&lt;time.h&gt;</code> for the sake of clarity. The first three do in the ways I'm aware of declare that the <code>std</code> is to be used. The fourth function simply calls <code>time()</code> - which to me seems like either one of the <code>&lt;time.h&gt;</code> and <code>&lt;ctime&gt;</code> variants could be called.</p>
  
  <p><strong>Question 1:</strong> Why doesn't this (function4) result in an error or a warning due to ambiguity?</p>
  
  <p><strong>Question 2:</strong> Which variant is used and what determines that one takes precedence over the other?</p>
  
  <p><strong>Question 3:</strong> 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?</p>