Old style casts and Boost style guidelines

I wanted to get rid of all old-style casts in my application, so I turned on -Wold-style-cast in gcc and found a whole bunch of old-style casts in boost headers. It seems to me that the boost coding style guidelines ought to explicitly discourage old-style casts. Any one else have a point of view? I've attached a list of the old-style casts gcc found in the 1.36 headers. This list isn't exhaustive, since my app doesn't use all boost libraries. :) -- Jon Biggar jon@floorboard.com jon@biggar.org jonbiggar@gmail.com ../../../boost/asio/detail/posix_fd_set_adapter.hpp:41: warning: use of old-style cast ../../../boost/asio/detail/posix_fd_set_adapter.hpp:45: warning: use of old-style cast ../../../boost/asio/detail/posix_fd_set_adapter.hpp:53: warning: use of old-style cast ../../../boost/asio/detail/signal_init.hpp:41: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:117: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:163: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:423: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:469: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:470: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:471: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:558: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:560: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:581: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:583: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:772: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:78: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:81: warning: use of old-style cast ../../../boost/asio/detail/socket_ops.hpp:858: warning: use of old-style cast ../../../boost/asio/detail/socket_select_interrupter.hpp:61: warning: use of old-style cast ../../../boost/asio/detail/socket_select_interrupter.hpp:68: warning: use of old-style cast ../../../boost/asio/detail/socket_select_interrupter.hpp:90: warning: use of old-style cast ../../../boost/asio/ip/address_v4.hpp:153: warning: use of old-style cast ../../../boost/asio/ip/address_v4.hpp:159: warning: use of old-style cast ../../../boost/asio/ip/address_v4.hpp:165: warning: use of old-style cast ../../../boost/asio/ip/address_v4.hpp:171: warning: use of old-style cast ../../../boost/asio/ip/address_v4.hpp:213: warning: use of old-style cast ../../../boost/asio/ip/address_v4.hpp:219: warning: use of old-style cast ../../../boost/asio/ip/address_v4.hpp:225: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:189: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:207: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:215: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:222: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:229: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:236: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:243: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:250: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:257: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:264: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:271: warning: use of old-style cast ../../../boost/asio/ip/address_v6.hpp:278: warning: use of old-style cast ../../../boost/asio/ip/basic_endpoint.hpp:71: warning: use of old-style cast ../../../boost/asio/ip/basic_endpoint.hpp:99: warning: use of old-style cast ../../../boost/asio/ip/detail/socket_option.hpp:275: warning: use of old-style cast ../../../boost/asio/ip/detail/socket_option.hpp:284: warning: use of old-style cast ../../../boost/asio/ip/detail/socket_option.hpp:353: warning: use of old-style cast ../../../boost/asio/is_read_buffered.hpp:54: warning: use of old-style cast ../../../boost/asio/is_write_buffered.hpp:54: warning: use of old-style cast ../../../boost/concept_check.hpp:915: warning: use of old-style cast ../../../boost/concept_check.hpp:928: warning: use of old-style cast ../../../boost/concept/detail/general.hpp:29: warning: use of old-style cast ../../../boost/concept/detail/general.hpp:37: warning: use of old-style cast ../../../boost/concept/detail/has_constraints.hpp:40: warning: use of old-style cast ../../../boost/concept/usage.hpp:21: warning: use of old-style cast ../../../boost/detail/lcast_precision.hpp:176: warning: use of old-style cast ../../../boost/detail/lcast_precision.hpp:177: warning: use of old-style cast ../../../boost/detail/sp_convertible.hpp:48: warning: use of old-style cast ../../../boost/exception/enable_error_info.hpp:85: warning: use of old-style cast ../../../boost/functional/detail/hash_float.hpp:180: warning: use of old-style cast ../../../boost/functional/detail/hash_float.hpp:182: warning: use of old-style cast ../../../boost/functional/hash/hash.hpp:112: warning: use of old-style cast ../../../boost/functional/hash/hash.hpp:114: warning: use of old-style cast ../../../boost/functional/hash/hash.hpp:132: warning: use of old-style cast ../../../boost/functional/hash/hash.hpp:134: warning: use of old-style cast ../../../boost/function/function_base.hpp:300: warning: use of old-style cast ../../../boost/function/function_base.hpp:347: warning: use of old-style cast ../../../boost/function/function_base.hpp:434: warning: use of old-style cast ../../../boost/function/function_base.hpp:619: warning: use of old-style cast ../../../boost/function/function_template.hpp:342: warning: use of old-style cast ../../../boost/function/function_template.hpp:435: warning: use of old-style cast ../../../boost/iterator/detail/facade_iterator_category.hpp:145: warning: use of old-style cast ../../../boost/iterator/detail/facade_iterator_category.hpp:151: warning: use of old-style cast ../../../boost/iterator/detail/facade_iterator_category.hpp:152: warning: use of old-style cast ../../../boost/iterator/detail/facade_iterator_category.hpp:153: warning: use of old-style cast ../../../boost/iterator/detail/facade_iterator_category.hpp:155: warning: use of old-style cast ../../../boost/iterator/detail/facade_iterator_category.hpp:166: warning: use of old-style cast ../../../boost/lexical_cast.hpp:578: warning: use of old-style cast ../../../boost/lexical_cast.hpp:694: warning: use of old-style cast ../../../boost/lexical_cast.hpp:764: warning: use of old-style cast ../../../boost/mpl/aux_/preprocessed/gcc/arg.hpp:117: warning: use of old-style cast ../../../boost/mpl/aux_/preprocessed/gcc/arg.hpp:27: warning: use of old-style cast ../../../boost/mpl/aux_/preprocessed/gcc/arg.hpp:45: warning: use of old-style cast ../../../boost/mpl/aux_/preprocessed/gcc/arg.hpp:63: warning: use of old-style cast ../../../boost/mpl/aux_/preprocessed/gcc/arg.hpp:81: warning: use of old-style cast ../../../boost/mpl/aux_/preprocessed/gcc/arg.hpp:99: warning: use of old-style cast ../../../boost/none.hpp:23: warning: use of old-style cast ../../../boost/regex/v4/cpp_regex_traits.hpp:284: warning: use of old-style cast ../../../boost/regex/v4/cpp_regex_traits.hpp:294: warning: use of old-style cast ../../../boost/regex/v4/cpp_regex_traits.hpp:653: warning: use of old-style cast ../../../boost/regex/v4/cpp_regex_traits.hpp:663: warning: use of old-style cast ../../../boost/regex/v4/match_results.hpp:106: warning: use of old-style cast ../../../boost/regex/v4/match_results.hpp:115: warning: use of old-style cast ../../../boost/regex/v4/match_results.hpp:128: warning: use of old-style cast ../../../boost/regex/v4/match_results.hpp:369: warning: use of old-style cast ../../../boost/regex/v4/match_results.hpp:370: warning: use of old-style cast ../../../boost/regex/v4/match_results.hpp:94: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_common.hpp:787: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_common.hpp:831: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_common.hpp:866: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher.hpp:50: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher.hpp:54: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:432: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:433: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:476: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:477: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:665: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:671: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:733: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:739: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:802: warning: use of old-style cast ../../../boost/regex/v4/perl_matcher_non_recursive.hpp:808: warning: use of old-style cast ../../../boost/regex/v4/regex_split.hpp:124: warning: use of old-style cast ../../../boost/regex/v4/regex_token_iterator.hpp:112: warning: use of old-style cast ../../../boost/regex/v4/regex_token_iterator.hpp:141: warning: use of old-style cast ../../../boost/regex/v4/sub_match.hpp:69: warning: use of old-style cast ../../../boost/regex/v4/sub_match.hpp:75: warning: use of old-style cast ../../../boost/thread/locks.hpp:1259: warning: use of old-style cast ../../../boost/thread/locks.hpp:1293: warning: use of old-style cast ../../../boost/thread/locks.hpp:1299: warning: use of old-style cast ../../../boost/thread/locks.hpp:1305: warning: use of old-style cast ../../../boost/thread/locks.hpp:43: warning: use of old-style cast ../../../boost/thread/locks.hpp:59: warning: use of old-style cast ../../../boost/thread/locks.hpp:75: warning: use of old-style cast ../../../boost/thread/pthread/timespec.hpp:28: warning: use of old-style cast ../../../boost/thread/thread_time.hpp:34: warning: use of old-style cast ../../../boost/thread/xtime.hpp:83: warning: use of old-style cast

Jon Biggar:
I wanted to get rid of all old-style casts in my application, so I turned on -Wold-style-cast in gcc and found a whole bunch of old-style casts in boost headers.
...
../../../boost/detail/sp_convertible.hpp:48: warning: use of old-style cast
(Y*)0 is so much nicer than static_cast< Y* >( 0 )... :-/

Peter Dimov wrote:
I wanted to get rid of all old-style casts in my application, so I turned on -Wold-style-cast in gcc and found a whole bunch of old-style casts in boost headers.
...
../../../boost/detail/sp_convertible.hpp:48: warning: use of old-style cast
(Y*)0 is so much nicer than static_cast< Y* >( 0 )... :-/
True, but most of the stuff in boost headers is write-once. -- Jon Biggar jon@floorboard.com jon@biggar.org jonbiggar@gmail.com

Jon Biggar wrote:
Peter Dimov wrote:
I wanted to get rid of all old-style casts in my application, so I turned on -Wold-style-cast in gcc and found a whole bunch of old-style casts in boost headers.
...
../../../boost/detail/sp_convertible.hpp:48: warning: use of old-style cast
(Y*)0 is so much nicer than static_cast< Y* >( 0 )... :-/
True, but most of the stuff in boost headers is write-once.
... rewrite always, yes :-) Speaking in general --not about the specific usages spotted here by gcc, which I haven't examined-- there are some cases where C-style casts can't do any harm, and some where they have been used because of compiler deficiencies (at least one of the latter is in bitset_test.hpp, though it's adequately commented). For the rest, I think you're seeing the hair and not the beam, so to speak. -- Genny

Gennaro Prota wrote:
Jon Biggar wrote: Speaking in general --not about the specific usages spotted here by gcc, which I haven't examined-- there are some cases where C-style casts can't do any harm,
Other than perhaps to encouraging code written in a modern-style. :)
and some where they have been used because of compiler deficiencies (at least one of the latter is in bitset_test.hpp, though it's adequately commented).
Yes, I had wondered about compiler deficiencies. I'm a bit surprised that boost doesn't already have "broken compiler" macros for each of the new-style casts anyway.
For the rest, I think you're seeing the hair and not the beam, so to speak.
It's hard to find the beam in my own code when I need to sift through all that hair from the boost headers. :) Seriously, the problem with warnings in headers is that you see them over and over and over and over...all in one compile. -- Jon Biggar jon@floorboard.com jon@biggar.org jonbiggar@gmail.com

Jon Biggar wrote:
It's hard to find the beam in my own code when I need to sift through all that hair from the boost headers. :)
Seriously, the problem with warnings in headers is that you see them over and over and over and over...all in one compile.
Are you using boost in a place other than the system include directories? gcc shouldn't give these warnings for system headers, so I believe you are. Assuming I'm right, you can work around the problem by specifying the include path as "-isystem /path/to/boost" rather than "-I /path/to/boost". This is not very 'nice' or portable, but it's probably fine for the duration of your warning sifting. There can still be problems, though, if you use boost macros with old style casts in your own files. IME, BOOST_STATIC_ASSERT is the worst offender (according to the docs it's because of compiler deficiencies). That was what finally forced my to turn off -Wold-style-cast in some of my code. John Bytheway

John Bytheway wrote:
There can still be problems, though, if you use boost macros with old style casts in your own files. IME, BOOST_STATIC_ASSERT is the worst offender (according to the docs it's because of compiler deficiencies). That was what finally forced my to turn off -Wold-style-cast in some of my code.
Is this still an issue with the current Trunk code, I thought I had that issue quashed? John.

John Maddock wrote:
John Bytheway wrote:
There can still be problems, though, if you use boost macros with old style casts in your own files. IME, BOOST_STATIC_ASSERT is the worst offender (according to the docs it's because of compiler deficiencies). That was what finally forced my to turn off -Wold-style-cast in some of my code.
Is this still an issue with the current Trunk code, I thought I had that issue quashed?
I'm still using 1.35, but indeed it looks like the trunk code doesn't have this issue (based on visual inspection). Thanks for that; I look forward to its arrival in my distro :). John

Jon Biggar wrote:
Gennaro Prota wrote:
Jon Biggar wrote: Speaking in general --not about the specific usages spotted here by gcc, which I haven't examined-- there are some cases where C-style casts can't do any harm,
Other than perhaps to encouraging code written in a modern-style. :)
You're right: under some authors' interpretation of "modern" that's a harm :-) [snip]
For the rest, I think you're seeing the hair and not the beam, so to speak.
It's hard to find the beam in my own code when I need to sift through all that hair from the boost headers. :)
I wasn't talking about your code, of course. -- Genny

Peter Dimov wrote:
Jon Biggar:
I wanted to get rid of all old-style casts in my application, so I turned on -Wold-style-cast in gcc and found a whole bunch of old-style casts in boost headers.
...
../../../boost/detail/sp_convertible.hpp:48: warning: use of old-style cast
(Y*)0 is so much nicer than static_cast< Y* >( 0 )... :-/
It is shorter, as are most uses of old-style casts as opposed to C++ new-style casts, but what makes it "nicer" ? To me a consistent syntax which makes casts more easily recognizable is "nicer", so I always consider new-style casts "nicer".

Edward Diener:
Peter Dimov wrote: ...
(Y*)0 is so much nicer than static_cast< Y* >( 0 )... :-/
It is shorter, as are most uses of old-style casts as opposed to C++ new-style casts, but what makes it "nicer" ?
It's easier to read. C-style casts, in general, are shorter, but not necessarily easier to read, because one needs to look up the source (and sometimes target) types to determine what the cast actually does (is it a static_cast, a reinterpret_cast, a const_cast, or a combination) and whether it's correct. This slows the reader down, which is not nice. In this case, though, the meaning is unambiguous. (Y*)0 is - obviously - a null pointer "literal" of type Y*. So it's nice. :-)

Peter Dimov wrote:
In this case, though, the meaning is unambiguous. (Y*)0 is - obviously - a null pointer "literal" of type Y*. So it's nice. :-)
It's a pitty that function-style casts are invalid here, since pointers don't have constructors. :-) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...

On Mon, 15 Sep 2008 21:01:48 -0400, Stefan Seefeld <seefeld@sympatico.ca> wrote:
Peter Dimov wrote:
In this case, though, the meaning is unambiguous. (Y*)0 is - obviously - a null pointer "literal" of type Y*. So it's nice. :-)
It's a pitty that function-style casts are invalid here, since pointers don't have constructors. :-)
I don't think that's the reason; this is valid: typedef int* intp; intp x = intp(); -- David Abrahams Boostpro Computing http://www.boostpro.com

Peter Dimov wrote:
Edward Diener:
Peter Dimov wrote: ...
(Y*)0 is so much nicer than static_cast< Y* >( 0 )... :-/
It is shorter, as are most uses of old-style casts as opposed to C++ new-style casts, but what makes it "nicer" ?
It's easier to read. C-style casts, in general, are shorter, but not necessarily easier to read, because one needs to look up the source (and sometimes target) types to determine what the cast actually does (is it a static_cast, a reinterpret_cast, a const_cast, or a combination) and whether it's correct. This slows the reader down, which is not nice.
In this case, though, the meaning is unambiguous. (Y*)0 is - obviously - a null pointer "literal" of type Y*. So it's nice. :-)
If 'nullptr' becomes part of C++ in the future I imagine that one would be able to implicitly pass that around without casting at all whenever a pointer is appropriate. This would eliminate such a consideration in the present case.
participants (8)
-
David Abrahams
-
Edward Diener
-
Gennaro Prota
-
John Bytheway
-
John Maddock
-
Jon Biggar
-
Peter Dimov
-
Stefan Seefeld