Using #define to include another file in C++/C

后端 未结 4 1216
刺人心
刺人心 2020-12-15 20:57

I want to define a macro which includes another header file like so:

#define MY_MACRO (text) #include \"__FILE__##_inline.inl\"

So that whe

4条回答
  •  情深已故
    2020-12-15 21:12

    You cannot use __FILE__ because that is already quoted, and #include doesn't support string concatenation. But you can use macros after #include:

    #define STRINGIZE_AUX(a) #a
    #define STRINGIZE(a) STRINGIZE_AUX(a)
    #define CAT_AUX(a, b) a##b
    #define CAT(a, b) CAT_AUX(a, b)
    #define MY_MACRO(file, name) STRINGIZE(CAT(file, CAT(name, _inline.inl)))
    #include MY_MACRO(aaaa, qqq)
    

    You should use the equivalent Boost.Preprocessor macros instead of CAT and STRINGIZE to prevent global namespace pollution.

提交回复
热议问题