ho recentemente scoperto uno schema di ricorsione con il C-preprocessore meccanismo di inclusione di file CPP il preprocessore __INCLUDE_LEVEL__ letterale che è considerato automaticamente - quindi forse questo algoritmo funziona solo per gcc?!?
- L'algoritmo è concettualmente illimitato, può essere esteso con il file indiretto aggiuntivo.
- Il Herin presentato codice gestisce un ITERATION_COUNT 0-39.202
- con il commento/rimuovere il commento del ITERATION_SEPARATOR è possibile generare N elementi, o 1 elemento con N concatenazioni, adatto per ripetizioni di stringa.
- La macro ITERATION_ELEMENT viene utilizzato come "elemento ripetizione"
È possibile compilare il codice regolarmente, senza definisce aggiuntivi.L'invocazione della macro all'interno del codice è idempotente.
Un'uscita esemplificativa:
> gcc -o iterate.c iterate -Wall -s -O3 & & ./iterate.exe
0-1591 contatore
1592 Elements
iterate.c:
#include <stdio.h>
#include <inttypes.h>
int main(void) {
const char * preproc_array[] = {
#define ITERATION_COUNT 1592 //0-(199*197-1)39202 (maximum counter)
#define ITERATION_SEPARATOR , //this macro, if active, determines wheather there exits N separate elements otherwise, if outcommented, just 1 element with N concatenations
#define ITERATION_ELEMENT 0-__COUNTER__ Counter\n //the expanded macro as an arbitrary element
#include "iterate.h"
};
return !printf("%s%"PRIu32" Elements",preproc_array[
#ifndef NO_ITERATION_SEPARATOR
__COUNTER__-1
#else
0
#endif
], sizeof(preproc_array)/sizeof(const char *));
}
iterate.h:
#define ITERATION_START 1 //start index of first inclusion
#define ITERATION_LIMIT 199 //conforming to CPP preprocessor manual pg. 54 chapter 11.5, a limit of 200 is set arbitrary
#define ITERATION(...) _ITERATION(__VA_ARGS__)
#define _ITERATION(...) #__VA_ARGS__ ITERATION_SEPARATOR
#ifndef ITERATION_SEPARATOR
#define ITERATION_SEPARATOR
#define NO_ITERATION_SEPARATOR
#endif
//here begins the recursive algorithm via preprocessor file inclusion, enable the warnings if you want to see how it loops through
#if __INCLUDE_LEVEL__ <= ITERATION_COUNT/ITERATION_LIMIT
//~ #warning DIV
#define ITERATION_END ITERATION_COUNT/ITERATION_LIMIT+3 // + offset
#include "loop.h"
#define ITERATION_END ITERATION_LIMIT
#include "loop.h"
#include "iterate.h"
#endif
#if __INCLUDE_LEVEL__ == ITERATION_START
//~ #warning MOD
#define ITERATION_END ITERATION_COUNT%ITERATION_LIMIT+ITERATION_START
#include "loop.h"
#if ITERATION_COUNT % ITERATION_LIMIT
#define ITERATION_END 3 // + offset
#include "loop.h"
#endif
#endif
//end of alogrithm
loop.h:
#if __INCLUDE_LEVEL__ < ITERATION_END
#include "loop.h"
ITERATION(ITERATION_ELEMENT)
#undef ITERATION_END
#endif
fonte
2016-07-22 12:55:34
Sono abbastanza sicuro che non è possibile. Vedi un'altra domanda qui che è simile - http://stackoverflow.com/questions/319328/writing-a-while-loop-in-the-c-preprocessore – mattjgalloway
Grazie, @mattjgalloway. Sembri avere ragione Non c'è modo di lunghezza di ricorsione variabile in puro C99 usando il preprocessore. Quindi la mia idea sembra essere l'unica (brutta!) Via. – sonntam