tau
tau copied to clipboard
Make parameters and variables constant when they are not mutated
Hey, can you please merge this?
Please address my review changes from June.
@jasmcaus I don't see any code reviews from you on this pull request? What are you referring to?
Strange. Just made some updates. LMK if you can see those.
I still don't see it. Are you sure you are submitting the code review? (big green "submit review" button :>)
Yes. Strange why it isn't visible to you.
I'll add below what I'd added:
- What's the difference between
const char*...,char* const...andconst char* const...? -- intauShouldDecomposeMacro(tau.h:435) - Why the
const* const? -- intauCmdLineRead(tau.h:1019)
Sorry about that. Please see above reply.
const char*... and char const*... are equivalent, they mean that the value of the pointer can change, but the pointer may not be used to modify the value being pointed-to (no matter if it's a constant or not).
char* const... means that the value of the pointer cannot change, but it can be used to modify the pointed-to value.
const char* const... and char const* const... combines the two, so you get both a pointer which cannot change, and the value pointed-to which cannot be modified using this pointer.
int foo = 2;
int const bar = 3;
int *const foo_ptr = &foo;
int const* bar_ptr = &bar;
foo_ptr = world_ptr; // Invalid since foo_ptr variable is constant.
*foo_ptr = 0; // Valid since the foo int is not constant.
*bar_ptr = 1; // Invalid since the value pointed to by bar_ptr is constant.
bar_ptr = foo_ptr; // Valid since the bar_ptr variable is not constant.
// You can go a step further though:
int const* const foo_ptr2 = &foo; // or const int* const foo_ptr2 = &foo;
// This prevents us from modifying the foo int using this pointer even though
// the foo variable itself is not a constant.
// You can also do this:
int const* test_ptr = foo_ptr;
test_ptr = bar_ptr;
The reason I added const in so many places was because most of the variables do not change and if someone wanted to pass constant values to Tau macros/functions, they wouldn't be able to compile. This essentially allows more of the code of Tau and projects using Tau to be immutable, so it gives more guarantees to a human reader. As an extra, you might get extra compiler optimizations thanks to the immutable guarantee.
P.S. Sorry it took me a while, but had to move and was quite busy in general.
Hey, hope you didn't forget about this :).
Yup, thanks for this!