Obviously, a for loop is nothing more than a leak. Look at it like this:
for( ) { cout << "World"; }
So, the first thing you need to do is get rid of all the annoying code that is already in the program:
for( ;0;); if(0 ) { cout << "World"; }
Since, naturally, they will want to test your intelligence, let's do something very smart as follows:
for( ;0;); struct cout { void operator<<(cout world) { hello <world> !""; } char const* out; cout() : out(0) { } cout(char const* out) : out(out) { } operator bool() { return !out; } struct typeof { typeof(decltype(::std::cout) const&) { } void operator>(bool a) { ::std::cout << (!a ? "!" : "?"); } }; struct { typeof operator<(cout c) { return typeof(::std::cout << "Hello " << c.out); } } hello; } cout; if(cout ) { cout << "World"; }
Note. Since they like the name cout , be sure to use it in your code!
Also note the common GCC error (well, they call it the β extension β), which can cause problems with your C ++ compatible code.
Finally, recall that the initial instructions did not call for any new line at the end, so don't expect it. If you want to see the result right away, you can flush your output.
Some of the hoops that you should skip may seem unnecessary, but the strict requirement that you use a nested class instead of one at the namespace level causes some problems with proper operator overloading.
Personally, I would like to note that this code is very easy to understand: everything that it does says hello <world>! , so let's just reformat the result a bit, which will lead to your final code (this time the surrounding constructs are omitted):
;0;);struct cout{void operator<<(cout world){ hello <world>! "";}char const*out;cout():out(0){}cout(char const*out):out(out){} operator bool(){return!out;}struct typeof{typeof(decltype(::std::cout)const&){}void operator>(bool a){::std::cout<<(!a?"!":"?");}}; struct{typeof operator<(cout c){return typeof(::std::cout<<"Hello "<<c.out);}}hello;}cout;if(cout
Of course, the last step is taken only for purely aesthetic reasons and can be omitted if necessary.
PS: In fact, he prints βHello World!β, And not β: - Hello World!β, Which, I assume, is exactly what it was said. Changing it to a different format remains as an exercise for the reader.