Topic: unnamed namespace


Author: comeau@panix.com (Greg Comeau)
Date: 2000/04/02
Raw View
In article <5u57XJA5iN24Ew8i@robinton.demon.co.uk> Francis Glassborow <francisG@robinton.demon.co.uk> writes:
>Now I
>am finding myself wondering about an unnamed namespace encapsulated in a
>named namespace; is that always reopenable in the scope of the
>encapsulating namespace?

Given that we are told:
|An unnamed-namespace-definition behaves as if it were replaced by
|
|namespace unique { /* empty body */ }
|using namespace unique;
|namespace unique { namespace-body }
|
|where all occurrences of unique in a translation unit are replaced
|by the same identifier and this identifier differs from all other
|identifiers in the entire program

Then it would seem that:

namespace xyz {
    namespace {
        void foo();
    };
    namespace {
        void foo() { }
    }
}

becomes:

namespace xyz {
    namespace UNIQUE { }
    using namespace UNIQUE;
    namespace UNIQUE {
        void foo(); // ::xyz::UNIQUE::foo
    }
    namespace UNIQUE { }
    using namespace UNIQUE;
    namespace UNIQUE {
        void foo() { } // ::xyz::UNIQUE::foo
    }
}

- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.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: tamlin@NOSPAM.algonet.se (Mike Nordell)
Date: 2000/03/22
Raw View
Is it possible to define a function in an unnamed namespace, close
this namespace, reopen it, and finally define the function? Example:

#include <cstdio>
namespace { void foo(); }
int main()
{
 foo();
 return 0;
}
namespace { void foo() { printf("Hello\n"); } }

What I've read from the standard, this can't be done, but the
(non-conforming VC6) compiler I'm using allows this. Compiler-bug or
defined behaviour?

--
Mike
X-No-Reject-Notice

---
[ 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: Roger Orr <roger_orr@my-deja.com>
Date: 2000/03/23
Raw View
In article <38644e1a.13212879@news.algonet.se>,
tamlin@NOSPAM.algonet.se (Mike Nordell) wrote:
> Is it possible to define a function in an unnamed namespace, close
> this namespace, reopen it, and finally define the function? Example:
>
> #include <cstdio>
> namespace { void foo(); }
> int main()
> {
> foo();
> return 0;
> }
> namespace { void foo() { printf("Hello\n"); } }
>
> What I've read from the standard, this can't be done, but the
> (non-conforming VC6) compiler I'm using allows this. Compiler-bug or
> defined behaviour?
>
> --
> Mike
What have you read in the standard which says this can't be done?

AFAIK this should be perfectly valid C++

Roger Orr


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/03/23
Raw View
In article <38644e1a.13212879@news.algonet.se>, Mike Nordell
<tamlin@NOSPAM.algonet.se> writes
>Is it possible to define a function in an unnamed namespace, close
>this namespace, reopen it, and finally define the function? Example:
>
>#include <cstdio>
>namespace { void foo(); }
>int main()
>{
>       foo();
>       return 0;
>}
>namespace { void foo() { printf("Hello\n"); } }
>
>What I've read from the standard, this can't be done, but the
>(non-conforming VC6) compiler I'm using allows this. Compiler-bug or
>defined behaviour?

To the best of my knowledge VC6 is quite correct.  What you cannot do is
to re-open an unnamed namespace in a different translation unit.  Now I
am finding myself wondering about an unnamed namespace encapsulated in a
named namespace; is that always reopenable in the scope of the
encapsulating namespace?


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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: Darin Adler <darin@bentspoon.com>
Date: 2000/03/23
Raw View
In article <38644e1a.13212879@news.algonet.se>,
tamlin@NOSPAM.algonet.se (Mike Nordell) wrote:

> Is it possible to define a function in an unnamed namespace, close
> this namespace, reopen it, and finally define the function? Example:
>
> #include <cstdio>
> namespace { void foo(); }
> int main()
> {
>  foo();
>  return 0;
> }
> namespace { void foo() { printf("Hello\n"); } }

This is legal, except that "std::" is missing in front of your call to
printf inside the definition of foo.

All unnamed namespaces within a single translation unit are the same
namespace as per 7.3.1.1/1.

> What I've read from the standard, this can't be done, but the
> (non-conforming VC6) compiler I'm using allows this. Compiler-bug or
> defined behaviour?

What did you read in the standard that led you to believe it can't be
done? Perhaps I missed something.

    -- Darin

---
[ 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: 2000/03/23
Raw View
tamlin@NOSPAM.algonet.se (Mike Nordell) writes:

> Is it possible to define a function in an unnamed namespace, close
> this namespace, reopen it, and finally define the function?

It sure is allowed.

>
> #include <cstdio>
> namespace { void foo(); }
> int main()
> {
>  foo();
>  return 0;
> }
> namespace { void foo() { printf("Hello\n"); } }
>
> What I've read from the standard, this can't be done, but the
> (non-conforming VC6) compiler I'm using allows this. Compiler-bug or
> defined behaviour?

You must have mis-interpreted the standard. When they say 'namespace
unique{', they mean 'where all occurrences of unique in a translation
unit are replaced by the same identifier and this identifier differs
from all other identifiers in the entire program.'

So this program is equivalent to

namespace unique{}
using namespace unique;
namespace unique{void foo();}

int main(){...}
namespace unique{}
using namespace unique;
namespace unique{void foo(){...}}

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: Michael Rubenstein <miker3@ix.netcom.com>
Date: 2000/03/23
Raw View
On Wed, 22 Mar 2000 23:57:47 CST, tamlin@NOSPAM.algonet.se (Mike
Nordell) wrote:

>Is it possible to define a function in an unnamed namespace, close
>this namespace, reopen it, and finally define the function? Example:
>
>#include <cstdio>
>namespace { void foo(); }
>int main()
>{
> foo();
> return 0;
>}
>namespace { void foo() { printf("Hello\n"); } }
>
>What I've read from the standard, this can't be done, but the
>(non-conforming VC6) compiler I'm using allows this. Compiler-bug or
>defined behaviour?

Why should this be illegal?  From 7.3.1.1 all unnamed namespace
definitions in a compilation unit refer to the same namespace.

---
[ 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: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/03/23
Raw View
7.3 says:
A namespace is on optionally-named declarative region.
Hope it helps.

With regards,
Michael Kochetkov

Mike Nordell <tamlin@NOSPAM.algonet.se> wrote in message
news:38644e1a.13212879@news.algonet.se...
> Is it possible to define a function in an unnamed namespace, close
> this namespace, reopen it, and finally define the function? Example:
>
> #include <cstdio>
> namespace { void foo(); }
> int main()
> {
> foo();
> return 0;
> }
> namespace { void foo() { printf("Hello\n"); } }
>
> What I've read from the standard, this can't be done, but the
> (non-conforming VC6) compiler I'm using allows this. Compiler-bug or
> defined behaviour?
>
> --
> Mike
> X-No-Reject-Notice
>
> ---
> [ 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              ]
>


---
[ 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: "Matt Seitz" <mseitz@meridian-data.com>
Date: 1998/06/17
Raw View
Matt Seitz wrote in message <3586a966.0@newsread.exodus.net>...
>Based on this article, I believe the solution is:
>namespace { void dummy();} //declaration
>
>//Insert your program here
>namespace{
>  void
>y(){
>    //definition
>  }
>}

That should have read:

namespace { void dummy();} //declaration

//Insert your program here

namespace{
    void dummy(){
        //definition
    }
}
---
[ 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              ]