通过C ++中的实例对象调用静态方法的警告 [英] Warning for calling static methods via instance object in C++

查看:120
本文介绍了通过C ++中的实例对象调用静态方法的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道通过实例对象调用静态方法是明确允许的

I know calling static method via instance object is explicitly allowed.

但是如果我想手动避免它,我怎么能在调用发生时被警告?我的意思是,是否有一些编译器选项(或一些特殊的属性)来检查情况?

But if I want to avoid it manually, how can I to be warned when the calling happens? I mean, is there some compiler options (or some special attributes) to check the situation?

我使用Clang,但任何其他编译器特定的功能也受欢迎。 / p>

I am using Clang, but any other compiler specific features are also welcome.

推荐答案

答案就是没有。根据其他Q& A,标准要求并允许这种行为。参见n3797 s9.4 / 2:

The answer is simply no. As per the other Q & A, the standard requires and permits this behaviour. See n3797 s9.4/2:


类X的静态成员可以使用qualified-id表达式X :: s;没有必要使用类成员访问语法(5.2.5)来引用静态成员。 可以使用类成员访问语法来引用静态成员,在这种情况下,将评估对象表达式。

由于钝语言,任何编译器如何做你要的?

Given that blunt language, how would any compiler do what you ask?

也许你应该寻找一个lint的版本。我找到了 http://stellar.cleanscape.net/products/cpp/checks.html

Perhaps you should be looking for a version of lint instead. I found http://stellar.cleanscape.net/products/cpp/checks.html and error 1705 that might be what you want, and I'm sure there are others.

因此,问题可能是你想要的是为什么lint,而不是编译器。这里有灰色阴影,但我认为编译器警告通常保留为是或可能是或可能实际上是错误的东西。它们可能是未定义的行为,或实现定义,或编译器执行与预期不同的地方。通过指针调用静态方法不在该类别中。它绝对是标准的一个要求,并且准确地生成您期望的代码。

So the question is why lint, instead of the compiler. There are shades of grey here, but I think compiler warnings are generally reserved for things that are are or could be or might actually be wrong. They might be undefined behaviour, or implementation defined, or places where the compiler does something different than might have been expected. Calling a static method via a pointer is not in that category. It is absolutely a requirement of the standard, and generates exactly the code you would expect.

编译器不是用于实施编码标准,还有其他工具。在C / C ++ Unix世界中,它历史上是lint,让您能够检测和抱怨违反编码标准,而不是编译器。在.Net世界里它一直是FxCop,虽然现在Visual Studio做了大部分相同的事情。当您希望代码满足特定标准时,您可以寻找一个执行静态代码分析的工具,例如此列表此处

Compilers are not for enforcing coding standards, there are other tools for that. In the C/C++ Unix world it is historically lint that gives you the capability to detect and complain about violations of coding standards, not the compiler. In the .Net world it has been FxCop, although now Visual Studio does most of the same things. When you want your code to satisfy specific standards you look for a tool that does static code analysis, such as this list here.

如果这没有回答您的问题,也许您可​​能想问另一个。就这方面而言,我相信编译器通过实现标准来执行正确的操作,而不发出警告。

If this doesn't answer your question, perhaps you might like to ask another one. As far as this goes, I'm confident that a compiler does the right thing by implementing the standard and not issuing a warning.

这篇关于通过C ++中的实例对象调用静态方法的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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