Can I create a for loop containing multiple type declarations?
For example:
Is there anything I can do, that might allow me to do this:
for(TiXmlElement * pChild = elem->First(), int i=0; // 开发者_运维百科note multiple type declarations
    pChild; 
    pChild=pChild->NextSiblingElement(), i++) // note multiple types
{
    //do stuff
}
Perhaps there is a boost header?
Nope.
If you want to limit the scope of variables to the loop just add another scope:
{
    TiXmlElement * pChild = elem->First();
    int i = 0;
    for(; pChild; pChild=pChild->NextSiblingElement(), i++)
    {
        //do stuff
    }
}
Blocks do not have to be attached to functions or conditionals. You can surround any piece of code with a block to limit the scope of temporary variables to that block.
{
    TiXmlElement * pChild;
    int i;
    for ( pChild = elem->First(), i = 0;
          pChild;
          pChild = pChild->NextSiblingElement(), ++i )
    {
        // do stuff
    }
}
Since C++17, introducing multiple variables is possible with structured bindings:
// multiple type declarations
for (auto [pChild, i] = std::tie(elem->First(), 0); pChild; pChild = pChild->NextSiblingElement(), ++i) {
    // ...
}
pChild is a TiXmlElement*, and i is an int, as expected.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论