0

I am implementing a vector library.

I planned to do it in the header .h file only.

// vector.h

typedef struct vec2_t {
    float x;
    float y;
} vec2;

Consider I will implement the add function, I am having 2 choices:

// also in vector.h

// 1st option
static inline vec2 add(vec2 lhs, vec2 rhs) {
    vec2 v;
    v.x = lhs.x + rhs.x;
    v.y = lhs.y + rhs.y;
    return v;
}
// usage in 1st option
vec2 v3 = add(v1, v2);

// 2nd option
static inline void add(const vec2 * const lhs, const vec2 * const rhs, vec2 * const rt) {
    rt->x = lhs->x + rhs->x;
    rt->y = lhs->y + rhs->y;
}
// usage of 2nd option
vec2 ret;
add(&v1, &v2, &ret);

So, if this is a normal function I will go with the 2nd option.

However, in this case, the static inline, I am not quite sure what is the appropriate way.

I learned that the inline keyword is just a hint, not a command that the compiler needs to follow. (What's the difference between "static" and "static inline" function?)

  • if the compiler decides not to inline, the 2nd option seems to be a good choice (especially if that is vec3 vec4).
  • if the compiler decides to inline, the 1st option makes sense (I don't think the compiler is smart enough to substitute the pointer -> to the member access .).

So with that in mind, my question is:

What is the appropriate way to define a static inline function in C language?

Thank you very much!

Khoa
  • 2,632
  • 18
  • 13
  • Why do you (think you) need to define the functions in the header file? – August Karlstrom Mar 13 '22 at 10:27
  • "convenience" is my keyword. a single header with all math stuff is my target. But I am not sure if this relates to my question ;) – Khoa Mar 13 '22 at 10:32
  • I use it for my personal project or prototype. If that is a real project, I will respect the convention of that project. – Khoa Mar 13 '22 at 10:37
  • 1
    If the objects indeed get large a third option might be returning by value and accepting by pointers. Returning by value most likely would profit from return value optimisation, i.e. the return value is written directly to where it should be assigned. If the compiler indeed inlines don't worry about resolving pointers – any valuable one should be able to, they usually do much more complex optimisations... – Aconcagua Mar 13 '22 at 10:56
  • 2
    "I don't think the compiler is smart enough". Do not guess, verify. – n. m. could be an AI Mar 13 '22 at 11:00
  • 1
    When you define a function with `static inline`, it will be compiled and made available in the current translation unit and will not cause multiple definition errors when included in other translation units. Semantically, this is fine: It gives your program the desired meaning, that all your functions are defined in the headers as you state as a goal. The question of whether the compiler “inlines” the function by inserting its code in places where its called is a separate question, one of optimization, rather than semantics… – Eric Postpischil Mar 13 '22 at 11:02
  • 1
    … So clarify your question. Do you want to know whether using `static inline` will accomplish the goal you state, of “doing it [implementing a vectory library] in the header `.h` file only”, or are you asking whether the compiler will “optimize” the call to be inline and/or how you can ensure that? If the latter, why do you care; what goal are you trying to achieve? – Eric Postpischil Mar 13 '22 at 11:03
  • @EricPostpischil Initially, I would like to know which implementing way of the `add` function is the appropriate way of a `static inline` function)? However, through your comment and @n.1.8e9-where's-my-sharem 's comment, I think I need to go back and research a bit about how things work. – Khoa Mar 13 '22 at 11:07

0 Answers0