C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static variables

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Andrew Walker
Guest





PostPosted: Wed Oct 22, 2003 6:24 pm    Post subject: static variables Reply with quote



I've just completed an introductory course in C++ at university, one
of the sample questions on the exam is:

what is the advantage of declaring a (global) variable to be "static"?

After having a look through the standard I found Annex D.2/1, which
states:
"The use of the static keyword is deprecated when declaring objects in
namespace scope".

After more reading within this group it seems that the global
namespace is considered a namespace with regard to this issue.

Is there then any advantage? My lecturers opinion was that the
variable's scope would be changed, but I'm not sure whether this is
correct or meaningful.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Blake Kaplan
Guest





PostPosted: Thu Oct 23, 2003 3:28 pm    Post subject: Re: static variables Reply with quote



Andrew Walker wrote:
Quote:
I've just completed an introductory course in C++ at university, one
of the sample questions on the exam is:

It's worth noting that many university courses haven't updated their
courses to the standard, and some professors don't know the standard.

Quote:

what is the advantage of declaring a (global) variable to be "static"?

After having a look through the standard I found Annex D.2/1, which
states:
"The use of the static keyword is deprecated when declaring objects in
namespace scope".

In C code (I think) and in older (pre 1998) C++, you could use the
static qualifier on a variable (and function) at file scope to limit its
linkage to that file. This would allow you to have:
// File1.c
static int foo;

// File2.c
static int foo; // no name clash

The standard has deprecated this usage of 'static' in favor of anonymous
namespaces:

// File1.cpp
namespace { int foo; }
// File2.cpp
namespace { int foo; } // no name clash

Quote:
After more reading within this group it seems that the global
namespace is considered a namespace with regard to this issue.

Is there then any advantage? My lecturers opinion was that the
variable's scope would be changed, but I'm not sure whether this is
correct or meaningful.

The only 'advantage' would be to limit the scope of the variable, though
this is an older usage that should be replaced with anonymous namespaces
in current code.
--
Blake
"Before they had drawing boards, what did they go back to?"
Remove "_nospam_" from my e-mail address to reply


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Edward Diener
Guest





PostPosted: Thu Oct 23, 2003 3:29 pm    Post subject: Re: static variables Reply with quote



Andrew Walker wrote:
Quote:
I've just completed an introductory course in C++ at university, one
of the sample questions on the exam is:

what is the advantage of declaring a (global) variable to be "static"?

After having a look through the standard I found Annex D.2/1, which
states:
"The use of the static keyword is deprecated when declaring objects in
namespace scope".

After more reading within this group it seems that the global
namespace is considered a namespace with regard to this issue.

Is there then any advantage? My lecturers opinion was that the
variable's scope would be changed, but I'm not sure whether this is
correct or meaningful.

The modern C++ way of using non-class static variables is by using an
unnamed namespace in a file. So instead of:

static int SomeVariable;

one would instead use:

namespace
{
int SomeVariable;
}

The declarations in an unnamed namespace are accessible from the translation
unit in which the namespace exists, giving it the same effect as static
declarations.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Thu Oct 23, 2003 3:52 pm    Post subject: Re: static variables Reply with quote

"Andrew Walker" <andal_ (AT) hotmail (DOT) com> wrote in message

Quote:
I've just completed an introductory course in C++ at university, one
of the sample questions on the exam is:

what is the advantage of declaring a (global) variable to be "static"?

After having a look through the standard I found Annex D.2/1, which
states:
"The use of the static keyword is deprecated when declaring objects in
namespace scope".

It seems you may be mixing up two meanings of 'static'. First meaning: make
a function have internal linkage, and this usage is deprecated because
unnamed namespaces are a superior alternative. Second meaning appears to be
what the sample question is about: a class static variable is like a global
variable except it is a member of a class. It respects the public,
protected, and private access levels, so aids encapsulation.

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
rishi
Guest





PostPosted: Thu Oct 23, 2003 3:56 pm    Post subject: Re: static variables Reply with quote

[email]andal_ (AT) hotmail (DOT) com[/email] (Andrew Walker) wrote in message news:<9d1084b4.0310212347.234aa918 (AT) posting (DOT) google.com>...
Quote:
I've just completed an introductory course in C++ at university, one
of the sample questions on the exam is:

what is the advantage of declaring a (global) variable to be "static"?

The linkage of global variable declared with static is internal . This makes
them private to the translation unit in which they are declared/defined.

Without static specifier the linkage is external and the variable can be
accessed from other translation units.

Quote:

After having a look through the standard I found Annex D.2/1, which
states:
"The use of the static keyword is deprecated when declaring objects in
namespace scope".

The use of static specifier to mean internal linkage is deprecated in current
c++ std as it can be achieved using anonymous namespaces. This might be because
of the ambiguity in storage/linkage semantics of the keyword static.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Spangenberg
Guest





PostPosted: Thu Oct 23, 2003 6:48 pm    Post subject: Re: static variables Reply with quote

Hello Andrew Walker!

Andrew Walker schrieb:

Quote:
I've just completed an introductory course in C++ at university, one
of the sample questions on the exam is:

what is the advantage of declaring a (global) variable to be "static"?

After having a look through the standard I found Annex D.2/1, which
states:
"The use of the static keyword is deprecated when declaring objects in
namespace scope".

After more reading within this group it seems that the global
namespace is considered a namespace with regard to this issue.

Is there then any advantage? My lecturers opinion was that the
variable's scope would be changed, but I'm not sure whether this is
correct or meaningful.


The main reason for the deprecation of the static keyword in relation to
specifying a linkage is twofold:

- The static keyword has several meanings in C++ which are easily
mixed up.

- The effect of the meaning of static as linkage descriptor can much
better reached by means of unnamed namespaces.

"Static" in the sense of a linkage descriptor provides a mechanism
which can be described as "name visibility". It effectively makes
the name of a given item invisible outside the translation unit it
is located in. This stands in contrast to external linkage which
provides a global name accessibility. Obviously this global name
access can be viewed as a loss of privacy.

The usage of unnamed namespaces makes it possible to take
advantage of both external linkage (E.g. some template idioms need
external linkage) and access reduction (Because unnamed namespaces
are not accessible from different namespaces).

Hope that helps,

Daniel Spangenberg




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Thu Oct 23, 2003 6:50 pm    Post subject: Re: static variables Reply with quote

[email]andal_ (AT) hotmail (DOT) com[/email] (Andrew Walker) wrote in message
news:<9d1084b4.0310212347.234aa918 (AT) posting (DOT) google.com>...
Quote:
I've just completed an introductory course in C++ at university, one
of the sample questions on the exam is:

what is the advantage of declaring a (global) variable to be "static"?

After having a look through the standard I found Annex D.2/1, which
states:

"The use of the static keyword is deprecated when declaring objects in
namespace scope".

That's because the alternative of an anonymous namespace is considered
superior. Still, this is a recent change, and static won't go away
anytime soon.

Quote:
After more reading within this group it seems that the global
namespace is considered a namespace with regard to this issue.

The global namespace is the standard's name for what C called file
scope. In one or two contexts, the standard even seems to shorten this
to simply global. Which IMHO is a bit of a misnomer -- I've always
understood global to mean names which are accessible from other
translation units (which means that it is a linkage, and not a scope, in
C++ terms).

Quote:
Is there then any advantage?

The semantic of static hasn't changed, even if it has been deprecated.
When used in a declaration at namespace scope, it causes the name to
have internal linkage, instead of external. In traditional terms, it
prevents the object from being global. And in practical terms, the
advantage is that the name cannot conflict with any use of the same name
in other translation units.

Quote:
My lecturers opinion was that the variable's scope would be changed,
but I'm not sure whether this is correct or meaningful.

The standard, being a very precise document, uses scope in a particular
way. Within the range of the standard's use of scope, static has no
effect on scope. In colloquial speach, however, it is common to
confound scope and linkage, and use the word scope for both; in this use
of the word, static restricts the scope.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antonio Maiorano
Guest





PostPosted: Thu Oct 23, 2003 10:43 pm    Post subject: Re: static variables Reply with quote

When global variables are declared 'static', the variable is guaranteed to
have 1 instance of itself per object file. For instance, say in header.h,
you declare:

static int gs_someStaticGlobal;

Then if test1.cpp and test2.cpp include header.h, each of these cpp files
(which compile into object files) have their own copy of
gs_someStaticGlobal. So if test1.cpp modifies the value of this global,
test2.cpp will not be aware of this change.

I discovered this almost by accident. I declared a global variable that I
wanted to share between two cpp files. So in my header, I declared my global
variable (without the static keyword). The code compiled properly, but then
the linker complained about multiple definitions of that global variable.
This makes sense because the two object files each contained a definition
for the global. Anyway, somehow I ended up placing the "static" keyword
before the global variable declaration, thinking it was some language trick
to tell the linker to only use one instance, but alas, I discovered I was
wrong due to the strange results my program exhibited.

The solution was to declare the global in the header using the "extern"
keyword as follows:

extern int g_someGlobal;

And then to declare ONE instance of this global in ONE cpp file as:

int g_someGlobal;

The extern basically tells the compiler "there exists an int called
g_someGlobal declared somewhere, let the linker figure it out". At link
time, there must be one and only one instance of this variable declared in
some object file (a cpp) so that the linker can resolve the uses of this
externed variable.

In the end, as you can see, the use of static on global declarations does
indeed change the variable's scope to that of any cpp file that includes it.

Antonio Maiorano

----- Original Message -----
From: "Andrew Walker" <andal_ (AT) hotmail (DOT) com>
Newsgroups: comp.lang.c++.moderated
Sent: Wednesday, October 22, 2003 2:24 PM
Subject: static variables


Quote:
I've just completed an introductory course in C++ at university, one
of the sample questions on the exam is:

what is the advantage of declaring a (global) variable to be "static"?

After having a look through the standard I found Annex D.2/1, which
states:
"The use of the static keyword is deprecated when declaring objects in
namespace scope".

After more reading within this group it seems that the global
namespace is considered a namespace with regard to this issue.

Is there then any advantage? My lecturers opinion was that the
variable's scope would be changed, but I'm not sure whether this is
correct or meaningful.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Spangenberg
Guest





PostPosted: Fri Oct 24, 2003 7:19 pm    Post subject: Re: static variables Reply with quote

Minor correction:

Daniel Spangenberg schrieb:

Quote:
.. (Because unnamed namespaces are not accessible from different
namespaces).

Should mean "Because unnamed namespaces are not accessible from different
translation units"

Sorry,

Daniel



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Robert Smith
Guest





PostPosted: Fri Oct 24, 2003 7:27 pm    Post subject: Re: static variables Reply with quote

Daniel Spangenberg <dsp (AT) bdal (DOT) de> wrote

Quote:

The usage of unnamed namespaces makes it possible to take
advantage of both external linkage (E.g. some template idioms need
external linkage) and access reduction (Because unnamed namespaces
are not accessible from different namespaces).

Could you give an example of this? Are you saying that a name could
have external linkage but local scope? How? Why?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Sat Oct 25, 2003 8:42 am    Post subject: Re: static variables Reply with quote

"Robert Smith" <rsmith (AT) ncbi (DOT) nlm.nih.gov> wrote in message

Quote:
Could you give an example of this? Are you saying that a name could
have external linkage but local scope? How? Why?

Local classes have internal linkage, and so cannot be used in template
algorithms like std::for_each. This is not good. It would be nice if local
classes had external linkage. Then they would have external linkage and
local scope. But right now this is not in the standard.

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.