Topic: Proposal for the Anonymous Namespace


Author: spope33@speedymail.org (Steve Pope)
Date: Sat, 2 Sep 2006 13:20:20 GMT
Raw View
Tom <tom.browder@gmail.com> wrote:

>In a given translation unit I would like to be able to refer to the
>anonymous namespace by name (e.g., "anon") so I can do this:

>namespace {
>  int foo1 = 3;
>  int foo(); // declare anonymous function foo()
>}

>// define anonymous function foo()
>int
>anon::foo()
>{
>  //...
>}

You can do what you're trying to do as follows:

namespace {
struct anon {
   static int foo();
   };
}

Steve

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: "John Moeller" <fishcorn@gmail.com>
Date: Sat, 2 Sep 2006 16:08:03 CST
Raw View
Steve Pope wrote:
> Tom <tom.browder@gmail.com> wrote:
>
> >In a given translation unit I would like to be able to refer to the
> >anonymous namespace by name (e.g., "anon") so I can do this:
>
> >namespace {
> >  int foo1 = 3;
> >  int foo(); // declare anonymous function foo()
> >}
>
> >// define anonymous function foo()
> >int
> >anon::foo()
> >{
> >  //...
> >}
>
> You can do what you're trying to do as follows:
>
> namespace {
> struct anon {
>    static int foo();
>    };
> }

You can also just declare it in the anonymous namespace, call the
function, then define it farther down in the file simply by putting the
definition in the anonymous namespace as well:

namespace {
  // declare foo():
  int foo();
}

int main(int argc, char *argv[])
{
  // call foo():
  return foo();
}

namespace {
  // define foo():
  int foo() {
    return 0;
  }
}

I do this kind of splitting all the time, and AFAIK, it's
standards-conforming.  It links on two different compilers for my
platform.  No tricks, just simple, and you don't need a change to the
standard because it already exists in the standard (I'm sure the gurus
will correct me if I'm wrong).

IIRC, this works because the anonymous namespace has the same generated
name throughout the whole compilation unit.

John Moeller

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Tom" <tom.browder@gmail.com>
Date: Mon, 24 Jul 2006 11:02:00 CST
Raw View
In a given translation unit I would like to be able to refer to the
anonymous namespace by name (e.g., "anon") so I can do this:

namespace {
  int foo1 = 3;
  int foo(); // declare anonymous function foo()
}


.
// define anonymous function foo()
int
anon::foo()
{
  //...
}

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Richard Smith" <richard@ex-parrot.com>
Date: Mon, 24 Jul 2006 13:20:32 CST
Raw View
Tom wrote:
> In a given translation unit I would like to be able to refer to the
> anonymous namespace by name (e.g., "anon") so I can do this:

Well, if it has a name, it's not anonymous, is it?  Seriously,
though...

> namespace {
>   int foo1 = 3;
>   int foo(); // declare anonymous function foo()
> }
> int anon::foo() {}

If you want to give it a name, why not just do this:

  namespace anon {
    namespace {
      int foo();
  }}
  int anon::foo() {}

It has the effect of preventing link-time conflicts between translation
units that the anonymous namespace provides, but also allows you to use
the name "anon" to refer to its contents.

One question, though: why do you want to do this?

--
Richard Smith

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Tom" <tom.browder@gmail.com>
Date: Mon, 24 Jul 2006 17:56:29 CST
Raw View
Richard Smith wrote:
> If you want to give it a name, why not just do this:
>
>   namespace anon {
>     namespace {
>       int foo();
>   }}
>   int anon::foo() {}

Hm, I believe the compiler will complain.  You are still referring to
foo() which hasn't been defined in the anonymous namespace.

> One question, though: why do you want to do this?

Well, frankly, so I can delay definitions of anonymous functions til
later in the file and not have to use the curly braces for the
namespace again (I don't like the extra indentation my editor adds for
long stretches of code inside the braces).  I admit, my rationale is
weak.

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: gdr@cs.tamu.edu (Gabriel Dos Reis)
Date: Tue, 25 Jul 2006 05:29:31 GMT
Raw View
On Mon, 24 Jul 2006, Richard Smith wrote:

| > namespace {
| >   int foo1 = 3;
| >   int foo(); // declare anonymous function foo()
| > }
| > int anon::foo() {}
|
| If you want to give it a name, why not just do this:
|
|   namespace anon {
|     namespace {
|       int foo();
|   }}
|   int anon::foo() {}

because that is not valid C++.

--
                                                        Gabriel Dos Reis
                                                         gdr@cs.tamu.edu
 Texas A&M University -- Department of Computer Science
 301, Bright Building -- College Station, TX 77843-3112

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]