为什么iostream包含time.h?

Rug*_*rra 5 c++ namespaces compiler-errors std

考虑以下代码:

#include <iostream>

template<class C>
struct time { };

int main() { }
Run Code Online (Sandbox Code Playgroud)

它产生(GCC 4.5):

error: ‘template<class C> struct time’ redeclared as different kind of symbol
/usr/include/time.h:186:15: error: previous declaration of ‘time_t time(time_t*)’
Run Code Online (Sandbox Code Playgroud)
  1. 为什么iostream包含time_t time(time_t*)
  2. 为什么iostream包含time_t time(time_t*)外部std命名空间?
  3. (未答复)为什么,如果我删除template<class C>,我不会收到此错误?

Bas*_*tch 6

如果你跑g++ -H -Wall -c testim.cc(testim.cc你的例子在哪里),你会看到

.... /usr/include/c++/4.6/bits/ios_base.h
..... /usr/include/c++/4.6/ext/atomicity.h
...... /usr/include/c++/4.6/x86_64-linux-gnu/./bits/gthr.h
....... /usr/include/c++/4.6/x86_64-linux-gnu/./bits/gthr-default.h
........ /usr/include/pthread.h
......... /usr/include/sched.h
.......... /usr/lib/gcc/x86_64-linux-gnu/4.6/include/stddef.h
.......... /usr/include/time.h
Run Code Online (Sandbox Code Playgroud)

因此<time.h>包含在pthread支持中.

这是在Debian/Sid/AMD64上的GCC 4.6