CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2021
    Posts
    6

    Do not understand conditional in for loop

    Hello, I was hoping someone could help me understand the conditional in this for loop:

    Code:
    for (auto i = maps.begin(); i != maps.end(); i++)
        {
          if (&((*i).second) == this)
          {
            assert(!getManager().hasActiveMap());
            ret = ret && getManager().setActiveMap((*i).first);
          }
        }
    what is happening here:

    Code:
    if (&((*i).second) == this)
    is this just asking for the value located at "second" of the index i? If so, could this not just be:

    Code:
    if(&*i.second)
    ?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Do not understand conditional in for loop

    Quote Originally Posted by Circuits View Post
    what is happening here:

    Code:
    if (&((*i).second) == this)
    is this just asking for the value located at "second" of the index i? If so, could this not just be:

    Code:
    if(&*i.second)
    ?
    No.
    just because
    Code:
    (*i).second
    is the same as
    Code:
     i->second
    Besides it is in the most cases easier to use construction like
    Code:
    for (auto& it : maps)
    rather than
    Code:
    for (auto i = maps.begin(); i != maps.end(); i++)
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Do not understand conditional in for loop

    You need to consider precedence. . has the highest level and then & (as address). and * (as de-reference). So to have .second you need (*I).

    You could have:

    Code:
    if (&i->second == this)
    as -> has a higher precedence than &
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jan 2021
    Posts
    6

    Re: Do not understand conditional in for loop

    Would this be equivalent then?

    Code:
    for (auto&& i : maps)
        {
          if (i.second == this)
          {
            assert(!getManager().hasActiveMap());
            ret = ret && getManager().setActiveMap((*i).first);
          }
        }

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Do not understand conditional in for loop

    Quote Originally Posted by Circuits View Post
    Would this be equivalent then?

    Code:
    for (auto&& i : maps)
        {
          if (i.second == this)
          {
            assert(!getManager().hasActiveMap());
            ret = ret && getManager().setActiveMap((*i).first);
          }
        }
    Does it compile???
    Victor Nijegorodov

  6. #6
    Join Date
    Jan 2021
    Posts
    6

    Re: Do not understand conditional in for loop

    Oh, heh no, no it doesn't

    Code:
        for (auto& i : maps)
            {
              if (&i.second == this)
              {
                assert(!getManager().hasActiveMap());
                ret = ret && getManager().setActiveMap((i).first);
              }
            }

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Do not understand conditional in for loop

    Quote Originally Posted by Circuits View Post
    Oh, heh no, no it doesn't
    It doesn't compile? And what the compiler says?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured