Type-safe generic data structures in plain-old C? -


I have done more C ++ programming than "plain old C" programming. One thing that I miss a lot when there are programming type-safe generic data structures in plain C, which are provided by templates in C ++.

For concretions, consider a general single link list. In C ++, this is a simple matter to define your template class, and then instantiate it for those types of methods instantly.

In C, I can think of some ways of implementing a common single link list:

  1. Type the linked list type and helpful processes once Use zero signals to move around one type of type system.
  2. Write preprocessor macros while taking the required types of names, to generate a type-specific version of the data structure and support processes.
  3. Use a more sophisticated, standalone tool for the codes of the required types. I do not like option 1, because it changes the type system, and can be worse than a specific type-specific implementation, a uniform representation of the data structure for all types, And for zero signals / castings, as far as I can see, an indication is needed that special implementation will be avoided for any element type.

    Option 2 does not require any additional equipment, but it feels somewhat frightening, and can give bad compressor errors inappropriately used.

    Option 3 Compared to Better Compiler Error Message Option 2, the special data structure code will be in extended form which can be opened in an editor and inspected by the programmer (as opposed to the code generated by preprocessor macros) . However, this option is the most weighty, one type of "poor-man template" I have used earlier in this approach, using a simple script for a specialist of "templated" version of some code.

    I want to do my future "low-level" projects in C. Instead of C, but are afraid of rewriting general data structures for each specific type.

    What do people have experience with this problem? Is there a good library of generic data structures and algorithms that do not go with option 1 (i.e. casting casting zero pointers, sacrificing the type of security and giving a level of disbelief)? Option 1 I see the approach taken by the most implementation of generic containers.

      • I do not think I would consider a safe type in one of these ways. Useful, but not secure type


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -