2011-11-04 134 views
5

考虑以下代码:为什么iostream包含time.h?

#include <iostream> 

template<class C> 
struct time { }; 

int main() { } 

它产生(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*)’ 
  1. 为什么iostream包括time_t time(time_t*)
  2. 为什么iostream包含time_t time(time_t*)以外的std命名空间?
  3. (未回复)为什么如果我删除template<class C>,我没有收到此错误吗?

回答

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 

所以<time.h>是包括用于pthread支持。

这是在Debian/SID/AMD64

GCC 4.6
+0

试试4.6.2。更新日志表示对''的依赖性已经降低。 –

+0

在我的机器上g ++是'gcc版本4.6.2(Debian 4.6.2-3)' –

2

似乎是在我的箱子区域支持:

$ cat whytime.cc 
#define _TIME_H_ 
#include <iostream> 

int main() { 
    std::cout << "hello, world" << std::endl; 
    return 0; 
} 

$ g++ -Wall -Werror -Wextra -ansi -o whytime whytime.cc && ./whytime 
In file included from /usr/include/c++/4.2.1/cwchar:52, 
       from /usr/include/c++/4.2.1/bits/postypes.h:46, 
       from /usr/include/c++/4.2.1/iosfwd:49, 
       from /usr/include/c++/4.2.1/ios:43, 
       from /usr/include/c++/4.2.1/ostream:45, 
       from /usr/include/c++/4.2.1/iostream:45, 
       from whytime.cc:2: 
/usr/include/c++/4.2.1/ctime:66: error: ‘::clock_t’ has not been declared 
/usr/include/c++/4.2.1/ctime:68: error: ‘::tm’ has not been declared 
/usr/include/c++/4.2.1/ctime:70: error: ‘::clock’ has not been declared 
/usr/include/c++/4.2.1/ctime:71: error: ‘::difftime’ has not been declared 
/usr/include/c++/4.2.1/ctime:72: error: ‘::mktime’ has not been declared 
/usr/include/c++/4.2.1/ctime:73: error: ‘::time’ has not been declared 
/usr/include/c++/4.2.1/ctime:74: error: ‘::asctime’ has not been declared 
/usr/include/c++/4.2.1/ctime:75: error: ‘::ctime’ has not been declared 
/usr/include/c++/4.2.1/ctime:76: error: ‘::gmtime’ has not been declared 
/usr/include/c++/4.2.1/ctime:77: error: ‘::localtime’ has not been declared 
/usr/include/c++/4.2.1/ctime:78: error: ‘::strftime’ has not been declared 
In file included from /usr/include/c++/4.2.1/locale:46, 
       from /usr/include/c++/4.2.1/bits/ostream.tcc:46, 
       from /usr/include/c++/4.2.1/ostream:572, 
       from /usr/include/c++/4.2.1/iostream:45, 
       from whytime.cc:2: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘_InIter std::time_get<_CharT, _InIter>::_M_extract_via_format(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*, const _CharT*) const’: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1839: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1846: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1854: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1861: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1873: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1880: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1883: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1895: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1900: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1908: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1912: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1932: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1968: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:1976: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘virtual _InIter std::time_get<_CharT, _InIter>::do_get_weekday(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*) const’: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:2210: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘virtual _InIter std::time_get<_CharT, _InIter>::do_get_monthname(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*) const’: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:2259: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
/usr/include/c++/4.2.1/bits/locale_facets.tcc: In member function ‘virtual _InIter std::time_get<_CharT, _InIter>::do_get_year(_InIter, _InIter, std::ios_base&, std::_Ios_Iostate&, tm*) const’: 
/usr/include/c++/4.2.1/bits/locale_facets.tcc:2288: error: invalid use of incomplete type ‘struct tm’ 
/usr/include/wchar.h:152: error: forward declaration of ‘struct tm’ 
+0

不错的诀窍,但我没有得到你的错误 –

+0

通过定义'time.h'的头部守卫,我们强制它的包含什么也不做。据推测,无论什么包括它也会在不久之后使用它,并且由此产生的编译错误将会被红包打包。对我来说,这似乎是'ostream'带入的'/ usr/include/C++/4.2.1/locale'。人们应该记住,这只显示一个包装器,可能有其他包装器。 – phs

1

显然,GCC的iostream需要的东西在time的东西。

time_tC语言的事情,这没有命名空间,所以为了向后可比性十岁上下,它是在两个std命名空间,而不是在std命名空间。

至于为什么没有错误,我不是100%确定。我知道C可能具有相同名称的函数和结构,并且struct被限定为名称struct,以便编译器可以区分它们。 C++编译器认为这是一个函数,除非关键字为structhttp://ideone.com/XZB2M v http://ideone.com/kWMKE。我想它也允许向后可比性?

+0

phs表示它包含用于语言环境支持的'ctime',这是有道理的。 –

+0

好的,但'template'呢? –

+0

我完全不知道那个。对不起:( –