Robinson
Robinson

Reputation: 10132

Initialiser, specific member of a union

I have a feeling the answer to this question is no, but is it possible to initialise a specific member of a union? For example the following:

#include <cassert>
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    auto time = 20090520145024798ull;

    auto large = ULARGE_INTEGER() = {           
        { time }
    };

    assert(large.QuadPart == time);

    return 0;
}

(Visual Studio 2013, Windows 10), produces a compiler "conversion from 'unsigned __int64' to 'DWORD'", implying it's going to try to shoe-horn the uint64_t into the DWORD.

ULARGE_INTEGER is the union:

typedef union _ULARGE_INTEGER {
    struct {
        DWORD LowPart;
        DWORD HighPart;
    } u;
    ULONGLONG QuadPart;
} ULARGE_INTEGER;

What does the standard say about order of initialisation in cases like this? I had hoped the compiler would see that QuadPart was the appropriate member to assign.

Upvotes: 1

Views: 121

Answers (2)

Jts
Jts

Reputation: 3527

Keeping it simple:

ULARGE_INTEGER large;
large.QuadPart = time;

Or if you want to use auto and initialize it in one line, make a little helper perhaps?

inline auto MAKE_ULARGE_INTEGER(ULONGLONG t)
{
    ULARGE_INTEGER result;
    result.QuadPart = t;
    return result;
}

auto large = MAKE_ULARGE_INTEGER(time);

Upvotes: 3

Barry
Barry

Reputation: 303517

With unions, you can use list-initialization to initialize the first member only, as per [dcl.init.aggr]:

When a union is initialized with a brace-enclosed initializer, the braces shall only contain an initializer-clause for the first non-static data member of the union. [ Example:

union u { int a; const char* b; };
u a = { 1 };
u b = a;
u c = 1; // error
u d = { 0, "asdf" }; // error
u e = { "asdf" }; // error

—end example ]

So, in your example, the following:

ULARGE_INTEGER large{time};

would initialize u.LowPart, not QuadPart, regardless of what the types of the various members are.

If you want to do anything else, you'll have to be explicit about it:

ULARGE_INTEGER large;
large.QuadPart = time;

Or write a better union type that actually has a constructor.

Upvotes: 4

Related Questions