Search code examples
c++windowsvisual-c++structunions

LARGE_INTEGER and DUMMYSTRUCTNAME


When I come across the definition of LARGE_INTEGER. I find some questions:

typedef union _LARGE_INTEGER {
    struct {
        DWORD LowPart;
        LONG HighPart;
    } DUMMYSTRUCTNAME;
    struct {
        DWORD LowPart;
        LONG HighPart;
    } u;
    LONGLONG QuadPart;
} LARGE_INTEGER;

Can We rewrite as follows:

typedef union _LARGE_INTEGER {
    struct {
        DWORD LowPart;
        LONG HighPart;
    } DUMMYSTRUCTNAME;
    LONGLONG QuadPart;
} LARGE_INTEGER;

I think the struct u is duplicate. So why MSVC implement LARGE_INTEGER as this? I wonder maybe some reason like compatibility.


Solution

  • It is apparently an adjustment to avoid having an anonymous structure in a union. This had been supported by Visual C++, but is not allowed in by the language standard. (See can't make sense of LARGE_INTEGER struct for a declaration of the struct without the DUMMYSTRUCTNAME.) The name would have been added to bring the union into conformance with standard C++, possibly with some sort of automated tool.

    Later versions of LARGE_INTEGER get rid of the structs entirely and just have the QuadPart.