Compile error in if statement without braces

Started by
1 comment, last by wilberolive 10 years, 2 months ago

I get an error stating that a ';' is expected after the if statement when I try to compile the following code.


if( true )
   MyHandle @handle = cast<MyHandle>(object.getMyHandle("MyHandleName"));

However if I change the code like this, the error goes away.


MyHandle @handle;
if( true )
   @handle = cast<MyHandle>(object.getMyHandle("MyHandleName"));

The error also goes away if I change the code like this.


if( true )
{
   MyHandle @handle = cast<MyHandle>(object.getMyHandle("MyHandleName"));
}

Is this maybe a bug in the compiler??

Advertisement

It's not a bug. This was a conscious decision on my part.

The thing is that the variable that is declared would only live for that single statement, which makes it useless. It is also a quite common bug in C++ where developers mistakedly declare variables like this and then discover (possibly after long hours of debugging) that the value is not visible in the following statements (especially if the variable happen to have the same name as another variable declared at a higher scope).

The error message could be clearer though. It should be something like 'Cannot declare variable as the only result of condition'.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks for the response. I agree that it is a totally pointless statement and would never actually do it myself, but I just stumbled across it while putting AngelScript through its paces and wasn't sure if it was intentional or a bug. I think it would be a great idea to use clearer messages when you've intentionally deviated away from something that would otherwise compile in c++ for example. Cheers for the prompt support too smile.png

This topic is closed to new replies.

Advertisement