[PATCH] D151187: [doc] Add casting style preference to coding standards

Sergei Barannikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 23 06:51:48 PDT 2023


barannikov88 added a comment.

In D151187#4364160 <https://reviews.llvm.org/D151187#4364160>, @jhenderson wrote:

> The issue here is that if a C-style/functional-style cast is added here, and then one of the types changes, the cast will prevent a potential build breakage. C++-style casts don't completely fix that, but should prevent some mistakes at least.

I don't think this is an issue. static_cast can't catch a change in size of an integer or a change between floating point types. In fact, for scalar types it works exactly the same as static_cast.
C-style cast can only be dangerous when casting pointers (downcasting and unrelated class hierarchies). Chances that one of the types changes to pointer is miserable.

Moreover, I find the difference between C-style casts and named casts more of a stylistic issue rather than a safety issue.
This does not make the issue less important though -- consistency is very important. And for that matter:

$ egrep -r "\(u?int[0-9]+_t\)" llvm | wc -l
1943
$ egrep -r "static_cast<u?int[0-9]+_t>" llvm | wc -l
1078

C-style cast wins :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151187/new/

https://reviews.llvm.org/D151187



More information about the llvm-commits mailing list