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:
11 string myTest()
12 {
13 if(!testCondition)
14 return logError("testcondition failed");
15 }
And I want the result of the function to be:
“myTest line 14: testcondition failed”
How can I write logError? Does it have to be some monstrosity of a macro?
Why do you even need it as a string? What’s wrong with an integer? Here are two ways you could write
logError():If you really need the line as a string, you can use the stringizing operator
#, but because of the way macros work, you’ll need to wrap it in two macros:And now
LINE_STRINGis a macro that will expand to a string containing the current line number wherever it is expanded. If you only had one level of macros (i.e. if you had#define STRINGIZE(x) #x), then you would get the literal string"__LINE__"every time you expanded it, which is not what you want.