Keith said:
One more question. In C, if two libraries use the same identifier,
it's difficult or impossible for a program to use both libraries.
Using a library-specific prefix can avoid this, but if two libraries
use the same prefix you still have the same problem.
Suppose two C++ libraries use the same namespace identifier, say
"foo". Is it possible to use both libraries by aliasing one or both
of the namespaces? For example, could the program refer to one
namespace as "foo1" and the other as "foo2"?
No. Those namespace aliases are just that, just as a typedef is just an
alias and doesn't change the symbol emitted to the linker (with the
exception of typedef'ing an anonymous type).
Ian, your solution doesn't work, it changes the declarations found in a
header, but it still doesn't change the symbols exported from a library.
No, I think in order to shoehorn two conflicting two libs into one program
one needs to resort to things like changing the linker symbols manually or
writing a wrapper lib that consists of a bunch of forwarding functions
with just a different name.
Without such a feature, namespaces are useful, but all they really do
is encourage some structure in naming and allow abbreviations in some
cases; the don't give you anything you couldn't do in C with some
extra work. With such a feature, they would solve a problem that
can't really be solved in C, which could be an argument (here's where
it becomes marginally topical) for adding C++-style namespaces to a
future C standard.
I'm not sure that otherwise they wouldn't be worthwhile adding. Point is
that they could be added without breaking any existing code, they are
definitely no runtime overhead, because just a different syntax, and I
think they would indeed solve problems for which there is no solution:
Think about for example the Apache portable runtime library. If it had
used apache_runtime (let's just drop the portable) as prefix, everyone
would have to write
apache_runtime_file* f = apache_runtime_open("fou.texte");
Tedious. Very. That's the reason they chose apr_ as a prefix, which is much
shorter but then again bears the danger of conflicting. Using namespaces,
you could alias namespace apache:
ortable_runtime to just apr when using
it.
Uli