Skip to content

Instantly share code, notes, and snippets.

@Trass3r
Created July 28, 2020 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Trass3r/b8ab5916c7b40e1e58c4c221e0803620 to your computer and use it in GitHub Desktop.
Save Trass3r/b8ab5916c7b40e1e58c4c221e0803620 to your computer and use it in GitHub Desktop.
C++17 range-for with index macro: https://godbolt.org/z/Yd4j8G
#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