我应该转换传递给Format / printf的CString(和一般的varargs)吗? [英] Should I cast a CString passed to Format/printf (and varargs in general)?

查看:74
本文介绍了我应该转换传递给Format / printf的CString(和一般的varargs)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用了一个小型MCF C ++应用程序,该应用程序显然处于工作状态。首先,我在代码上运行PC-Lint,lint抱怨将CStringT传递给Format。互联网上的意见分歧。有人说CSting旨在无错误地处理此用例,但其他人(还有 MSDN文章)说,在将其传递给变量参数函数时,应始终对其进行强制转换。可以在这个问题上达成Stackoverflow的共识吗?

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 Stackoverflow come to any consensus on the issue?

推荐答案

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

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.

话虽如此,我个人更喜欢演员表。字符串类的行为并不常见(例如std :: string不这样),为了保持心理一致性,最好以安全的方式进行。

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.

PS:请参见此线程有关实现的详细信息以及有关如何投射的更多说明。

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

这篇关于我应该转换传递给Format / printf的CString(和一般的varargs)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆