2

I wonder is it possible to put struct in for loop instead of just one variable, i read from this answer, but can't get think work, here is code:

for(struct { int a; char b; } s = { 0, 'a' } ; s.a < 5 ; ++s.a) 
{
    std::cout << s.a << " " << s.b << std::endl;
}

Errors are:

    error C2332: 'struct' : missing tag name
    error C2228: left of '.b' must have class/struct/union
    ...

How to make code work on c++ 11, visual studio 2012?

Community
  • 1
  • 1
tonni
  • 1,155
  • 3
  • 16
  • 34
  • 3
    In case it helps, it works on http://ideone.com/DuPIw6 – jsantander Apr 08 '14 at 08:26
  • yes i also try online compile it work ... it seems that something on my computer is not working ... thnx – tonni Apr 08 '14 at 08:27
  • 2
    I don't have Visual Studio to test with, but did you try adding a tag name as the error suggests? (e.g. `struct s_ {...} s = ...`) – Mike Seymour Apr 08 '14 at 08:28
  • yes and then those error gone, but still you get other errors ... you don't have to put tag name when you define struct out of loop – tonni Apr 08 '14 at 08:30
  • Why would one want to do this? Just curious what advantages this would have over declaring the `struct` right before the loop and/or limiting the scope of the `struct`? It seems more time would be wasted trying to figure this out vs. just doing it a more readable way .. ?? – txtechhelp Apr 08 '14 at 08:33
  • when you need temporary variables in one scope (for loop scope) that is why you want to make struct inside of loop – tonni Apr 08 '14 at 08:34
  • and scope limiting brackets wouldn't achieve this? – txtechhelp Apr 08 '14 at 08:35
  • I think you found a compiler bug – M.M Apr 08 '14 at 08:36
  • @MattMcNabb i think that something at my visual studio don't work as well thnx all for help – tonni Apr 08 '14 at 08:37
  • 1
    I don't have Visual Studio to test with either, but on g++ the following options work (in addition to the on in the question): `for( struct X { int a; char b; } s = { 0, 'a' } ; s.a < 5 ; ++s.a) { ... }` and `struct X { int a; char b; }; for( X s = { 0, 'a' } ; s.a < 5 ; ++s.a) { ... } ` note on this last one, the variable (s) is still local to the loop (but the X type is not). – jsantander Apr 08 '14 at 08:37
  • @jsantander thnx for effort it still don't work on my machine ... but i see it working on online compiles – tonni Apr 08 '14 at 08:41
  • `{ struct X { int a; char b; }; for( X s = { 0, 'a' } ; s.a < 5 ; ++s.a) { ... } }` will limit the scope of your struct and variable to the loop with the extra `{}` scope brackets .. – txtechhelp Apr 08 '14 at 08:43
  • Visual Studio 2013 gives quite different errors, but also does not appear to support this. – Bart van Nierop Apr 08 '14 at 08:45

3 Answers3

3

You can use an std::tuple (reference):

#include <tuple>

for(std::tuple<int,char> s(0,'a'); std::get<0>(s) < 5 ; ++std::get<0>(s)) 
{
    std::cout << std::get<0>(s) << " " << std::get<1>(s) << std::endl;
}

Not sure how well this is support by MSVC2012. Update: it does work with MSVC2012.

Danvil
  • 20,344
  • 18
  • 61
  • 85
  • 1
    Since the question is specifically tagged `Visual Studio 2012`, why would you give an answer that is not tested with that particular IDE? – Bart van Nierop Apr 08 '14 at 08:54
  • Does work **IF** the `s{0,'a'}` is replaced by `s(0,'a')`. In VS2012. – TobiMcNamobi Apr 08 '14 at 08:55
  • @TobiMcNamobi can you post full code with struct which you defined ... this don't work on vs12 as i can see – tonni Apr 08 '14 at 08:58
  • @tonni Well, what I would do is already in the answer I have given myself. To make Danvils answer work you must not use an initializer list (i.e. use `()` instead of `{}` for initializing `s`) and include tuple `#include `, of course. – TobiMcNamobi Apr 08 '14 at 09:04
  • @BartvanNierop: Because "not sure" != "does not work" ;) – Danvil Apr 08 '14 at 09:09
  • @Danvil put full code just put also base struct inside (struct { int a; char b; } s;) ... for other people who interested in this post (and include ) it work on vs 2012 – tonni Apr 08 '14 at 09:10
2

As comments have pointed out, with what you are trying to do, some compilers might not exactly give the results you are wanting.

If you're merely trying to limit the scope of a struct or variable type to a limited area (without having to go into another function, etc.), you can just add some extra brackets around your code, ex:

{ // scope start
struct X { int a; char b; };
for(X s = { 0, 'a' } ; s.a < 5 ; ++s.a) 
{
    std::cout << s.a << " " << s.b << std::endl;
}
} // scope end .. struct X no longer 'visible'

This has the advantage of being more 'readable' as well as have a better chance of 'playing nice' with other compilers.

Hope that helps

Edit:

Even the above code doesn't play well with VS2012, here's what DID work with VS2012 limiting scope:

{ // scope start
struct X { int a; char b; X(int z, char y) : a(z), b(y) {} };
for(X s(0, 'a') ; s.a < 5 ; ++s.a)
{
    std::cout << s.a << " " << s.b << std::endl;
}
} // scope end .. struct X no longer 'visible'
txtechhelp
  • 6,147
  • 1
  • 27
  • 36
2

My suggestions:

struct X
{
    X(int a_, char b_)
        : a(a_), b(b_)
    {}
    int a; char b;
};

for(X s(0, 'a'); s.a < 5 ; ++s.a)
{
    std::cout << s.a << " " << s.b << std::endl;
}

You have two problems (in VS 2012, that is!): 1. Declaring a struct in the first for statement. 2. Initializing the local variable c with the brace syntax

I get errors for each of these issues separately. But both are addressed with the above code. I hope this comes close enought to what you are searching for.

TobiMcNamobi
  • 4,305
  • 2
  • 29
  • 51