如何在字符串中使用编译时常量 __LINE__ ?
我可以使用 __LINE__ 作为方法参数,但我想要一种在使用字符串的函数中使用它的简单方法。
例如,假设我有这个:
11 string myTest()
12 {
13 if(!testCondition)
14 return logError("testcondition failed");
15 }
我希望函数的结果是:
“myTest 第 14 行:测试条件失败”
如何编写 logError?它一定是某种怪物般的宏吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
为什么你甚至需要它作为字符串?整数有什么问题?这里有两种编写
logError()
的方法:如果您确实需要将该行作为字符串,则可以使用字符串化运算符
#
,但由于宏的工作方式,您需要将其包装在两个宏中:现在
LINE_STRING
是一个宏,它将扩展为包含当前行号的字符串(无论它在何处扩展)。如果您只有一级宏(即如果您有#define STRINGIZE(x) #x
),那么每次您都会得到文字字符串"__LINE__"
扩展它,这不是你想要的。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_STRING
is 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.没有理由为此做任何运行时工作:
或者:
如果您是像詹姆斯这样的酷人。
There's no reason to do any run-time work for this:
Or:
If you're a cool person like James.
他的目标是创建一个宏(名为 logError),该宏将自动包含必要的符号,并仅使用字符串文字在预处理器中进行字符串连接。
因此,结合到目前为止基本正确的答案,让我们编写宏:
然后您可以在任何地方使用此宏在编译时以字符串文字格式创建通用错误消息代码。
注意:如果您愿意,您还可以使用
__FUNCTION__
(或等效项,因编译器而异)而不是__FILE__
来跟踪函数名称而不是文件姓名。His goal is to create a macro (named logError) that will automatically include the symbols necessary and do the string concatenation within the preprocessor, only using string literals.
So, combining the basically-correct answers answers thus far, let's write the macro:
You can then use this macro anywhere to create a generic error message code in string literal format at compile time.
Note: You can also use
__FUNCTION__
(or an equivalent, it varies by compiler) instead of__FILE__
, if you prefer, to keep track of the function name instead of the file name.将数字格式化为字符串的常用选项适用:Boost lexical_cast、ostringstream、sprintf 或 snprintf 等。
这是我最喜欢的主题链接之一:http://www.gotw.ca/publications/mill19.htm
The usual options for formatting a number into a string apply: Boost lexical_cast, ostringstream, sprintf or snprintf, etc.
Here is one of my favorite links on the topic: http://www.gotw.ca/publications/mill19.htm
是的,它很丑。您需要宏的组合。将整数转换为字符串是一个两步过程 - 这是 Boost 的实现:
现在您可以生成一个字符串:
Yes, it's ugly. You need a combination of macros. Converting an integer to a string is a two-step process - here's Boost's implementation:
Now you can generate a string:
用法:
如果您愿意的话,您可以为此创建一个宏:
然后用法将是:
Usage:
You could then make a macro for this if you were so inclined:
And then the usage would be:
应该用c风格来做。我知道使用 C++ 字符串库有很多方法可以做到这一点。
您还可以使用 strcat() 或 strncat 或任何其他数量的 C 库来执行此操作。
也会起作用。
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.
will work as well.
试试这个?
Try this?