Re: Placing functions from .lib in a namespace?

From: Alf P. Steinbach (alfps_at_start.no)
Date: 01/11/04


Date: Sun, 11 Jan 2004 19:12:52 +0100


Pete Becker skrev i meldingen <40017000.A5ABA0ED@acm.org>...
>Jeff Schwab wrote:
>>
>>
>> I've usually gotten away with this:
>>
>> namespace Acme
>> {
>> #include "acme_headers.h"
>> }
>>
>> Acme::func( );
>>
>> If that doesn't work, try:
>>
>> namespace Acme
>> {
>> #include "acme_headers.h"
>> }
>>
>> using namespace Acme;
>>
>> func( );
>>
>> It's just a band-aid, of course, but at least it's explicit.
>
>Can't work. Acme::func is a different function from ::func. The library
>defines 'extern "C" func", so calls to Acme::func won't link.

Not necessarily. I participated in a discussion about this many
years ago, and just as an investigation wrote some simple applications
using the Windows API placed in a namespace. Linking wasn't a problem,
but all the macro definitions in the header files were...

In short it's a maintainance nightmare, even where it can be made
to work.

And for header files of the size of the Windows API, say, there's no
practical way to put everything in a namespace at once; instead, if
one chooses to do this thing it's necessary to add more and more (e.g.
macro workarounds) as the need arises.

>Even if you can recompile the library this probably won't work: the
>behavior of a program that includes any standard headers inside a
>namespace declaration is undefined, and chances are good that
>"acme_headers.h" will do just that.

Yep.

One non-standard workaround (with formally undefined effect) is to
include the basic C library headers before including the library
header(s). For the Windows API using one particular vendor's headers
that turned out to be just a handful of C library headers. I think it
was four.

Not that I recommend the approach, of course.



Relevant Pages