Topic: extern and namespace


Author: devnull@dummy.de (Johnny Tevessen)
Date: 1999/12/11
Raw View
Francis Glassborow <francis@robinton.demon.co.uk> wrote:

[unnamed namespaces]
> No, AFAIK they have external linkage, but you cannot use their linkage
> name outside the current translation unit.  Subtle, but it makes a
> difference for template use.

How do I make an object with internal linkage then?
Especially functions and POD types. I read that the
use of "static" for this purpose is deprecated/obsolete,
so I used unnamed namespaces instead.

What is the standard way of telling the compiler that
it really won't have to create this function/object
"in code" because it isn't needed in another translation
unit?

No, not "inline".

ciao,
johnny
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/12/13
Raw View
In article <38510734-comp.std.c@antity.dummy.de>, Johnny Tevessen
<devnull@dummy.de> writes
>What is the standard way of telling the compiler that
>it really won't have to create this function/object
>"in code" because it isn't needed in another translation
>unit?

That is an optimisation problem.  But the compiler 'knows' that anything
in an unnamed namespace cannot be referred to elsewhere, so if it is not
referred to locally then it can ignore it.

The difference is really language law stuff:) in practice.

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Biju Thomas <b_thomas@ibm.net>
Date: 1999/12/07
Raw View
Steve Clamage wrote:
>
[ good explanation of scope and linkage snipped]
>
> At namespace scope, a function has external linkage unless explicitly
> declared static.

This statement doesn't apply to unnamed namespaces, right? That is,
objects in unnamed namespaces always have internal linkage.

--
Regards,
Biju Thomas
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/12/07
Raw View
In article <3846FA63.1CA4CEC8@ibm.net>, Biju Thomas <b_thomas@ibm.net>
writes
>This statement doesn't apply to unnamed namespaces, right? That is,
>objects in unnamed namespaces always have internal linkage.

No, AFAIK they have external linkage, but you cannot use their linkage
name outside the current translation unit.  Subtle, but it makes a
difference for template use.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Darin Adler <darin@bentspoon.com>
Date: 1999/11/30
Raw View
Alexandre Hulot <ahulot@rstcorp.com> wrote:

> The definition to which the extern declaration refers must be in the
> global namespace (I am not completly sure about that, but how could an
> "extern int a" refers to a variable a declare inside a namespace, for
> example. There is the using directive for that)

An identifier in any namespace can have either internal or external linkage.
The two concepts are largely orthogonal. The using directive lets you make
an identifier that's already declared in one namespace visible in another.
It does not allow you to declare an identifier.

> When I look at the stdio.h header file provided with my compiler, I do
> see a lot of extern in it. Since I could include <cstdio> and then I
> would be in the std namespace. What does that mean to have extern int
> f() or extern int a.... inside a namespace. I am able to access perror
> (for example) using std::perror(...) even if the function was declared
> extern in the namespace??

Having something declared with external linkage inside a namespace means
that the object must be defined in the same namespace but possibly in
another translation unit.

    -- Darin
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <david@tribble.com>
Date: 1999/11/30
Raw View
Alexandre Hulot wrote:
> I [am] try[ing] to understand the internal and external linkage and
> its relationship with namespace.
>
> As far as I have understood, the extern keyword can be used anywhere
> in a translation unit in order to declare a function, class,
> etc. that is defined in another translation unit.

No, only variables, constants, and functions can be declared extern.
Types (classes, structs, unions, enums, and typedefs) are not objects
or functions, so they don't define data or code, so they can't be
extern.

> The definition to which the extern declaration refers must be in the
> global namespace (I am not completly sure about that, but how could an
> "extern int a" refers to a variable a declare inside a namespace, for
> example. There is the using directive for that).

Perhaps some examples will clarify:

    extern int    foo();           // extern func ::foo()
    extern float  bar();           // extern func ::bar()

    extern int        a;           // extern var ::a
    extern const int  sz = 12;     // extern const ::sz

    namespace Foo
    {
        extern int  foo();         // extern func Foo::foo()
        float       bar();         // extern func Foo::bar()
        char *      get();         // extern func Foo::get()

        extern int        a;       // extern var Foo::a
        extern const int  sz = 2;  // extern const Foo::sz
    }

Constants are a little different than variables, in that they are
static by default; declaring a constant 'extern' forces its name to
be visible outside the file it's defined in.

Normally, the code above would have to supply the fully qualified
(prefixed) name 'Foo::get()' in order to invoke the Foo::get()
function.  The 'using' directive is used to remove the need for the
explicit 'Foo::' prefix:

    ... c = Foo::get(); ...   // calls Foo::get()

    using Foo::get;

    ... c = get(); ...        // calls Foo::get()

> It is a little confusing for me. Any help appreciated.

You're not alone in your confusion.

-- David R. Tribble, david@tribble.com, http://david.tribble.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/11/30
Raw View
Alexandre Hulot wrote:
>
> I try to understand the internal and external linkage and its
> relationship with namespace.

There is no relationship. They are completely independent concepts.

A namespace affects the scope of a name, not its linkage. A name
can have external or internal linkage, whether it is in a namespace
or not. "Scope" refers to the visibility of a name in source code
within one translation unit. "Linkage" describes how a name is
related to the same name in a different translation unit.

A name with external linkage refers to the same entity as the same
name appearing in other translation units. That is all that external
linkage means. If a name in a translation unit does not have external
linkage, it does not refer to any entity in any other translation unit.

The term "namespace scope" means within a namespace or at global scope.
Global scope is also referred to as the global namespace. Examples:

 int i1; // global scope (in the global namespace)
 namespace N1 {
  int i2; // namespace scope
  namespace N2 {
   int i3; // namespace scope
  }
  class C { // namespace scope
   int i4; // class scope
  };
 }
 void f() { // namespace scope
  int i5; // local scope
 }
 namespace { // the unnamed namespace
  int i6; // namespace scope
 }
 int i7; // global scope

A name declared at namespace scope has external linkage unless it is
declared "const" or "static". If declared static, it has internal
linkage. If declared const and not also explicitly declared extern,
it has internal linkage. Examples, at namespace scope:

 int i1;                  // external linkage
 static int i2;           // internal linkage
 extern int i3;           // external linkage
 const int i4 = 1;        // internal linkage
 extern const int i5;     // external linkage
 extern const int i6 = 1; // external linkage
Putting any of the declarations in a namespace does not affect
its linkage.

At namespace scope, an object declaration that is not explicitly
declared extern is also a definition. If it is explicitly declared
extern, it is a definition if it has an initializer, and just a
declaration otherwise. Examples, at namespace scope:

 int i1;                  // definition
 static int i2;           // definition
 extern int i3;           // declaration
 const int i4 = 1;        // definition
 extern const int i5;     // declaration
 extern const int i6 = 1; // definition

At namespace scope, a function has external linkage unless explicitly
declared static. A function declaration that provides the function
body is a definition.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Alexandre Hulot <ahulot@rstcorp.com>
Date: 1999/11/26
Raw View
Hello,

I try to understand the internal and external linkage and its
relationship with namespace.

As far as I have understood, the extern keyword can be used anywhere in
a translation unit in order to declare a function, class, etc...that is
defined in an other translation unit.

The definition to which the extern declaration refers must be in the
global namespace (I am not completly sure about that, but how could an
"extern int a" refers to a variable a declare inside a namespace, for
example. There is the using directive for that)

When I look at the stdio.h header file provided with my compiler, I do
see a lot of extern in it. Since I could include <cstdio> and then I
would be in the std namespace. What does that mean to have extern int
f() or extern int a.... inside a namespace. I am able to access perror
(for example) using std::perror(...) even if the function was declared
extern in the namespace??


It is a little confusing for me. Any help appreciated.

Alexandre


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]