How can I use the compile time constant __LINE__ in a string?

后端 未结 8 2071
旧巷少年郎
旧巷少年郎 2021-01-31 10:41

I can use __LINE__ as a method parameter just fine, but I would like an easy way to use it in a function that uses strings.

For instance say I have this:

8条回答
  •  梦谈多话
    2021-01-31 11:17

    sprintf(newStringBuffer, "myTest line %d: testcondition failed\n", __LINE__);
    

    should do it c style. I know that there are ways and ways of doing this with the C++ string libraries.

    You could also use strcat() or strncat or any other number of C libs to do this.

    cout <<"String" + __LINE__ + " another string" 
    

    will work as well.

提交回复
热议问题