开发者

How to deal with daft header files

I'm trying to use a third party SDK in some C++ code. While the SDK and headers are t开发者_C百科echnically compatible with C++, it's really just a lump of nasty C.

In particular the main header files has many hundreds of #defines of which these are the worst.

#define     C  0       //Celsius
#define     F  1       //Fahrenheit
#define     R  2       // Rankine
#define     K  3       // Kelvin

Now, you can imagine what nice error messages I get when trying to use boost libraries that have things like this in them:

template< typename F > struct template_arity;

A few tactical #undefs could fix things, but it still feels like a ticking bomb. I could alternatively rewrite large parts of the third-party header, or maybe just try and isolate the sections I really need.

Is there any better solution to this problem?


What about only incuding the offnding files in a single .cpp file, and then just expose the functionality you need through a separate header?


Have the original author rewrite it for you?

Seriously, this looks like a case of very poor design. I'd rewrite the macros, perhaps as enums.


You can wrap the 3rd party library behind your own interface. This allows you to include only the 3rd party header in a source file, with no risk of messing with anything else. Actually this is good advice: Never include a 3rd party header in your own header. Always, and only, include 3rd party headers in your source files (.cpp).

Good luck!


Do you actually need these values in your code or are they only used in their header internally?

You could write a new header to include after that one which undef's the offending definitions before including anything else.

Probably not practical but macros cause this kind of pain so there may notbe much to do :(

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜