namespaces with static linkage?

namespaces with static linkage?

Post by Martin von Loewi » Tue, 01 Sep 1998 04:00:00



Section 3.5, [basic.link]/4, says

Quote:>> 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{
  }

Quote:}

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 ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

namespaces with static linkage?

Post by Martin von Loewi » Thu, 03 Sep 1998 04:00:00



> 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;}

Quote:}

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

Quote:}}

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

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

namespaces with static linkage?

Post by Fergus Henders » Thu, 03 Sep 1998 04:00:00



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

--

WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

namespaces with static linkage?

Post by Jason Merril » Fri, 04 Sep 1998 04:00:00


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

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

namespaces with static linkage?

Post by Martin von Loewi » Fri, 04 Sep 1998 04:00:00



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

Quote:> 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?

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

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

namespaces with static linkage?

Post by Steve Clama » Fri, 04 Sep 1998 04:00:00




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

--

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

namespaces with static linkage?

Post by Steve Clama » Sun, 06 Sep 1998 04:00:00





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

--

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

namespaces with static linkage?

Post by David R Tribbl » Thu, 10 Sep 1998 04:00:00



> 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)?


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

namespaces with static linkage?

Post by Jason Merril » Fri, 11 Sep 1998 04:00:00


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

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

namespaces with static linkage?

Post by Christopher Eltschk » Fri, 25 Sep 1998 04:00:00




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

[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

 
 
 

1. Anonymous namespaces and static linkage

I was wondering what the difference between the following
C++ constructs was.

namespace {
  bool my_function()
  {
    return true;
  }

and

static bool my_function()
{
  return true;

Do both have the same linkage? Can the address of either
function be passed back as a return value by another
function (with external linkage) within the same translation
unit ?


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. web site pictures

3. Namespace and Linkage

4. bouncing mail

5. DLL linkage error of static method

6. Server Timed Out

7. static linkage?

8. IExtractIcon...

9. How to define (not declare) a static constant with external linkage?

10. Solving the member function static linkage problem, how?

11. CC static internal linkage is changed to global after "-g0" - debug turned on

12. Static initializers under C linkage

13. local static variables and linkage