-2

Look at this snippet:

void Sample_Compositor::createEffects(void)
{
    Ogre::CompositorPtr comp3 = Ogre::CompositorManager::getSingleton().create("Motion Blur", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
    {
        {
            Ogre::CompositionTargetPass *tp = t->getOutputTargetPass();
            tp->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
            {
                Ogre::CompositionPass *pass = tp->createPass();
                pass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
                pass->setMaterialName("Ogre/Compositor/MotionBlur");
                pass->setInput(0, "sum");
            }
        }
    }
}

If this code is legit, how do these blocks work?

James Adkison
  • 9,412
  • 2
  • 29
  • 43
adflixit
  • 39
  • 1
  • 6

2 Answers2

2

They are simply blocks that introduce scope, and hide their contents. Perfectly valid.

Paul Beckingham
  • 14,495
  • 5
  • 33
  • 67
2

If s; is a valid statement, then so is { s; } and {{ s; }} and so on.

Adding redundant braces is perfectly legitimate, but entirely pointless.

The interesting question is why someone has written code this way. My guess is that the author is worried about exactly when local variables get destroyed. But almost all of the time that's the wrong thing to worry about - clarity of code trumps almost anything.

(There are occasionally times when apparently redundant braces are needed - in a switch statement, for example - but the code you show is not one of those.)

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64