I was reading an article on a ‘tag based’ resource system implemented by Bungie for the BLAM! game engine. And I came across a mysterious syntax (C structs I think?) and I was wondering what permits this? How can this syntax be valid? And by what method is it achieved?
I have pasted the snippet that is of question below.
TAG_GROUP(
sound_environment,
SOUND_ENVIRONMENT_TAG,
sizeof(sound_environment))
{
{_field_real, "room intensity"},
{_field_real, "room intensity hf"},
{_field_real, "room rolloff (0 to 10)"},
{_field_real, "decay time (.1 to 20)" },
{_field_real, "decay hf ratio (.1 to 2)"},
{_field_real, "reflections intensity:dB[-100,10]"},
{_field_real, "reflections delay (0 to .3):seconds" },
{_field_real, "reverb intensity:dB[-100,20]"},
{_field_real, "reverb delay (0 to .1):seconds"},
{_field_real, "diffusion"},
{_field_real, "density"},
{_field_real, "hf reference(20 to 20,000)},
};
TAG_GROUP looks like a macro; if so, then it’s expanding to something else, in which case the syntax could be correct depending on what it’s expanding to.
For instance, if TAG_GROUP is expanding to a 2D array or even an array of structs, then that initialization is valid, as it’s a standard initialzation list.
Take the following program, which compiles:
Now, imagine if TAG_NAME used the parameters to build up the appropriate definitions and tacked on an =. Then essentially, it built up the equivalent of the const char* a[12][2] (or whatever types it’s using).
A good rule of thumb when you see code like this is to think macro, and think of what substitutions can lead to legal code. Also, a convention I have often seen is that names that look like functions, but are in all caps are macros.