Will all anonymous namespace variables act as static ?

Will all anonymous namespace variables act as static ?

Post by qazm » Tue, 29 Oct 2002 20:05:58



namespace
{
  boolean firstTime = true ;  // LINE-A

Quote:}

void myFunction( )
{
   if( firstTime )
   {        
      // do_something ;    
      firstTime =  false ;
   }

Quote:}

At line 'LINE-A',  should I declare firstTime as 'static' explicitly if
I want to make it as static ? I hope, all anonymous namespace variables
will act as static variables.  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 ?
 
 
 

Will all anonymous namespace variables act as static ?

Post by Jon Bill » Tue, 29 Oct 2002 20:44:51



> namespace
> {
>   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:

void myFunction()
{
  static bool firstTime = true;
  if(firstTime)
  {
    // do something
    firstTime = false;
  }

Quote:}

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.

Cheers,

Jon.

 
 
 

Will all anonymous namespace variables act as static ?

Post by Victor Bazaro » Tue, 29 Oct 2002 22:57:02




> > namespace
> > {
> >   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.

> > I hope, all anonymous namespace
> > variables will act as static variables.

> They will have internal linkage, if that's what you are expecting.

Actually, they will have _external_ linkage (meaning that their name
is going to be exposed) just like any other "global" object declared
in a namespace.  The thing is, that it is just impossible to know
their exposed name outside of the translation unit.  See 7.3.1.1,
footnote 82.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Will all anonymous namespace variables act as static ?

Post by Jon Bill » Wed, 30 Oct 2002 04:44:55


[snip]

Quote:>> They will have internal linkage, if that's what you are expecting.

> Actually, they will have _external_ linkage (meaning that their name
> is going to be exposed) just like any other "global" object declared
> in a namespace.  The thing is, that it is just impossible to know
> their exposed name outside of the translation unit.  See 7.3.1.1,
> footnote 82.

Ah yes, thanks Victor.

Quote:> Victor

Jon.
 
 
 

1. Anonymous namespace vs. static variables

It's always recommended to declare variables inside an anonymous namespace
to get the effect of static variables declared at file scope. For instance:

static int SomeVar ; // Anachronism

namespace            // Recommended
{
    int OtherVar ;

void main()
{
    :
    :
    :

What advantage does this approach have over the conventional one?

Mandar

2. My time offset is getting biger!

3. static and anonymous namespace

4. FS: Games, New listing

5. Anonymous namespaces and static linkage

6. Game Software

7. Anonymous static variable?

8. Hash codes for min-matching?

9. Problems with static variables (Namespace/lifetime)

10. Windows Explorer 'acts up' while debugging namespace

11. Static variables and ordinary variables

12. static variables in DLLs and local variables in FORTRAN

13. static variables vs global variables vs performance