How to make preprocessor generate a string for __LINE__ keyword?

前端 未结 2 817
夕颜
夕颜 2021-02-02 08:10

__FILE__ is replaced with \"MyFile.cpp\" by C++ preprocessor. I want __LINE__ to be replaced with \"256\" string not with 256 integer. Without using m

2条回答
  •  -上瘾入骨i
    2021-02-02 09:00

    EDIT: In response to request on the other answer, I added a non-macro version:

    #include 
    #include 
    #include 
    
    #define B(x) #x
    #define A(x) B(x)
    
    void f(const char *s) {
    std::cout << s << "\n";
    }
    
    int main() {
    
       f(A(__LINE__));
       f(boost::lexical_cast(__LINE__).c_str());
    }
    

提交回复
热议问题