Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6112 > unrolled thread

Anonymous namespace

Started byruben safir <ruben@mrbrklyn.com>
First post2011-06-02 21:53 -0400
Last post2011-06-27 14:29 -0400
Articles 20 on this page of 55 — 15 participants

Back to article view | Back to comp.lang.c++


Contents

  Anonymous namespace ruben safir <ruben@mrbrklyn.com> - 2011-06-02 21:53 -0400
    Re: Anonymous namespace Pete Becker <pete@versatilecoding.com> - 2011-06-02 16:00 -1000
    Re: Anonymous namespace ruben safir <ruben@mrbrklyn.com> - 2011-06-02 22:40 -0400
      Re: Anonymous namespace ruben safir <ruben@mrbrklyn.com> - 2011-06-03 02:29 -0400
        Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-03 20:12 +1200
          Re: Anonymous namespace Ruben Safir <mrbrklyn@panix.com> - 2011-06-04 02:40 +0000
            Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-04 15:40 +1200
              Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 04:56 +0000
                Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-04 17:19 +1200
                  Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:37 +0000
                    Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-04 17:40 +1200
          Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:05 +0000
            Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-04 17:31 +1200
              Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:42 +0000
        Re: Anonymous namespace Öö Tiib <ootiib@hot.ee> - 2011-06-03 06:08 -0700
          Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:09 +0000
            Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-04 17:32 +1200
              Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:39 +0000
                Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-04 17:45 +1200
      Re: Anonymous namespace gwowen <gwowen@gmail.com> - 2011-06-03 01:08 -0700
        Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-03 20:17 +1200
          Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:08 +0000
        Re: Anonymous namespace Victor Bazarov <v.bazarov@comcast.invalid> - 2011-06-03 09:44 -0400
          Re: Anonymous namespace Ruben Safir <mrbrklyn@panix.com> - 2011-06-04 02:44 +0000
          Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-04 15:57 +1200
          Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:14 +0000
            Re: Anonymous namespace "Bo Persson" <bop@gmb.dk> - 2011-06-04 11:33 +0200
    Re: Anonymous namespace Michael Doubez <michael.doubez@free.fr> - 2011-06-03 03:10 -0700
      Re: Anonymous namespace Ruben Safir <mrbrklyn@panix.com> - 2011-06-04 02:48 +0000
        Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-04 16:01 +1200
          Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:16 +0000
            Re: Anonymous namespace Ian Collins <ian-news@hotmail.com> - 2011-06-04 17:34 +1200
    Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-04 05:01 +0000
      Re: Anonymous namespace Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-09 11:26 +0000
        Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-10 04:12 +0000
          Re: Anonymous namespace Michael Doubez <michael.doubez@free.fr> - 2011-06-10 01:49 -0700
            Re: Anonymous namespace mrbrklyn <ceo.brooklyn@gmail.com> - 2011-06-10 10:34 -0700
            Re: Anonymous namespace mrbrklyn <ceo.brooklyn@gmail.com> - 2011-06-10 10:45 -0700
              Re: Anonymous namespace Michael Doubez <michael.doubez@free.fr> - 2011-06-12 00:01 -0700
                Re: Anonymous namespace ruben safir <ruben@mrbrklyn.com> - 2011-06-13 04:08 +0000
                  Re: Anonymous namespace Michael Doubez <michael.doubez@free.fr> - 2011-06-13 08:43 -0700
          Re: Anonymous namespace Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-10 18:27 +0000
            Re: Anonymous namespace ruben safir <ruben@mrbrklyn.com> - 2011-06-13 05:02 +0000
              Re: Anonymous namespace Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-13 07:58 +0000
                Re: "namespaces were a mistake" (was: Anonymous namespace) Öö Tiib <ootiib@hot.ee> - 2011-06-13 09:56 -0700
                Re: "namespaces were a mistake" (was: Anonymous namespace) Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-13 19:52 +0000
                  Re: "namespaces were a mistake" Miles Bader <miles@gnu.org> - 2011-06-14 13:43 +0900
                    Re: "namespaces were a mistake" Miles Bader <miles@gnu.org> - 2011-06-15 07:15 +0900
                      Re: "namespaces were a mistake" Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-15 12:01 +0000
                  Re: "namespaces were a mistake" (was: Anonymous namespace) drew@furrfu.invalid (Drew Lawson) - 2011-06-14 12:24 +0000
                    Re: "namespaces were a mistake" (was: Anonymous namespace) gwowen <gwowen@gmail.com> - 2011-06-14 06:00 -0700
                      Re: "namespaces were a mistake" (was: Anonymous namespace) Joshua Maurice <joshuamaurice@gmail.com> - 2011-06-14 14:10 -0700
                    Re: "namespaces were a mistake" Miles Bader <miles@gnu.org> - 2011-06-15 06:30 +0900
                Re: Anonymous namespace Ruben Safir <ruben@mrbrklyn.com> - 2011-06-27 16:34 +0000
                  Re: Anonymous namespace Victor Bazarov <v.bazarov@comcast.invalid> - 2011-06-27 14:29 -0400

Page 1 of 3  [1] 2 3  Next page →


#6112 — Anonymous namespace

Fromruben safir <ruben@mrbrklyn.com>
Date2011-06-02 21:53 -0400
SubjectAnonymous namespace
Message-ID<is9eqf$ft8$2@reader1.panix.com>
what is the anonymous name space and how is it accessed?

[toc] | [next] | [standalone]


#6113

FromPete Becker <pete@versatilecoding.com>
Date2011-06-02 16:00 -1000
Message-ID<2011060216002187959-pete@versatilecodingcom>
In reply to#6112
On 2011-06-02 15:53:19 -1000, ruben safir said:

> what is the anonymous name space and how is it accessed?

namespace {
int i;
}

int main() {
	i = 3;
	return 0;
}

Each translation unit has a separate anonymous namespace. Name lookup 
looks at names in the anonymous namespace, so there's nothing special 
you should do (or can do) to access it.

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

[toc] | [prev] | [next] | [standalone]


#6118

Fromruben safir <ruben@mrbrklyn.com>
Date2011-06-02 22:40 -0400
Message-ID<is9hj6$91p$1@reader1.panix.com>
In reply to#6112
On 06/02/2011 10:07 PM, Stefan Ram wrote:
> ruben safir<ruben@mrbrklyn.com>  writes:
>> what is the anonymous name space and how is it accessed?
>
>    When an unnamed namespace is defined as in
>    (»...« denotes the contents, not to be read literally)
>
> namespace { ... }
>
>    , this is the same as
>
> namespace unique {} using namespace unique; namespace unique
> { ... }
>
>    , where »unique« is a unique (think: »compiler generated,
>    secret«) identifier used only for this namespace.
>

Can the object in the namespace be accessed in main with ::

namespace{
	int i;
}

int main(int argc, char * argv[] ){
	::i = 20;
	std::cout << i << std::endl;

}

What is the point of the anonymous namespace.

Ruben

[toc] | [prev] | [next] | [standalone]


#6122

Fromruben safir <ruben@mrbrklyn.com>
Date2011-06-03 02:29 -0400
Message-ID<is9v15$gpd$1@reader1.panix.com>
In reply to#6118
On 06/02/2011 10:54 PM, Stefan Ram wrote:
>> >What is the point of the anonymous namespace.
>    Names can be hidden from the linker (from other translation
>    units), even if they should have external linkage.
>


please expand?

[toc] | [prev] | [next] | [standalone]


#6124

FromIan Collins <ian-news@hotmail.com>
Date2011-06-03 20:12 +1200
Message-ID<94rjavFbjiU7@mid.individual.net>
In reply to#6122
On 06/ 3/11 06:29 PM, ruben safir wrote:
> On 06/02/2011 10:54 PM, Stefan Ram wrote:
>>>> What is the point of the anonymous namespace.
>>     Names can be hidden from the linker (from other translation
>>     units), even if they should have external linkage.
>
> please expand?

One way to look at the anonymous namespace is to consider it a namespace 
with a cryptic name.  That name is only known within the current 
compilation unit.  Thus anything declared within the namespace can't 
been seen elsewhere because the namespace name is unknown.

So I could write

namespace { int n; }

and n would only be visible within the current compilation unit whereas

int n;

would be globally visible.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6163

FromRuben Safir <mrbrklyn@panix.com>
Date2011-06-04 02:40 +0000
Message-ID<isc5ug$b8n$1@reader1.panix.com>
In reply to#6124
Ian Collins <ian-news@hotmail.com> wrote:
> On 06/ 3/11 06:29 PM, ruben safir wrote:
>> On 06/02/2011 10:54 PM, Stefan Ram wrote:
>>>>> What is the point of the anonymous namespace.
>>>     Names can be hidden from the linker (from other translation
>>>     units), even if they should have external linkage.
>>
>> please expand?
> 
> One way to look at the anonymous namespace is to consider it a namespace 
> with a cryptic name.  That name is only known within the current 
> compilation unit.  Thus anything declared within the namespace can't 
> been seen elsewhere because the namespace name is unknown.
> 
> So I could write
> 
> namespace { int n; }
> 
> and n would only be visible within the current compilation unit whereas
> 
> int n;
> 
> would be globally visible.
> 

so the annonymous name space must be in the same file as main?

Ruben

[toc] | [prev] | [next] | [standalone]


#6166

FromIan Collins <ian-news@hotmail.com>
Date2011-06-04 15:40 +1200
Message-ID<94tnpvFivcU1@mid.individual.net>
In reply to#6163
On 06/ 4/11 02:40 PM, Ruben Safir wrote:
> Ian Collins<ian-news@hotmail.com>  wrote:
>> On 06/ 3/11 06:29 PM, ruben safir wrote:
>>> On 06/02/2011 10:54 PM, Stefan Ram wrote:
>>>>>> What is the point of the anonymous namespace.
>>>>      Names can be hidden from the linker (from other translation
>>>>      units), even if they should have external linkage.
>>>
>>> please expand?
>>
>> One way to look at the anonymous namespace is to consider it a namespace
>> with a cryptic name.  That name is only known within the current
>> compilation unit.  Thus anything declared within the namespace can't
>> been seen elsewhere because the namespace name is unknown.
>>
>> So I could write
>>
>> namespace { int n; }
>>
>> and n would only be visible within the current compilation unit whereas
>>
>> int n;
>>
>> would be globally visible.
>
> so the annonymous name space must be in the same file as main?

No, it can be an any compilation unit, that's one of the reasons for its 
existence.

I could have write "namespace { int n; }" in a dozen source files (or a 
header they include) and there would be a dozen unique instances of n.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6169

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-06-04 04:56 +0000
Message-ID<iscduf$keo$1@reader1.panix.com>
In reply to#6166
On Sat, 04 Jun 2011 15:40:46 +1200, Ian Collins wrote:


>> so the annonymous name space must be in the same file as main?
> 
> No, it can be an any compilation unit, that's one of the reasons for its
> existence.
> 
> I could have write "namespace { int n; }" in a dozen source files (or a
> header they include) and there would be a dozen unique instances of n.

Sorry for being thick about this, but such stuff is not covered in normal 
C++ text books.  It seems you have to learn it by word of mouth or maybe 
I need yet more books.

A single computational unit is  the files that are used to create any 
single object file?  That would be basically anything that would be 
#include (ed) in a source file?

Now what makes that interesting is that when I was studying templates, 
the declarations and definitions needed to be inside the same file.  How 
is that related (or the reason why it is not related)to the concept of a 
transitional unit?  And how does this affect the linking of shared 
libraries?

Ruben

[toc] | [prev] | [next] | [standalone]


#6176

FromIan Collins <ian-news@hotmail.com>
Date2011-06-04 17:19 +1200
Message-ID<94ttj9FivcU4@mid.individual.net>
In reply to#6169
On 06/ 4/11 04:56 PM, Ruben Safir wrote:
> On Sat, 04 Jun 2011 15:40:46 +1200, Ian Collins wrote:
>
>
>>> so the annonymous name space must be in the same file as main?
>>
>> No, it can be an any compilation unit, that's one of the reasons for its
>> existence.
>>
>> I could have write "namespace { int n; }" in a dozen source files (or a
>> header they include) and there would be a dozen unique instances of n.
>
> Sorry for being thick about this, but such stuff is not covered in normal
> C++ text books.  It seems you have to learn it by word of mouth or maybe
> I need yet more books.
>
> A single computational unit is  the files that are used to create any
> single object file?  That would be basically anything that would be
> #include (ed) in a source file?

"compilation unit", yes, pretty much.

> Now what makes that interesting is that when I was studying templates,
> the declarations and definitions needed to be inside the same file.  How
> is that related (or the reason why it is not related)to the concept of a
> transitional unit?  And how does this affect the linking of shared
> libraries?

Many, but not all compilers require the declaration and definition of a 
template to be in the same compilation unit.  It doesn't really affect 
the linking of libraries,  There are a couple of techniques used with 
libraries containing templates.  One is to keep all of the template code 
inline in a header, the other is to instantiate all possibilities in the 
library.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6181

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-06-04 05:37 +0000
Message-ID<iscgb5$keo$9@reader1.panix.com>
In reply to#6176
On Sat, 04 Jun 2011 17:19:37 +1200, Ian Collins wrote:

>  the other is to instantiate all possibilities in the
> library.

How can you do that?  You would need to know what all the class T's can 
be and in a language where you create types on the fly, you can't do that.

You can have limited predefined number of partial or explicit 
specifications for the general template.  Or I guess you can instantiate 
them, but I can't think of how that can be done except within a local 
namespace

vector<char *> a;

but then you have to call for a.



Ruben

[toc] | [prev] | [next] | [standalone]


#6184

FromIan Collins <ian-news@hotmail.com>
Date2011-06-04 17:40 +1200
Message-ID<94tupvFivcU8@mid.individual.net>
In reply to#6181
On 06/ 4/11 05:37 PM, Ruben Safir wrote:
> On Sat, 04 Jun 2011 17:19:37 +1200, Ian Collins wrote:
>
>>   the other is to instantiate all possibilities in the
>> library.
>
> How can you do that?  You would need to know what all the class T's can
> be and in a language where you create types on the fly, you can't do that.

Quite.  That's why it isn't done that often!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6171

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-06-04 05:05 +0000
Message-ID<iscefn$keo$3@reader1.panix.com>
In reply to#6124
On Fri, 03 Jun 2011 20:12:15 +1200, Ian Collins wrote:

>  
>>
>> please expand?
> 
> One way to look at the anonymous namespace is to consider it a namespace
> with a cryptic name.  That name is only known within the current
> compilation unit.  Thus anything declared within the namespace can't
> been seen elsewhere because the namespace name is unknown.
> 
> So I could write
> 
> namespace { int n; }
> 
> and n would only be visible within the current compilation unit whereas
> 
> int n;
> 
> would be globally visible.

Ok - I'm getting a clearer picture.  What specifically did you mean, 
however, by the phrase "even if they should have external linkage"

This is why I wanted a job with a lot of smart C++ coders.  I'd sweep the 
floor if need be.

Ruben.

[toc] | [prev] | [next] | [standalone]


#6178

FromIan Collins <ian-news@hotmail.com>
Date2011-06-04 17:31 +1200
Message-ID<94tu9qFivcU5@mid.individual.net>
In reply to#6171
On 06/ 4/11 05:05 PM, Ruben Safir wrote:
> On Fri, 03 Jun 2011 20:12:15 +1200, Ian Collins wrote:
>
>>
>>>
>>> please expand?
>>
>> One way to look at the anonymous namespace is to consider it a namespace
>> with a cryptic name.  That name is only known within the current
>> compilation unit.  Thus anything declared within the namespace can't
>> been seen elsewhere because the namespace name is unknown.
>>
>> So I could write
>>
>> namespace { int n; }
>>
>> and n would only be visible within the current compilation unit whereas
>>
>> int n;
>>
>> would be globally visible.
>
> Ok - I'm getting a clearer picture.  What specifically did you mean,
> however, by the phrase "even if they should have external linkage"

Not declared static.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6185

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-06-04 05:42 +0000
Message-ID<iscgk3$keo$11@reader1.panix.com>
In reply to#6178
On Sat, 04 Jun 2011 17:31:37 +1200, Ian Collins wrote:


>> Ok - I'm getting a clearer picture.  What specifically did you mean,
>> however, by the phrase "even if they should have external linkage"
> 
> Not declared static.
> 
> --


ah - Thank YOU!  I understand better.  the haze is lifting.

I used to have a physics teach, Dr Wolf, who used to get questions from 
confused students that to me, sounded like complete gibberish.  He was 
able to parse them and give them the answers they needed, to questions i 
don't think they even understood.

Ruben

[toc] | [prev] | [next] | [standalone]


#6135

FromÖö Tiib <ootiib@hot.ee>
Date2011-06-03 06:08 -0700
Message-ID<d9697582-1886-4c69-9242-9effcf49e8b4@hd10g2000vbb.googlegroups.com>
In reply to#6122
On Jun 3, 9:29 am, ruben safir <ru...@mrbrklyn.com> wrote:
> On 06/02/2011 10:54 PM, Stefan Ram wrote:
>
> >> >What is the point of the anonymous namespace.
> >    Names can be hidden from the linker (from other translation
> >    units), even if they should have external linkage.
>
> please expand?

The code like:

 namespace
 {
     int i;

     void foo() {/*...*/}
 }

Means that i and foo() are translation unit local.
It was meant to replace code like:

 static int i;
 static void foo() {/*...*/}

That is because you can not declare types as static in C++. Lot of
things in C++ are done using types. You might still want to have
translation unit local types and nameless namespace helps there.

To keep the style consistent the  usage of the 'static' keyword in
sense of 'translation unit local' was even deprecated by C++03 but i
trust it will be not deprecated by C++11 again. C++ does not want to
enforce style upon its users.

[toc] | [prev] | [next] | [standalone]


#6173

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-06-04 05:09 +0000
Message-ID<isceli$keo$5@reader1.panix.com>
In reply to#6135
On Fri, 03 Jun 2011 06:08:42 -0700, Öö Tiib wrote:

> That is because you can not declare types as static in C++.

Yes - I've seen the compiler through that error are me.  Why?

[toc] | [prev] | [next] | [standalone]


#6179

FromIan Collins <ian-news@hotmail.com>
Date2011-06-04 17:32 +1200
Message-ID<94tubmFivdU1@mid.individual.net>
In reply to#6173
On 06/ 4/11 05:09 PM, Ruben Safir wrote:
> On Fri, 03 Jun 2011 06:08:42 -0700, Öö Tiib wrote:
>
>> That is because you can not declare types as static in C++.
>
> Yes - I've seen the compiler through that error are me.  Why?

Because you can't, the language doesn't allow it.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6183

FromRuben Safir <ruben@mrbrklyn.com>
Date2011-06-04 05:39 +0000
Message-ID<iscgec$keo$10@reader1.panix.com>
In reply to#6179
On Sat, 04 Jun 2011 17:32:38 +1200, Ian Collins wrote:


>> Yes - I've seen the compiler through that error are me.  Why?
> 
> Because you can't, the language doesn't allow it.

:)

I thought I'd get that answer. :)  Rephrase.  Why did the C++ designers 
chose to prevent static types.

[toc] | [prev] | [next] | [standalone]


#6187

FromIan Collins <ian-news@hotmail.com>
Date2011-06-04 17:45 +1200
Message-ID<94tv3sFivcU9@mid.individual.net>
In reply to#6183
On 06/ 4/11 05:39 PM, Ruben Safir wrote:
> On Sat, 04 Jun 2011 17:32:38 +1200, Ian Collins wrote:
>
>
>>> Yes - I've seen the compiler through that error are me.  Why?
>>
>> Because you can't, the language doesn't allow it.
>
> :)
>
> I thought I'd get that answer. :)  Rephrase.  Why did the C++ designers
> chose to prevent static types.

I guess because C doesn't have them.  Also types don't have any linkage. 
  Linkage applies to instances of types.  The language rules state that 
if a class is declared in more than one compilation unit, the 
definitions must be identical.  The anonymous namespace works around 
that restriction.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#6123

Fromgwowen <gwowen@gmail.com>
Date2011-06-03 01:08 -0700
Message-ID<03464db0-e118-463c-9834-171dab457e42@h9g2000yqk.googlegroups.com>
In reply to#6118
On Jun 3, 3:54 am, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
> >What is the point of the anonymous namespace.
>
>   Names can be hidden from the linker (from other translation
>   units), even if they should have external linkage.

Can someone give the benefits of external-but-unlinkable anon-
namespace variables (or functions) against old-school internal linkage
file-scope "static" variables and functions.

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c++


csiph-web