How to work with daft header files

I am trying to use a third-party SDK in C ++ code. Although the SDKs and headers are technically compatible with C ++, it's really just a piece of nasty C.

In particular, the main header files contain many hundreds of #defines, of which they are worse.

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

Now you can imagine what good error messages I get when I try to use boost libraries that have these kinds of things:

 template< typename F > struct template_arity; 

A few tactical #undefs can fix the situation, but it still looks like a tick bomb. I could alternatively rewrite large parts of the third-party header, or just try and isolate the sections that I really need.

Is there a better solution to this problem?

+4
source share
4 answers

How about just inserting files into a single .cpp file and then just expanding the functions you need through a separate header?

+8
source

Ask the author to rewrite it for you?

Seriously, this seems like a very poor design. I would rewrite macros, possibly like enum s.

+2
source

You can wrap a third-party library with your own interface. This allows you to include only the third-party header in the source file, without the risk of messing with anything else. This is actually good advice: never include a third-party title in your own title. Always and only include third-party headers in the source files (.cpp).

Good luck

+2
source

Do you really need these values โ€‹โ€‹in your code or are they only used inside your header?

You can write a new heading, which should include after it one that does not cancel the definition before including anything else.

Probably not practical, but macros cause such pain, so there may not be much to do: (

0
source

All Articles