Created
July 28, 2020 12:32
-
-
Save Trass3r/b8ab5916c7b40e1e58c4c221e0803620 to your computer and use it in GitHub Desktop.
C++17 range-for with index macro: https://godbolt.org/z/Yd4j8G
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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