简体   繁体   English

在Android下删除C ++代码中的C样式转换

[英]Removing C-style casts in C++ code under Android

I saw this post on SO: Is there a way of disabling the old c style casts in c++ , and was excited to apply -Wold-style-cast to my Android C++ code. 我在SO上看过这篇文章: 有没有办法在c ++中禁用旧的c样式转换 ,并很高兴将-Wold-style-cast到我的Android C ++代码中。 I quickly ran into the following casts in stdio.h : 我很快在stdio.h遇到了以下演员:

static __inline int __sputc(int _c, FILE *_p) {
    if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n'))
        return (*_p->_p++ = _c);
    else
        return (__swbuf(_c, _p));
}

The file stdio.h was included through a series of other includes starting from ostream . 文件stdio.h是通过一系列其他包含从ostream开始的。 Should C++ library headers include C headers that do C style casts? C ++库头文件是否应包含进行C样式转换的C头文件? Has anyone tried disabling C style casts under Android NDK? 有没有人尝试在Android NDK下禁用C样式演员?

Yes, it's perfectly valid for a C++ standard library header to include C headers. 是的,对于包含C头的C ++标准库头,它是完全有效的。

If you want to get around this (without modifying the standard library code), you can disable the warnings before including the header then re-enable them using GCC Diagnostic Pragmas . 如果您想解决这个问题(不修改标准库代码),可以在包含标题之前禁用警告,然后使用GCC Diagnostic Pragmas重新启用它们。

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#include <iostream>
#include <vector>
// etc.
#pragma GCC diagnostic pop

The push and pop are there so that you can maintain the state of the diagnostics before and after the #pragmas . pushpop是存在的,因此您可以在#pragmas之前和之后维护诊断的状态。

Of course, you'll need to do this everywhere that you include the standard headers. 当然,您需要在包含标准标题的任何地方执行此操作。 If you have a lot of places that include them then it might be best to "refactor" your includes so that all your headers include one single header, which in turn includes the standard headers with the diagnostic wrappers. 如果你有很多地方包含它们,那么最好“重构”你的包含,这样你的所有标题都包含一个标题,而标题包含带有diagnostic包装的标准标题。

Yes, obviously C++ headers may include C headers. 是的,显然C ++标头可能包含C标头。 And the standard doesn't prohibit any header to perform a C-style cast. 并且该标准不禁止任何标题执行C风格的演员表。 No, I haven't tried that feature. 不,我没有尝试过这个功能。 Personally I avoid using C-Style casts without aid of the compiler. 我个人在没有编译器的帮助下避免使用C-Style演员表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM