Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernise: replaces C-style casts with static_cast< > #374

Merged

Conversation

krishnakumarg1984
Copy link
Collaborator

@krishnakumarg1984 krishnakumarg1984 commented Jun 22, 2023

According to this answer on stackoverflow,

  • C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime.
  • Also, C++ style casts can be searched for easily, whereas it's really hard to search for C style casts.
  • Another big benefit is that the 4 different C++ style casts express the intent of the programmer more clearly.
  • When writing C++ I'd pretty much always use the C++ ones over the the C style.

According to this answer on stackoverflow,

  • static_cast<>() gives you a compile time checking ability, C-Style cast doesn't.
  • static_cast<>() is more readable and can be spotted easily anywhere inside a C++ source code, C_Style cast is'nt.
  • Intentions are conveyed much better using C++ casts.

Further reading at learncpp

Although a C-style cast appears to be a single cast, it can actually perform a variety of different conversions depending on context. This can include a static cast, a const cast or a reinterpret cast (the latter two of which we mentioned above you should avoid). As a result, C-style casts are at risk for being inadvertently misused and not producing the expected behavior, something which is easily avoidable by using the C++ casts instead.
Also, because C-style casts are just a type name, parenthesis, and variable or value, they are both difficult to identify (making your code harder to read) and even more difficult to search for.

The main advantage of static_cast is that it provides compile-time type checking, making it harder to make an inadvertent error.
static_cast is also (intentionally) less powerful than C-style casts, so you can’t inadvertently remove const or do other things you may not have intended to do.


Closes #375.

@krishnakumarg1984 krishnakumarg1984 self-assigned this Jun 22, 2023
@krishnakumarg1984 krishnakumarg1984 changed the title Modernise codebase: replaces c-style casts with static_cast< > Modernise: replaces c-style casts with static_cast< > Jun 22, 2023
@krishnakumarg1984 krishnakumarg1984 changed the title Modernise: replaces c-style casts with static_cast< > Modernise: replaces C-style casts with static_cast< > Jun 22, 2023
@krishnakumarg1984 krishnakumarg1984 merged commit f35f143 into development Jun 23, 2023
@krishnakumarg1984 krishnakumarg1984 deleted the kg/replace_cstyle_casts_with_static_casts branch June 23, 2023 16:33
krishnakumarg1984 added a commit that referenced this pull request Jun 25, 2023
* development:
  feat(modernise): replaces c-style casts with static_cast< > (#374)
  feat(modernise): concatenates nested namespaces (capability introduced in C++17 std) (#368)
  chore: updates .gitignore with more ignore entries for C++/CMake
krishnakumarg1984 added a commit that referenced this pull request Jul 21, 2023
* chore(safety,perf): partial set of fixes aiming at const correctness

* chore: updates .gitignore with more ignore entries for C++/CMake

* feat(modernise): concatenates nested namespaces (capability introduced in C++17 std) (#368)

* feat(modernise): replaces c-style casts with static_cast< > (#374)

---------

Co-authored-by: Tuomas Koskela <t.koskela@ucl.ac.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modernise: replaces C-style casts with static_cast< >
2 participants