Skip to content

Instantly share code, notes, and snippets.

@Trass3r

Trass3r/foridx.cpp

Created Jul 28, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.