auto-conversion from struct to long in liinux C++?

Issue

I’m converting a C++ Linux program (I don’t know what compiler) to Visual C++ 2019. I’m seeing some strange code.

Today’s example is:

long offset = timezone;

I also see:

offset = timezone - 3600;

timezone appears to be defined as:

struct timezone;

It should be defined as:

struct timezone {
   int tz_minutewest;
   int tz_dsttime;
};

I don’t see any evidence that it is.

It appears that the code is using timezone as if it is timezone.tz_minutewest.

Do some compilers do an automatic conversion?

Solution

The struct timezone you are showing is a Linux/glibc-specific type defined in <sys/time.h>. See man gettimeofday.

There is also a variable named timezone in <time.h> which is specified by POSIX (X/Open). See man tzset for information about the Linux/glibc implementation of that feature.

Of course it is not clear whether you are referring to either of these or something else entirely. However in context where the name is used in your example it cannot name a type.

Even if you have both headers included, using timezone in an expression will refer to the variable, not the type. C++ allows collision between the name of a type and a non-type. In most contexts the non-type will be preferred if not explicitly prefixed with struct or similar keywords.

Answered By – user17732522

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published