Re: Library / header compilation mismatch
On Jan 23, 1:42 am, red floyd <redfl...@gmail.com> wrote:
On Jan 22, 4:36 pm, "stephen.dive...@gmail.com"
<stephen.dive...@gmail.com> wrote:
On Jan 22, 1:53 am, James Kanze <james.ka...@gmail.com> wrote:
I don't do it for options (yet---it's a good idea for options
which affect binary compatibility), but I encode the version in
the namespace name. Something like:
#define PASTE(a,b) a ## b
#define MyNamespace PASTE( MyNamespace_, versionId )
namespace MyNamespace {
// ...
}
If you link against the wrong version, you get all of your
symbols undefined.
Ah, that's great, thank you! A namespace is a perfect
solution to the problem.
The only downside might be if clients want to use a debugger;
the debugger isn't going to find the symbol MyNamespace::toto.
Use an alias.
#define PASTE(a,b) a ## b
#define MyNamespace_ PASTE( MyNamespace_, versionId )
namespace MyNamespace_ {
// ...
}
namespace MyNamespace = MyNamespace_;
That's what I actually did---I wanted the user to only know a
single namespace name. In practice, however, you can't use the
alias in a namespace definition, and the debugger (at least g++)
doesn't know about it, so it really doesn't buy you much. I'm
still using it, because that's the way I started (and at least
with some compilers, like g++, the alias is what appears in
error messages---if you mispell a member, for example, g++ will
output "'xxx' is not a member of 'Alias'").
For what it's worth, gdb will do automatic completion, so if you
have something like:
#define VersionedMine PASTE(Mine_,version)
namespace VersionedMine {}
namespace Mine = VersionedMine ;
in a common header, you still have to define things in
namespace VersionedMine {
// ...
}
but client code can use Mine, and in gdb, if they enter
Mine<TAB>, gdb will complete it with whatever is appropriate.
It's not perfect, but it's a fairly usable.
BTW: I'm not the inventor of this idea. I think I got it from
Nathan Meyrs, but I'm not sure. But it's been around almost
since namespaces were introduced into the language.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34