我应该将一个CString传递给Format / printf(和一般的varargs)吗?

时间:2022-09-20 10:55:13

I recently took in a small MCF C++ application, which is obviously in a working state. To get started I'm running PC-Lint over the code, and lint is complaining that CStringT's are being passed to Format. Opinion on the internet seems to be divided. Some say that CSting is designed to handle this use case without error, but others (and an MSDN article) say that it should always be cast when passed to a variable argument function. Can * come to any consensus on the issue?

我最近接受了一个小型的MCF C ++应用程序,它显然处于工作状态。为了开始,我在代码上运行PC-Lint,并且lint抱怨CStringT正在传递给Format。互联网上的意见似乎有分歧。有人说CSting旨在无错误地处理这个用例,但是其他人(以及一篇MSDN文章)说它应该在传递给变量参数函数时始终被强制转换。 *可以就此问题达成共识吗?

1 个解决方案

#1


CString has been carefully designed to be passed as part of a variable argument list, so it is safe to use it that way. And you can be fairly sure that Microsoft will take care not to break this particular behavior. So I'd say you are safe to continue using it that way, if you want to.

CString经过精心设计,可以作为变量参数列表的一部分传递,因此以这种方式使用它是安全的。而且您可以相当确定Microsoft会注意不要破坏这种特殊行为。所以,如果你愿意,我会说你可以安全地继续使用它。

That said, personally I'd prefer the cast. It is not common behavior that string classes behave that way (e.g. std::string does not) and for mental consistency it may be better to just do it the "safe" way.

那就是说,我个人更喜欢演员。字符串类以这种方式行为并不常见(例如std :: string没有),并且对于心理一致性而言,以“安全”方式执行它可能更好。

P.S.: See this thread for implementation details and further notes on how to cast.

P.S。:请参阅此主题以获取实现细节以及有关如何投射的更多说明。

#1


CString has been carefully designed to be passed as part of a variable argument list, so it is safe to use it that way. And you can be fairly sure that Microsoft will take care not to break this particular behavior. So I'd say you are safe to continue using it that way, if you want to.

CString经过精心设计,可以作为变量参数列表的一部分传递,因此以这种方式使用它是安全的。而且您可以相当确定Microsoft会注意不要破坏这种特殊行为。所以,如果你愿意,我会说你可以安全地继续使用它。

That said, personally I'd prefer the cast. It is not common behavior that string classes behave that way (e.g. std::string does not) and for mental consistency it may be better to just do it the "safe" way.

那就是说,我个人更喜欢演员。字符串类以这种方式行为并不常见(例如std :: string没有),并且对于心理一致性而言,以“安全”方式执行它可能更好。

P.S.: See this thread for implementation details and further notes on how to cast.

P.S。:请参阅此主题以获取实现细节以及有关如何投射的更多说明。