[Lumiera] namespaces and other horrors (was time::Control Errors)

Ichthyostega prg at ichthyostega.de
Sun Oct 23 16:35:22 CEST 2011


Am 23.10.2011 00:49, schrieb Hendrik Boom:
> It's not newbie crap, either.  It's intricate stuff.

>> using std::tr1::ref

> Badly needed feature.  'using' wasn't there when I was intimately involved
> with C++ twenty years ago.  I'm guessing that it declares what namespace to
> use to resolve a naked 'ref'.

Hi Hendrik,

yes exactly, that's the purpose.
Technically speaking, it adds the symbol 'ref' to the known symbols of
the current namespace. For example, this allows to build an interface
namespace, where you introduce only those symbols needed by the users
of your library.

Another very useful (but at times dangerous) feature is "ADL"
(Argument Dependant Lookup). It was introduced only shortly before
year 2000 and only the recent compilers (e.g GCC since 4.x)
implement it properly.

ADL supports writing custom types. When you put a helper function
alongside of the custom type into some implementation namespace,
it will be found without explicit qualification, but only, when
the argument is your custom type.

Classical example:

namespace boo {

	using std::string;

	class Horror
	  {
            string innards;
		...
	  };

	operator string (Horror const& h) { return inards; }
}


namespace innocuous {
	
	using std::string;
	using std::cout;
	using std::endl;
	using boo::Horror;

	Horror horrible;

	// print the innards
	cout << string (horrible) << endl;
}

Here, when building the std::string instance from our horrible
object, the custom string conversion operator from namespace boo
will be picked, without the need of any further qualification.

This is pretty much the recommended standard way for a custom class
to provide a diagnostics output facility, or some kind of external
representation. In Python, you'd be providing a __repr__(self).
In Java this would be achieved by overriding the toString() method
of class Object. But fortunately C++ doesn't have a class Object.


Cheers,
Hermann



More information about the Lumiera mailing list