c++/5089: -Wold-style-cast warns about cast to void

llib@computer.org llib@computer.org
Tue Dec 11 22:52:00 GMT 2001


>Number:         5089
>Category:       c++
>Synopsis:       -Wold-style-cast warns about cast to void
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Dec 11 22:46:02 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Bill Clarke
>Release:        3.0.2
>Organization:
>Environment:
sparc-sun-solaris2.8
>Description:
why is this a problem?
"assert(EX)" (at least on my system) expands to "(void)((EX) || (__assert(#EX, __FILE__, __LINE__), 0))" which means use of assert (even via the new <cassert> header, which simply includes <assert.h>) will give a warning.
casting to void is thought (by some) to be good practice for ignoring a return value from an expression.  perhaps c++ code should instead do a static_cast<void>(expr)!  (this is excessive)  void initialisation also works: void(expr).

one could claim that this warning is only for occasional testing, and not for everyday use in which case this problem should be mentioned in the documentation for -Wold-style-cast.
>How-To-Repeat:
problem is obvious.  solution is not.
>Fix:
either:
- change -Wold-style-cast to not warn about cast to void (and add another warning -Wold-style-void-cast?)
- change <cassert>/<bits/std_cassert.h>/<assert.h> by header munging to do a void initialisation: here is a diff with our system assert.h:[
--- /usr/include/assert.h       Thu Jan  6 09:09:40 2000
+++ assert.h    Wed Dec 12 17:24:18 2001
@@ -41,7 +41,9 @@
 
 #else
 
-#if defined(__STDC__)
+#ifdef __cplusplus
+#define assert(EX) (void((EX) || (__assert(#EX, __FILE__, __LINE__), 0)))
+#elif defined(__STDC__)
 #define        assert(EX) (void)((EX) || (__assert(#EX, __FILE__, __LINE__), 0))
 #else
 #define        assert(EX) (void)((EX) || (_assert("EX", __FILE__, __LINE__), 0))
]end of diff.  normal user code (or other system header macros) that do casts to void will still get lots of warnings however.
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list