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. I quickly ran into the following casts in 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
. Should C++ library headers include C headers that do C style casts? Has anyone tried disabling C style casts under Android NDK?
Yes, it's perfectly valid for a C++ standard library header to include C headers.
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.
#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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With