Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Why I Hate C++ Cast Operators (2003) (informit.com)
2 points by pmelendez on Sept 11, 2015 | hide | past | favorite | 1 comment


My reason for liking C++ casts is covered at the end, which is to say that they're easier to find when scanning a code base. The article still tries to debunk this using two defenses that I don't agree with.

First, compilers with sufficient warnings enabled will tell you about implicit conversions. Perhaps they didn't do as well in 2003 when the article was written but they do now.

Second, you don't really have to search for all 3 cast tokens individually (most people would probably look for "_cast" and expect it to catch everything). If you want to be explicit though, "egrep '(dynamic|static|reinterpret)_cast'" isn't that hard, especially if you make it a script.

I do agree that the definitions of static_cast<> and reinterpret_cast<> are a bit unclear and to this day I probably screw them up in a few places. The naming of const_cast<> is also weird since it removes both "const" and "volatile", and it's even murkier now that C++11 has things like std::remove_const (presumably the latter is to help template implementations but are these not also forms of casting?).

Perhaps the operators should have been static methods in a standard class such as "std::cast::..." so that all present and future cast-like conversions could be found by searching for that prefix.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: