After playing around a bit with C preprocessors, I thought of a way to have something similar to a Pythonian with control structure, defined like this:
#define with(var) for(int i##__LINE__=0;i##__LINE__<1;)for(var;i##__LINE__<1;++i##__LINE__)
Sample usage:
#include <cstdio>
#include "FileClass.hpp"
#include "with.hpp"
int main(){
with(FileClass file("test.txt")){
printf("%s\n",file.readlines().c_str());}
return 0;}
The idea is that a doubly-nested for loop has an outer obfuscated iteration variable which is incremented once in the inner loop to break it. This causes the following code to be executed once with var in its scope.
Are there any downsides to this? If I obfuscate the iteration variable enough, there would be almost no chance of having a name clash, it uses only standard preprocessor features in a way that doesn't seem to have any possibility of backfiring, and it's very easy to understand.
It almost seems too good to be true - is there any reason this isn't used everywhere?