超载“空间”; C++ 中的运算符?
我在 C++ 方面有一点经验。我知道如何重载加号以及不重载什么,但想重载空格运算符。
例如:
MyObject obj();
result = obj - foo; // This would be treated as a normal '-' operation.
result = obj-foo; // This would invoke code which throws an assert at runtime
这将使我能够强制执行我试图为我的团队制定的某些风格指南。
I have a little bit of experience in C++. I know how to overload the plus sign and what not, but would like to overload the space operator.
For example:
MyObject obj();
result = obj - foo; // This would be treated as a normal '-' operation.
result = obj-foo; // This would invoke code which throws an assert at runtime
This would allow me to enforce certain style guidelines I'm trying to set forth for my team.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
C++ 中没有空格运算符。在大多数情况下,空格并不重要,从解析的角度来看,
和
之间没有区别,我对此感到高兴。
注意:在一些极端情况下(我想到的是嵌套模板),它实际上很重要,但在我看来,这更多的是语法的产物,而不是空白作为“运算符”处于活动状态的指示。
There is no space operator in C++. Whitespace is not significant in most cases, there is no difference from a parsing point of view between
and
Which I'm happy for.
Note: there are some corner cases (nested templates spring to mind) where it actually matters, but that's more of an artifact of the grammar than indications of the whitespace being active as an "operator", in my opinion.
Bjarne Stroustrup 确实曾经建议允许空格重载 [PDF ]。但鉴于这篇文章是在 4 月 1 日发表的,他可能并不是 100% 认真的……
不过这篇文章还是值得一读的。
Bjarne Stroustrup did at one time suggest allowing overloading of whitespaces [PDF]. But seeing as this article was published on April 1st he may not have been 100% serious...
The article is worth a read though.
不存在空白运算符这样的项目。一般来说,在 C++ 中,空格是无关紧要的,不会导致程序中的功能差异,因此没有什么可以重载的。
您正在寻找的是 C++ 静态分析工具。类似于 C++ 的 StyleCop。不幸的是,我在这方面没有太多经验,无法推荐特定的程序。不过,这个线程上的其他人可能也能做到。
There is no such item as the whitespace operator. In general in C++ whitespace is irrelevant and doesn't cause functionality differences in the program, hence there's nothing to overload.
What you're looking for is a static analysis tool for C++. Something akin to StyleCop for C++. Unfortunately I don't have a lot of experience in this area and can't recommend a specific program. Likely someone else on this thread will be able to though.
抱歉,不可能。
相反,请查看代码格式化程序 - 每个流行的 IDE 都有一些格式化程序(例如 eclipse 的 checkstyle),或者您可以在版本控制系统服务器中设置预提交挂钩,检查格式化之前和之后的代码是否相同,以及如果不是,它将返回错误而不是允许提交代码。
Sorry, not possible.
Instead look at code formatters - there are a few for every popular IDE (for example checkstyle for eclipse), or you can setup pre-commit hook in your version control system server, that checks if code before and after formattting is the same, and if not, it returns error instead of allowing to commit the code.
这是不可能的。空间不是运算符。如果您想强制执行风格,请使用静态代码分析工具并与您的团队成员交谈。尝试 cpplint 或类似的东西。
It's not possible. Space is not an operator. If you want to enforce style, use static code analysis tools and talk to your team members. Try cpplint or something similar.
如果您想强制执行风格指南,请获取可以识别风格违规的工具或应用程序。 Klocwork 是一个可以做到这一点的工具。可能有更小、更简单的工具。
最坏的情况,你自己写。
If you want to enforce style guidelines, obtain a tool or application that can identify violations of style. A tool that can do this is Klocwork. There are probably smaller and simpler tools out there.
Worst case, write your own.
我建议您查看 Guy Steele 的新 HPC 语言:Fortress。它允许重载并置运算符。 此处还有一篇博客文章介绍了这方面的内容。它不会帮助你格式化,但它相当时髦。 (Haskell 和 ML 也使用并置来应用函数)。
I'd recommend a look at Guy Steele's new HPC language: Fortress. It allows the overloading of the juxtaposition operator. There's also a blog post covering this aspect here. It's not going to help you with formatting, but it's rather funky. (Haskell and ML also use juxtaposition to apply functions).