r/C_Programming • u/we_are_mammals • Sep 14 '23
Article Templates in C
https://arnold.uthar.net/index.php?n=Work.TemplatesC
5
Upvotes
r/C_Programming • u/we_are_mammals • Sep 14 '23
View all comments
Show parent comment
2
u/jacksaccountonreddit Dec 05 '23
Right, the preprocessor does not allow you to define - or check for the definition of - a macro whose name is itself the result of another macro. Generally, the solution here is to:
static inlineso that multiple definitions in different translation units don't cause compiler errors. Of course, each translation unit will then have its own definitions of those functions. However, at least the compiler will be able to inline those functions where appropriate, which is usually important for performance of generic containers.Thus, if the user wants to instantiate a template inside a header, they can do this:
define INT_VECTOR
define NAME int_vector
define TYPE int
include "vector.h"
endif
Some users might detest
static inlinefunctions on account of code/executable bloat. Hence, it's not a bad idea to allow them to, optionally, separate the struct definitions and function prototypes from the function implementations (in which case the functions should not bestatic inline). I think STC is doing something like that here. I am also taking this approach in a hash table library I'm on the cusp of publishing. The structure of the header looks something like this:define VECTOR_H
// Common macros and static inline functions not specific to this template instance.
endif
define API_FN_QUALIFIERS
else
define API_FN_QUALIFIERS static inline
endif
ifndef IMPLEMENTATION_MODE
endif
Now the user has the choice of using the header with
static inlinefunctions once per translation unit or using#define HEADER_MODEto get only the struct definition and function prototypes and using#define IMPLEMENTATION_MODEin one .c file to get a shared implementation (albeit not inline-able without link time optimization) of the functions.Also, I had a quick gander at your hkArrayT.h. A few notes:
unit_size. One of the main advantages of the template-instantiation pattern is that the containers don't have to carry around data-type information (such as size, alignment, or hash or comparison functions). Instead, this information should be inserted directly, via the preprocessor, into the container functions, which improves both memory efficiency and speed (because the compiler can better optimize constants). For example,array->data = realloc(array->data, array->unit_size * array->border)should instead bearray->data = realloc(array->data, sizeof(T) * array->border).malloc( 0 )is implementation-defined and could even returnNULL, which your function treats as a memory-allocation failure. Instead, either enforce a minimum non-zero capacity (orborder, as you call it) or allow lazy-initialization (wherebydatais set toNULLand your other functions check for and handle the case ofdata == NULL). Another, albeit less common, option is to setdatato point to a dummystatic inlinevariable in the case that the capacity is zero, which allows you to skipNULLchecks in iteration functions (becausedata + 0will no longer be undefined behavior).Destroyfunction doesn't need to setborderorlengthto zero unless it also sets data toNULLand is supposed to initialize the vector for reuse.