Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnamed struct declaration inside for loop initialization statement

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?

like image 523
iammilind Avatar asked Nov 03 '22 21:11

iammilind


1 Answers

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.

like image 160
Mats Petersson Avatar answered Nov 10 '22 00:11

Mats Petersson