In one of the SO thread, I had seen a usage of unnamed struct
acting as a placeholder for multiple variables of different types inside for loop:
For example:
for(struct {
int i;
double d;
char c;
} obj = { 1, 2.2, 'c' };
obj.i < 10;
++obj.i)
{
...
}
This compiles fine with g++.
Is this a standard C++03 syntax?
You can use an unnamed struct anywhere you can use a struct - the only difference is that it doesn't get a name that can be used somewhere else. You can declare a new type anywhere you can use a type, pretty much. It may not be particularly meaningful to do so in most places, but that's another matter.
I wouldn't exactly recommend this, other than in very special cases, but it's valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With