Topic: namespaces with static linkage?


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/09/24
Raw View
Jason Merrill wrote:
>
> >>>>> David R Tribble <david.tribble@noSPAM.central.beasys.com> writes:
>
>  >> There is one special additional rule (3.5 Program and linkage,
>  >> paragraph 4) that the name of a namespace declared within an
>  >> unnamed namespace does not have external linkage.
>
>  > (Try saying that several times quickly.)  So can a namespace
>  > declared within an unnamed namespace NOT be used as a template
>  > parameter (since it doesn't have external linkage)?
>
> Namespaces can't be used as template parameters anyway, so the rule is
> irrelevant.

Is there *anything* for which the rule is relevant?


[ 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@noSPAM.central.beasys.com>
Date: 1998/09/09
Raw View
Steve Clamage wrote:

> Members of the unnamed namespace have external linkage,
> but you can't write the external name from any other
> translation unit. That is, the effect is as if the
> unnamed namespace has a special hidden name that only
> the compiler knows or can use.

> The effect is almost the same as if the members have
> internal linkage, so I find it helpful to think of the
> unnamed namespace as creating an area of internal linkage.

> The exception is that you can use members of an unnamed
> namespace as template parameters. That means in general
> (when the template implementation is separately compiled)
> that the template parameters need external linkage.

> There is one special additional rule (3.5 Program and linkage,
> paragraph 4) that the name of a namespace declared within an
> unnamed namespace does not have external linkage.

(Try saying that several times quickly.)  So can a namespace
declared within an unnamed namespace NOT be used as a template
parameter (since it doesn't have external linkage)?

-- David R. Tribble, dtribble@technologist.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: Jason Merrill <jason@cygnus.com>
Date: 1998/09/10
Raw View
>>>>> David R Tribble <david.tribble@noSPAM.central.beasys.com> writes:

 >> There is one special additional rule (3.5 Program and linkage,
 >> paragraph 4) that the name of a namespace declared within an
 >> unnamed namespace does not have external linkage.

 > (Try saying that several times quickly.)  So can a namespace
 > declared within an unnamed namespace NOT be used as a template
 > parameter (since it doesn't have external linkage)?

Namespaces can't be used as template parameters anyway, so the rule is
irrelevant.

Jason
---
[ 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: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1998/08/31
Raw View
Section 3.5, [basic.link]/4, says

>> A name having namespace scope has external linkage if it is the
>> name of ... a namespace (7.3), unless it is declared within an
>> unnamed namespace.

I assume this talks about code like

namespace{
  namespace ns{
  }
}

and defines that ns has no linkage. Why is this restriction important?
IOW, what would change if the 'unless ...'  part is omitted?

TIA,
Martin


[ 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: AllanW@my-dejanews.com
Date: 1998/08/31
Raw View
In article <p6q3eaduwyu.fsf@pandora.inst-inf-1.hu-berlin.de>,
  Martin von Loewis <loewis@informatik.hu-berlin.de> wrote:
>
> Section 3.5, [basic.link]/4, says
>
> >> A name having namespace scope has external linkage if it is the
> >> name of ... a namespace (7.3), unless it is declared within an
> >> unnamed namespace.
>
> I assume this talks about code like
>
> namespace{
>   namespace ns{
>   }
> }
>
> and defines that ns has no linkage.

That's how I read it. This is the "unless" part of the sentance; IOW,
the special case.

> Why is this restriction important?

A careful reading of the general case (the first part of the sentence)
shows that it states exactly the opposite; that is,

    // File1.h
    namespace myspace {
    //        ^^^^^^^  Namespace scope, but not unnamed namespace
        namespace ns{
        //        ^^  A name of a namespace has external linkage
            int i;
        };
    };

has external linkage.  Which the programmer probably relies on:

    // File2.cpp
    #include "File1.h"
    void modify_ns_i() { ++myspace::ns::i; }

> IOW, what would change if the 'unless ...'  part is omitted?

What would change is the first example, the one you posted. The
second example, which I typed in, would remain the same.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ 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: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/09/05
Raw View
stephen.clamage@sun.com (Steve Clamage) writes:

>Martin von Loewis <loewis@informatik.hu-berlin.de> writes:

>>fjh@cs.mu.OZ.AU (Fergus Henderson) writes:

>>> I don't think things inside anonymous namespaces *need* internal
>>> linkage.

>>Thanks for your response. The question then is, why does the standard
>>mandate internal linkage on namespaces inside unnamed namespaces,
>>whereas it defines normal linkage (i.e. external by default) for
>>everything else.

>The entire point and only purpose of an unnamed namespace is to
>prevent the contents from having external linkage.

As Martin pointed out to me in private email, I misspoke.

Members of the unnamed namespace have external linkage,
but you can't write the external name from any other
translation unit. That is, the effect is as if the
unnamed namespace has a special hidden name that only
the compiler knows or can use.

The effect is almost the same as if the members have
internal linkage, so I find it helpful to think of the
unnamed namespace as creating an area of internal linkage.
Unfortunately, I got tripped up by the exception to that
(technically incorrect) mental model.

The exception is that you can use members of an unnamed
namespace as template parameters. That means in general
(when the template implementation is separately compiled)
that the template parameters need external linkage.

There is one special additional rule (3.5 Program and linkage,
paragraph 4) that the name of a namespace declared within an
unnamed namespace does not have external linkage.

Martin was asking for the reason for that special rule, and
I don't know the answer to that.

--
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: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1998/09/02
Raw View
AllanW@my-dejanews.com writes:

> What would change is the first example, the one you posted. The
> second example, which I typed in, would remain the same.

You indicate (correctly) that linkage has only effect if you have two
translation units. So let's make a program with two translation units:

//File1.h
namespace {
  namespace ns{ extern int i;}
}

//File1.c
#include "File1.h"
namespace {
  namespace ns{
    int i;
}}

//File2.c
#include "File1.h"
void     void ns_i() { ++ns::i; }

int main(){}

This is wrong, whether ns has internal linkage or not: The namespace
ns in File1 is different from the one in File2, since they are in
different anonymous namespaces.

I still can't see why ns needs internal linkage.

Regards,
Martin
---
[ 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: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1998/09/02
Raw View
Martin von Loewis <loewis@informatik.hu-berlin.de> writes:

 >You indicate (correctly) that linkage has only effect if you have two
 >translation units. So let's make a program with two translation units:
 >
 >//File1.h
 >namespace {
 >  namespace ns{ extern int i;}
 >}
 >
 >//File1.c
 >#include "File1.h"
 >namespace {
 >  namespace ns{
 >    int i;
 >}}
 >
 >//File2.c
 >#include "File1.h"
 >void     void ns_i() { ++ns::i; }
 >
 >int main(){}
 >
 >This is wrong, whether ns has internal linkage or not: The namespace
 >ns in File1 is different from the one in File2, since they are in
 >different anonymous namespaces.
 >
 >I still can't see why ns needs internal linkage.

I don't think things inside anonymous namespaces *need* internal linkage.
Whether or not the standard says they has internal or external linkage
would be irrelevant, from a formal perspective, if there's no way you
can name them from another translation unit anyway.

So what's wrong with such entities having internal linkage?

Giving them internal linkage makes it clear that compilers do not
have to actually invent distinct names for unnamed namespaces.

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3        |     -- the last words of T. S. Garp.


[ 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: Jason Merrill <jason@cygnus.com>
Date: 1998/09/03
Raw View
>>>>> Fergus Henderson <fjh@cs.mu.OZ.AU> writes:

 > I don't think things inside anonymous namespaces *need* internal linkage.
 > Whether or not the standard says they has internal or external linkage
 > would be irrelevant, from a formal perspective, if there's no way you
 > can name them from another translation unit anyway.

 > So what's wrong with such entities having internal linkage?

Only names with external linkage can be used as template arguments.  That's
what the anonymous namespace is for.

Jason
---
[ 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: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1998/09/03
Raw View
fjh@cs.mu.OZ.AU (Fergus Henderson) writes:

> I don't think things inside anonymous namespaces *need* internal
> linkage.

Thanks for your response. The question then is, why does the standard
mandate internal linkage on namespaces inside unnamed namespaces,
whereas it defines normal linkage (i.e. external by default) for
everything else.

> So what's wrong with such entities having internal linkage?

Perhaps nothing. Perhaps, a compiler treating namespaces inside
unnamed namespaces is not compliant. This is my question:

Is it possible to observe somehow that a namespace inside an unnamed
namespace has internal linkage?

> Giving them internal linkage makes it clear that compilers do not
> have to actually invent distinct names for unnamed namespaces.

Sure. The compilers have that option for all objects with external
linkage in unnamed namespaces (*), so it would probably work for
namespaces as well.

Regards,
Martin

(*) except for objects with C linkage ...


[ 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: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/09/03
Raw View
Martin von Loewis <loewis@informatik.hu-berlin.de> writes:

>fjh@cs.mu.OZ.AU (Fergus Henderson) writes:

>> I don't think things inside anonymous namespaces *need* internal
>> linkage.

>Thanks for your response. The question then is, why does the standard
>mandate internal linkage on namespaces inside unnamed namespaces,
>whereas it defines normal linkage (i.e. external by default) for
>everything else.

The entire point and only purpose of an unnamed namespace is to
prevent the contents from having external linkage. No other
construct in the language provides all the capabilities of
the unnamed namespace. (Some kinds of declarations are
prohibited at block scope, and "static" applies only to
objects and functions, not to types or to member functions.)

You are never forced to use an unnamed namespace. If you want
something to have external linkage, don't put it in an
unnamed namespace.

--
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              ]