POV-Ray : Newsgroups : povray.beta-test : Beta 37 and C++0x : Re: Beta 37 and C++0x Server Time
7 Jul 2024 11:12:04 EDT (-0400)
  Re: Beta 37 and C++0x  
From: Warp
Date: 3 Jun 2010 14:48:11
Message: <4c07f8eb@news.povray.org>
Edouard <pov### [at] edouardinfo> wrote:
> The point of namespaces is to give you tools to *manage* namespaces. If all you
> wanted was a way of naming things differently, then C++, pre-namespace, already
> gave you ways of doing that.

> one example being:

> struct std { class cout { ... }; class endl { ... }  };
> std::cout << blah() << std::endl;

  That would be incorrect. Using a 'struct' instead of a 'namespace' means
that you have to declare all the elements inside the one and same block.
You cannot divide the declarations into separate files, each one declaring
elements inside the same 'std' struct.

  Hence the standard library would be impossible to declare as a struct,
unless you want *everything* to be in one single header file. Given how much
template code there is in the C++ standard library, this one header file
would probably be several megabytes in size and take a significant amount
of time to parse.

> Namespaces are more than just another scoping qualifier. The whole point of
> namespaces is that they give you access to the using statement (with differing
> levels granularity, and with aliasing).

  No. The whole point of namespaces is to add scoping and help remove names
from the global namespace. 'using' (with respect to namespaces) is just a
convenience feature which is more or less irrelevant. It doesn't really add
anything to the language. It could well be left out and little would change
in terms of functionality and usability. (Heck, IMO it would be a big-ass
*improvement* if it was removed from the standard.)

> If all you use namespaces for is to add
> an additional scoping qualifier, that you then use in full everywhere in your
> code, than you're not really using them properly.

  And what, in your opinion, is the "proper" way of using namespaces?

  A "using namespace" statement more or less makes the whole namespace
feature moot. If you are using that statement, you might just as well not
use any namespace to begin with. What's the point? I cannot comprehend how
declaring something in a named namespace and then immediately popping it to
the global namespace would be the "proper" way of using namespaces. That's
not using namespaces at all.

  And as I have commented in another post, namespace prefixes actually make
the code *easier* to read and understand, not harder. It makes the code
significantly less ambiguous, and the prefix syntax is a clear visual clue
that something in a different scope is being used, and thus you don't have
to guess where that name might be defined and what its scope might be.

  Popping a single name with eg. "using std::something;" is slightly better
because at least you are kind-of documenting that "when you see 'something'
in the code below, it's actually 'something' from the std::namespace". Doing
it like that also lessens the danger of naming collisions (because you are
popping the name out of one specific namespace). However, "using namespace
std;" is just nonsensical.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.