> I remember from GCC a syntax which was something like this:
>
> { 2, {.ptr = "test"} },
>
> But I can't figure out how to get it to work in Microsoft's C++
> compiler, and I don't know what to search for to find it via Google.
This is called designated initializers[1] and is part of the C99
standard, not in any C++ standard as far as i know. GCC allows it in
C++ code nonetheless as a GNU extension, similar to
variable-length-arrays. Clang supports it too but as Visual Studio only
has a recent C++ compiler it mostly likely does not.
Just define two overloaded ctors for the structure and use normal list
initialization.
struct SAbc
{
int discriminator;
union {
int value;
const char *ptr;
};
SAbc(int discriminator, int value)
: discriminator(discriminator), value(value) {}
SAbc(int discriminator, const char *ptr)
: discriminator(discriminator), ptr(ptr) {}
};
[1]
https://gcc.gnu.org/onlinedocs/gcc-6.1.0/gcc/Designated-Inits.html
Regards,
Frank