Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to wrap an #include in a namespace block?

I have a C header that was written to compile as both C and C++ (it only uses features from the common subset, and uses that extern "C" thing).

Problem is, that header declares stuff in the global namespace. I'd rather avoid that for the usual reasons. I thought about doing this:

namespace foo {
#include <foo.h>
}

Is doing this a good idea? Do I have alternatives that don't include editing the header file?

like image 902
R. Martinho Fernandes Avatar asked Jul 12 '11 20:07

R. Martinho Fernandes


People also ask

Is wrapping a good idea?

Are car wraps more durable than paint? Vehicles wraps are generally more durable than paint. While a top-tier paint job can last the life of a vehicle, standard paint jobs typically only last a couple of years. A high-quality vehicle wrap will last up to 10 years.

Do wraps damage your paint?

That's the beauty of a car wrap. Although they've been around for quite some time, many vehicle owners often ask us, does a car wrap damage paint? The truth is that a vinyl wrap or car wrap as some call it, is completely safe to be applied to any type of paint – whether it's gloss or a matte finish.

Is wrapping or spraying better?

But most of the time, car wrapping is cheaper. When you spray paint a car, it's a permanent modification and the process is almost impossible to undo. Depending on the car model and intricacy of work, it will minimally cost $1,000 to over $10,000. However, car wrapping is a lot cheaper, quicker and easily removed.

Do wraps hide scratches?

While car wraps can be applied over scratches, they will not hide scratches. In fact, a vinyl wrap may even make the imperfections in your vehicle's paint even more noticeable.


2 Answers

No, it’s a bad idea. With C++ declarations, it's likely to introduce linker errors as identifiers get declared in the wrong namespace. With C declarations, it works, but it may hide clashes between identifiers in the global namespace (which you were trying to avoid, I guess) until link time; it doesn't really put the identifiers in a namespace.

A better idea would be to put your own identifiers in a namespace and avoid defining anything but main in the global one.

like image 83
Fred Foo Avatar answered Sep 21 '22 10:09

Fred Foo


I did this kind of "place it in a namespace" for <windows.h> in the late 1990's.

Although not with complete support: it was on the principle of adding support for whatever next I needed when I needed it.

The key to make that work was to check which C library headers were included, and make sure to include them first. It boiled down to 4 such headers, IIRC. Microsoft's love of macros made things difficult, though.

So it can be done in practice for C headers (or C++ restricted to the C-like subset), but at cost of updating your wrapper for every new version of the wrappee, Which is impractical and/or very costly. Not to mention laborious.

In conclusion, no, it's not a good idea. :-)

Speaking from experience.

like image 42
Cheers and hth. - Alf Avatar answered Sep 18 '22 10:09

Cheers and hth. - Alf