Generate include file name in a macro

前端 未结 3 2111
日久生厌
日久生厌 2020-12-05 23:43

I\'m trying to generate include file name in macro. This is supposed to be legal in C++:

#define INCLUDE_FILE \"module_impl_win.hpp\"
#include INCLUDE_FILE
<         


        
3条回答
  •  温柔的废话
    2020-12-06 00:35

    You can also use Boost Preprocessor, especially the BOOST_PP_STRINGIZE/BOOST_PP_CAT macros:

    #include 
    #include 
    
    #define INCLUDE_FILE(M) BOOST_PP_STRINGIZE(BOOST_PP_CAT(M, _impl_win.hpp))
    
    // expands to: #include "module_impl_win.hpp"
    #include INCLUDE_FILE(module)
    

    (cf. C Macro - Dynamic #include)

    Note that this suffers from the same problem as @Andrew's answer (leading underscore = reserved identifier).

提交回复
热议问题