namespace confusion...

I am trying to be very careful about using namespaces, but it seems like my compiler is aware of names even though I have not provided a namespace. How does this happen? Today I successfully managed to write a program that prints the current time to the console. I am frustrated to see that it compiles without any issues. :) It's a good problem to have I know, but I need to understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <chrono>
//#include <ctime>

int main()
{
    std::chrono::system_clock::time_point hammerTime = std::chrono::system_clock::now();
    time_t tt = std::chrono::system_clock::to_time_t(hammerTime);
    struct tm *timeInfo = localtime(&tt);
    std::cout << timeInfo->tm_hour << ":"
              << timeInfo->tm_min << ":"
              << timeInfo->tm_sec << std::endl;
    return 0;
}


As far as I know 'time_t', 'tm', and 'localtime' are all defined in the ctime header, which I have not included. I also did not prefix those names with a namespace. So how does the compiler recognise them? They are surely not registered keywords of C++.
MSVC 12.0 has <chrono>. It starts:
1
2
3
4
5
6
7
8
9
// chrono C++11 header
#pragma once
#ifndef _CHRONO_
#define _CHRONO_
#ifndef RC_INVOKED
#include <limits>
#include <ratio>
#include <utility>
#include <time.h> 

There you see the C-style header that adds stuff to the global namespace.

In other words library headers may include other library headers. Some implementations do so more than others.
It partially circumvents the whole idea with namespaces. But I guess it's one of those things I just have to accept. Names from the original C library all over the place. *sigh* What a mess.

Anyway, thank you both for clearing this up.
Topic archived. No new replies allowed.