Possible to convert list of #defines into strings

前端 未结 8 1093
梦谈多话
梦谈多话 2021-02-05 22:45

Suppose I have a list of #defines in a header file for an external library. These #defines represent error codes returned from functions. I want to wri

8条回答
  •  隐瞒了意图╮
    2021-02-05 23:15

    One possibility here is to write a little program that parses the .h file that contains the #defines, and emits the corresponding source code for the convertToString() function. Then you can have that program automatically run as part of your build process whenever the .h file is changed. It's a little more work up front, but once it's implemented you'll never again need to manually update your int<->string conversion function.

    This is especially useful if you want to support code in multiple languages that uses the same constants. For example, in one of my applications, changing my #defines header file causes the corresponding C++, Python, and Java files to be auto-regenerated. This makes project maintenance much easier and less error-prone.

提交回复
热议问题