dimanche 29 mars 2015

Can C sort at compile time?

Is it possible to sort elements at compile time in C?


Syntax is of secondary importance, I was thinking of a macro like this:



SORT(9, -1, 12, 4) // expands to: -1, 4, 9, 12
SORT(dog, cat, cow) // expands to: cat, cow, dog


but I won't frown at any API as long as it sorts without issuing a single CPU instruction.


The requirements are pretty lax:



  • Pure C, no C++. Staying within the C standard would be nice but established language extensions are fair game.

  • Compiler is the only tool allowed. Unix sort or home-brewed code generators are not welcome as they complicate the build step.

  • Any element type is fine. It's OK if it can sort e.g. numbers but not strings.

  • Fixed-length API is fine. Having to call SORT_5 to sort 5 elements is OK.


I realize the solution will probably resort to some language voodoo that barely compiles, I just want to know if it's possible.


Aucun commentaire:

Enregistrer un commentaire