"CuriousGeorge" <brettrobichaud@newsgroup.nospam> wrote
So for the purpose of name lookup and hiding during
compilation it should be possible to wrap everything
into a namespace.
VC++ still doesn't get the right, but this should hardly be an
issue in such a simple case.
Syntactically how would I go about this? I have tried just creating a
single header that includes all of this libraries headers within a single
namespace. I then try calling one of the functions through the
namespace, but the compiler tells me that functions I am calling using
the namespace::function syntax are not members of the namespace.
That's what I would do:
// wrapper.h
# ifndef WRAPPER_H_INCLUDED
# define WRAPPER_H_INCLUDED
namespace N {
#include "their-header-1"
#include "their-header-2"
} // namespace N
#endif // WRAPPER_H_INCLUDED
So I started thinking about this and I realized that namespaces are a
compiler concept, and since I only have libs and headers for the third
party it doesn't seem like there is any way to enclose the actual
functions in a namespace. Am I off base here?
Namespaces are obviously used for name lookup. As Igor pointed
out the fully qualified names contribute to the mangled name.
However, functions with C-linkage are special. I.e.:
extern "C" {
void foo(); namespace N { void foo(); }
namespace A { namespace B { namespace C { void foo(); }}}
} // extern "C"
all refer to the same function. Without extern "C" they would
all be distinct functions. Now since ::foo refers to a
C function - so does N::foo.
That's why everything should work just fine short of
preprocessor badness that C headers tend to have.
Macros processing is done before the compiler pass
which does name lookup.
Anyway, say you have some third-part headers like
#ifndef /* include guard */
#define ..
#ifdef __cplusplus
extern "C" {
#endif
struct bar { int i; };
void foo( struct bar* );
...
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* include guard */
and you wrap that in your own header by opening a
namespace before and closing it after all third-party
includes you get the following logical part of the
translation unit:
...
namespace N {
extern "C" {
struct bar { int i; };
void foo( struct bar* };
} // extern "C"
} // N
You can perfectly use that:
int main() {
N::bar b; b.i = 1; // everything generated by the compiler
// fully-qualified name doesn't matter
N::foo(&b); // emits a call to "_foo" because of C-linkage
}
If you have any problems, I would run things through the
preprocessor to see what happens there. Of course,
if you can't figure it out we'd need to see some of the
code and the full diagnostics.
-hg
Yeah and this is exactly what I'm seeing. If I wrap their headers in a
signature. I guess I wasn't clear in that I not only want to wrap the third
that are in the global namespace that have the same name and signature.
I'm now seeing this isn't really possible.