cglm icon indicating copy to clipboard operation
cglm copied to clipboard

Const correctness: add const qualifiers for unmodified function parameters

Open CaspianA1 opened this issue 2 years ago • 1 comments

The functions I've seen in this library do not label unmodified parameters with const qualifiers. This causes some problems with a lot of existing code in a project that I'm working on.

Say that I'm doing this:

const vec3 a = {1, 2, 3}, b = {1, 1, 2};

vec3 sum;
glm_vec3_add(a, b, sum);

With enough Clang warnings flags, Clang starts to complain:

warning: passing 'const vec3' (aka 'const float[3]') to parameter of type 'float *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]

In order to fix this, I must do an ugly const-cast in order to remove the error:

glm_vec3_add((float*) a, (float*) b, sum);

Therefore, I suggest adding const qualifiers to the unmodified parameters of each function signature. In this case, this would change the signature of glm_vec3_add from void glm_vec3_add(vec3 a, vec3 b, vec3 dest) to void glm_vec3_add(const vec3 a, const vec3 b, vec3 dest).

This would not be too hard to add, or break any existing code, it would just take a little while to update each function signature. For the sake of const-correctness, please consider making this change!

CaspianA1 avatar Mar 10 '23 05:03 CaspianA1

Hi @CaspianA1,

This is discussed somehow earlier at: https://github.com/recp/cglm/issues/83 also see https://github.com/recp/cglm/pull/86. If there is a good way to do this we can approach to. Any feedbacks discussions are welcome

recp avatar Mar 10 '23 06:03 recp

I've constified my fork, and all seems well with what I use. I haven't changed the comments though.

Kharzette avatar Feb 29 '24 21:02 Kharzette

Hmm nevermind, looks like I have some errors to fix that don't get built locally. (WebAssembly whatever that is)

Kharzette avatar Feb 29 '24 23:02 Kharzette

@Kharzette thanks,

There are some issues discussed at: https://github.com/recp/cglm/issues/83 like:

warning: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic]

Let's merge this issue with that one by closing this.

recp avatar Mar 01 '24 15:03 recp