Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does iostream include time.h?

Consider this code:

#include <iostream>

template<class C>
struct time { };

int main() { }

It produces (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. Why does iostream include time_t time(time_t*) ?
  2. Why does iostream include time_t time(time_t*) outside std namespace?
  3. (unanswered) Why, if I remove template<class C>, do I not get this error?
like image 515
Ruggero Turra Avatar asked Nov 04 '11 22:11

Ruggero Turra


1 Answers

If you run g++ -H -Wall -c testim.cc (where testim.cc is your example) you'll see that

.... /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

So <time.h> is included for pthread support.

This is with GCC 4.6 on Debian/Sid/AMD64

like image 125
Basile Starynkevitch Avatar answered Sep 18 '22 19:09

Basile Starynkevitch