> boolean firstTime = true ; // LINE-A
> void myFunction( )
> if( firstTime )
> // do_something ;
> firstTime = false ;
> At line 'LINE-A', should I declare firstTime as 'static' explicitly
> if I want to make it as static ?
File-scope static is deprecated and should be replaced with unnamed
namespace, as you have done in your example.
Quote:> I hope, all anonymous namespace
> variables will act as static variables.
They will have internal linkage, if that's what you are expecting.
Quote:>Is it guaranteed that the
> code at "do_something" will be executed only once when 'myFunction'
> is called for the first time and the subsequent calls to myFunction
> will skip the code inside the 'if() block ?
Yes, but only because of the manually coded check. You seem to be confused
between file-scope static and local scope static (hardly surprising). If you
have a locally scoped static, such as:
static bool firstTime = true;
// do something
firstTime = false;
Then the variable firstTime will be constructed only once, on the first call
to myFunction (before entry to the function), and will be destructed at
program exit. File-scope static is different - it means "internal linkage",
which says that no linkage symbols will be exported from the translation
unit, and therefore the entity's representation cannot be shared amongst
translation units. In your example, firstTime can be shared amongst scopes
within the same translation unit, which may not be what you want. Unnamed
namespace is a direct replacement for file-scope static.