Topic: Defining functions declared in an unnamed namespace


Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/01/29
Raw View
Matt Seitz writes:

> How does one define a function that is declared in an unnamed
> namespace?

It must be declared inside the body of the unnamed namespace, as you
cannot qualify references to members of such namespace.

> For example, I tried the following:

>         namespace {
>                 void f();
>         }

>         void g(){
>                 f();
>         }

>         void f(){
>                 /* Do foo things */
>         }

The definition of void f() should have produced an error message, as
it conflicts with <unnamed namespace>::f()

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: John Lilley <jlilley@empathy.com>
Date: 1997/01/29
Raw View
Matt Seitz wrote:
> How does one define a function that is declared in an unnamed
> namespace?  For example, I tried the following:
>    namespace { void f(); }
>    void g(){ f(); }
>    void f() {...}

IMHO, you don't.  The construct:

    namespace { ... stuff... }

is equivalent to

    namespace  __uniqueName { }
    using namespace __uniqueName;
    namespace __uniqueName { ... stuff ... }

The "using" directive does not cause subsequent declarations or
definitions to be created in the namespace.  In order to define the f()
in the unnamed namespace, you would have to use a scope-override:

    void __unqiueName::foo() {...}

But you cannot because __uniqueName is inaccessible by definition.

john lilley
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: mseitz@meridian-data.com (Matt Seitz)
Date: 1997/01/29
Raw View
How does one define a function that is declared in an unnamed namespace?  For
example, I tried the following:

        namespace {
                void f();
        }

        void g(){
                f();
        }

        void f(){
                /* Do foo things */
        }


However, my linker complained that "'anonymous namespace'::f()" was not defined.
 Am I defining f() incorrectly, or is this a compiler or linker bug?


[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]