stclib / STC

A modern, user friendly, generic, type-safe and fast C99 container library: String, Vector, Sorted and Unordered Map and Set, Deque, Forward List, Smart Pointers, Bitset and Random numbers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

about self type

liigo opened this issue · comments

I've found that, in the current api, the self type is X* (a pointer) if the api may mutate object, other wise the type is X (non-pointer type).

void  cmap_X_clear(cmap_X* self); // type: pointer of cmap_X
size_t cmap_X_size(cmap_X  map);  // type: cmap_X (non-pointer)

I'd not say this is wrong, actually it's OK, ... but lacks a bit of consistency.

I'd like to suggest a new pattern:

void cmap_X_clear(cmap_X* self);
size_t cmap_X_size(const cmap_X* self);  // `const` means self's state cannot be changed

This is a typical usage of const in C.

feel free to close it, if not makes sense.

Thanks for the suggestion, liigo. I have thought about this too, but I'm not sure what is best.
All simple const functions are passed by value, like ..._size(cmap_X map), so it is consistent across all containers.
However, find() takes a const self ptr, although it is a const fn..
I think I did it because it returns a non-const reference..., but it is a bit messy.

I actually like to pass by value when possible, because you see it from the call that argument is const. Performance are generally not hurt much by passing by value, especially when code is inlined. AlI critical function uses pointers anyway (including find). I will go over to check for other inconsistencies,

STC version 3.7 has shipped with these changes. ❤️🎉 Closing.