Skip to content

Instantly share code, notes, and snippets.



Created Jul 28, 2020
What would you like to do?
C++17 range-for with index macro:
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <math.h>
#define for_i(...) \
if (decltype(sizeof(0)) i_next = 0; true) \
for (__VA_ARGS__) \
if (auto i = i_next++; true)
const uint32_t vals[] = {0, 1, 2, 3, 4};
void foridx()
for_i(uint32_t v : vals)
printf("i: %d\nv: %v", i, v);
void rawfor()
uint32_t i = 0;
for(uint32_t v : vals)
printf("i: %d\nv: %v", i++, v);
int main() {
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.