vendredi 2 mars 2018

C++ Name lookup

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!

Aucun commentaire:

Enregistrer un commentaire