简体   繁体   中英

C++ names lookup for functions from <time.h>

I am using C++11 and I met a problem that I cannot figure it out for several days.

Basically I have a header file like this:

#include <time.h>
#include <sys/time.h>

namespace MyNamespace {

static double get_wall_time(){
        struct timeval time;
        if (gettimeofday(&time,NULL)){
                return 0;
        }
        return (double)time.tv_sec + (double)time.tv_usec * .000001;
}

static double get_cpu_time(){
        return (double)clock() / CLOCKS_PER_SEC;
}

}

My stupid question is that why the functions defined in my own namespace(get_cpu_time, get_wall_time) are able to use functions exist in the std namespace (gettimeofday and clock) WITHOUT the "std:: "qualifier. I have use this header file for a while and it works fine. I think it has something to do with the name lookup mechanism but I just cannot find the exact rule

Thank you in advance for any reply!

No problem.

Because they are from C headers time.h and sys/time.h , those names are in global namespace, not in std:: .

The headers you include are C language legacy. As such, functions declared there are the global namespace not in std.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM