-
Notifications
You must be signed in to change notification settings - Fork 684
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
Added static asserts to fixed length header structures. #1693
base: dev
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## dev #1693 +/- ##
==========================================
- Coverage 83.11% 83.11% -0.01%
==========================================
Files 277 277
Lines 48207 48207
Branches 10192 9932 -260
==========================================
- Hits 40069 40068 -1
Misses 7243 7243
- Partials 895 896 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@Dimi1010 why are these static asserts needed? 🤔 |
They aren't needed per se, but they provide compile time safety that the header structures are precisely the specified fixed length, as they will raise an error at compile time if not. |
I always wondered why static asserts are needed 🙂 |
That is kinda the point. If you are changing the structs, it stops you by asking the question: "Do you really have a good reason to change it?". If you do, you change the assert too (and all the code that depends on the assert, hopefully). My opinion in general about them is that it prevents accidental changes and also ensures that certain assumptions that the developers make about the code are correct, instead of only relying on the dev(and all other devs that work the same code) that the assumption holds. It can also be used as a self documenting code. Without the assert, a person might not know that this struct is used in a way that needs it to be precisely N bytes long, unless they look through the code. There is the option of it being noted in documentation comments, but that is easier to forget to update than a static assert if making a change to the assumption. |
I think this change is helpful. It prevents people changing the struct layout accidently. I think asssertion and test are always useful. |
The PR adds cpp11
static_assert
statements after the declarations of fixed length structured used throughout Packet++ for encoding/decoding raw data.