I was recently struggling with the '
const int* const'
syntax. This is not yet supported in C2.
To narrow the discussion, I think only const in combination with pointers are an issue.
const i32 Max = 10;
is no issue. It is roughly equivalent of using a define in C.
Another note for Alias types in C2:
type IntP int*; // alias type
const IntP ip = a; // is a 'const IntP' ... but weird, 'const IntP' is different from 'const int*'
I always use the keyword const in combination with pointer arguments to indicate that the destination
is not changed. This is indeed more of an API thing. Also I recently had to work with a lot of UEFI
code. This is C code with many macros. One is that they use IN and OUT in all the header files. These
expend to nothing and are used like
void example(IN Buffer* buffer, OUT Result* result);
This is also use in other languages like Ada:
procedure TEST01 ( In_State : IN VECT_B ; Out_State : IN OUT VECT_B );
I think words like IN/OUT read quite naturally. But to include them in C2, we would have
to decide on what this means with pointers or pointers-to-pointers. I can think of the following use-cases:
- passing pointer - the dest may not be modified
- passing pointer - the dest may be modified
- passing pointer-pointer - the param may be modified, normal out-param case
- passing multiple-dimension pointer arg - dest may not be modified
The goal here should be making the code more readable (or easier to understand),
not allowing extra compiler-optimizations. Or does anyone know if const unlocks some
very powerfull optimizations?