-4

I'm encountering this terminology called generic library for the first time and i couldnt understanding the meaning of a generic libraries.

  • 1
    In C++, the generics mechanism is called "templates". Just mentioning so that you can do better research and improve the question. – Niklas B. Feb 25 '13 at 11:38
  • 1
    @NiklasB, generic doesn't necessarily refer to templates. It could be "generic sound processing library". OP needs to clarify what context he heard it in. –  Feb 25 '13 at 11:40
  • 1
    @NiklasB.: C89 did't have generics. modern C (the latest standard) has generics. or, what that standard calls generics – Cheers and hth. - Alf Feb 25 '13 at 11:42
  • @NiklasB.: Syntactically C has not straight way, but it doesn't mean you can not design a generic library. See [this](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.144.4358) and [this](http://sglib.sourceforge.net/) – masoud Feb 25 '13 at 11:42
  • @Cheersandhth.-Alf: Cool, what is it that the standard calls generics? – Niklas B. Feb 25 '13 at 11:42
  • Usually generics refers to function templates or class templates in C++, and that is related on pattern design. However, this term is used in C like in this example http://stackoverflow.com/questions/326202/generic-list-manipulation-function-in-c – Mihai8 Feb 25 '13 at 11:43

2 Answers2

2

a generic library is one that can be used with more than one type, e.g. more than one number type

in c11 a generic definition can look like this:

#define cbrt(X) _Generic((X), long double: cbrtl, \
                              default: cbrt, \
                              float: cbrtf)(X)

in earlier versions of C one had to use just various macro trickery, e.g. as illustrated in the SO question referred to by @user1929959 in the commentary

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • And `void*` based definitions are another approach... – masoud Feb 25 '13 at 11:47
  • Could you please point to an explanation of the code you are showing? The syntax looks awkard (me being not familar with c11) and I can not derive its intention. – Niklas R Feb 25 '13 at 11:49
  • @MM.: yeah, a means to the same end, but i never heard that approach referred to as generic code. the difference is one of the client code doing the casting to and from `void*`, instead of this being hidden in macros and made safe by centralized testing – Cheers and hth. - Alf Feb 25 '13 at 11:50
  • @Niklas: by googling "c++11 _Generic" i found [a blog with a simple explanation](http://www.robertgamble.net/2012/01/c11-generic-selections.html#more). of course the language standard is the final authority re details. – Cheers and hth. - Alf Feb 25 '13 at 11:53
  • @Cheersandhth.-Alf: It is a well known way to write generic stuffs in C. But the lack of encapsulation and transparency is one of reasons that we switched to C++. Read [this](http://stackoverflow.com/a/326232/952747) – masoud Feb 25 '13 at 11:56
0

"Generic" here probably means "parameterized datatype" (coming from Java?). Alas, C is very weak in juggling with (data-)types -- in the sense of "being type-safe". The usual way in C is therefore to parametrize an algorithm with call-back functions which you have to implement to do the type-specific work. The data itself is then usually represented by a void*.

As an example you can take a look at the C-standard function qsort (from man sort):

#include <stdlib.h>

void qsort(void *base, size_t nmemb, size_t size,
           int(*compar)(const void *, const void *));

base is a "generic" pointer to your data, nmemb and size is additional information about your data and compar will be the call-back-function that you have to provide that is able to compare two elements of your data-type.

There are many libraries that provide you with algorithms. The number is so vast that you would need to specify...

towi
  • 21,587
  • 28
  • 106
  • 187