Dinaiz
Dinaiz

Reputation: 2233

C++ creating a scope at the file level

Everybody knows that writing a "using" statement inside a .h is a terrible terrible thing.

Now, I'm using a tool to generate an UI (for those who know Juce, it's the Jucer) , which requires this kind of using statement in the .h .

To avoid this, I edited the template files to enclose this using into namespaces but that's not enough protection as those very namespaces are used elsewhere.

Therefore I tried to enclose this using directive into an unnamed scope like this :

namespace Gui 
{
    { 
         using namespace juce;

        <generated code>
    }
}

This seemed fine to me but Visual studio generates an error :

'{' : missing function header (old-style formal list?) The compiler encountered an unexpected open brace at global scope. In most cases, this is caused by a badly-formed function header, a misplaced declaration, or a stray semi-colon. To resolve this issue, verify that the open brace follows a correctly-formed function header, and is not preceded by a declaration or a stray semi-colon. This error can also be caused by an old-style C-language formal argument list. To resolve this issue, refactor the argument list to use modern style—that is, enclosed in parentheses.

Do you know another way to achieve that - except enclosing that in yet another namespace, which would change tons of legacy code ?

EDIT : I ended up modifying the generator tool. If someone needs it, contact me.

Upvotes: 3

Views: 199

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

Nope, you can't create a block scope outside of a function.

You'll have to enclose it in another namespace or find a way to work with Juce that doesn't result in this requirement, perhaps by modifying the code generator, or by adding a post-processing step on the generated code that turns everything into fully-qualified names.

Or you could live with it; if your whole project uses Juce, and you're not creating a library, then this isn't so bad.

Upvotes: 4

Related Questions