Topic: static and namespaces
Author: schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann)
Date: 31 Jan 1995 16:14:38 GMT Raw View
In article <9502607.29359@mulga.cs.mu.OZ.AU>, fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
[..]
|> > namespace Foo { int x; static int y; };
|> >
|> > how do x and y differ?
|>
|> `x' has external linkage, `y' has internal linkage.
|> In anonther translation unit, you can get at the value of `x'
|>
|> namespace Foo { extern int x; }
[..]
So
namespace Foo {
extern int ::glob_x;
extern int x;
static int y;
};
declares ::glob_x and Foo::x of external and defines Foo::y of internal linkage.
In the same manner
namespace {
extern int ::glob_x;
int y;
};
declares ::glob_x of external and defines Foo::y of internal linkage.
But what's about:
namespace {
extern int x; // ERROR ???
};
Is this an error or does it declare ::x of external linkage?
NB This would throw "x" from the unnamed into the global namespace
without qualification!
Ulf Schuenemann
--------------------------------------------------------------------
Ulf Sch nemann
Institut f r Informatik, Technische Universit t M nchen.
email: schuenem@informatik.tu-muenchen.de
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Wed, 25 Jan 1995 20:09:39 GMT Raw View
smeyers@netcom.com (Scott Meyers) writes:
>I'm confused about the meaning of variables at namespace scope versus
>global variables and file static variables. Before namespaces, if I
>said:
>
> SomeClass a; // at file scope
> static SomeClass b; // at file scope
>
>I knew that a had external linkage and b had internal linkage. For all
>intents and purposes, a was a global variable, b a file variable.
Yes, that's all still true.
>According to Bjarne's Design and Evolution (p. 412), the global scope
>becomes another namespace, so presumably the above is equivalent to
>
> namespace {
> SomeClass a;
> static SomeClass b;
> };
Nope. The global scope is a namespace, but it's not the same as an
unnamed namespace such as the one above.
>This gives rise to several questions in my mind:
>
> - What does static mean for variables at namespace scope? How does it
> differ from a non-static variable defintion at namespace scope?
> That is, given
>
> namespace Foo { int x; static int y; };
>
> how do x and y differ?
`x' has external linkage, `y' has internal linkage.
In anonther translation unit, you can get at the value of `x'
namespace Foo { extern int x; }
int next_x() { return Foo::x++; }
but there's no way to get at `y'.
> - With the advent of namespaces and the elimination of the global
> scope (it's now the unnamed namespace), how do I declare, define,
> and use global variables?
I think you have a big confusion between the global namespace
and unnamed namespaces. The global namespace doesn't have a name,
as such, but unlike unnamed namespaces you can refer to it from
other translation units (since they all share the same global namespace).
> - With the advent of namespaces, how do I achieve what I used to
> achieve by declaring something file static, i.e., a name unique to
> a translation unit?
You can do it either the same way that you used to do it (although
that is now deprecated), or by enclosing it in an unnamed namespace.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
all [L] (programming_language(L), L \= "Mercury") => better("Mercury", L) ;-)
Author: smeyers@netcom.com (Scott Meyers)
Date: Mon, 16 Jan 1995 20:31:01 GMT Raw View
I'm confused about the meaning of variables at namespace scope versus
global variables and file static variables. Before namespaces, if I
said:
SomeClass a; // at file scope
static SomeClass b; // at file scope
I knew that a had external linkage and b had internal linkage. For all
intents and purposes, a was a global variable, b a file variable.
According to Bjarne's Design and Evolution (p. 412), the global scope
becomes another namespace, so presumably the above is equivalent to
namespace {
SomeClass a;
static SomeClass b;
};
This is, in turn, apparently equivalent to the following non-namespace
definitions (based on p. 420 of the book):
static SomeClass a;
static SomeClass b;
Notice that a has now lost its external linkage.
This gives rise to several questions in my mind:
- What does static mean for variables at namespace scope? How does it
differ from a non-static variable defintion at namespace scope?
That is, given
namespace Foo { int x; static int y; };
how do x and y differ?
- With the advent of namespaces and the elimination of the global
scope (it's now the unnamed namespace), how do I declare, define,
and use global variables?
- With the advent of namespaces, how do I achieve what I used to
achieve by declaring something file static, i.e., a name unique to
a translation unit?
Thanks,
Scott
Author: jason@cygnus.com (Jason Merrill)
Date: Mon, 16 Jan 1995 22:32:30 GMT Raw View
>>>>> Scott Meyers <smeyers@netcom.com> writes:
> I'm confused about the meaning of variables at namespace scope versus
> global variables and file static variables. Before namespaces, if I
> said:
> SomeClass a; // at file scope
> static SomeClass b; // at file scope
> I knew that a had external linkage and b had internal linkage. For all
> intents and purposes, a was a global variable, b a file variable.
> According to Bjarne's Design and Evolution (p. 412), the global scope
> becomes another namespace, so presumably the above is equivalent to
> namespace {
> SomeClass a;
> static SomeClass b;
> };
Nope; it is equivalent to
namespace Magic_Global_Namespace_Name {
SomeClass a;
static SomeClass b;
};
And has the same semantics as it did before. The global namespace cannot
be referred to by name, only by the unary :: operator, just as before.
> - What does static mean for variables at namespace scope? How does it
> differ from a non-static variable defintion at namespace scope?
> That is, given
> namespace Foo { int x; static int y; };
> how do x and y differ?
y is static, x is not. Only the unnamed namespace is related to static.
> - With the advent of namespaces and the elimination of the global
> scope (it's now the unnamed namespace), how do I declare, define,
> and use global variables?
The global namespace is not the same as the unnamed namespace.
> - With the advent of namespaces, how do I achieve what I used to
> achieve by declaring something file static, i.e., a name unique to
> a translation unit?
int public;
namespace {
int unique_to_translation_unit;
}
Jason