0

In this game, post confusing or hard to understand code, and the reader will try to figure out how it works.
I will start with a small C++ program that uses #define's.

 #include <iostream>
 #include <string>

 using namespace std;

 #define FUNCTION(?????) ??????

 FUNCTION(inline, int, hello,
 for(int d = 0; d < i; d++) {
     cout << "\nhello " << t << d << "\n";
 }
 return i;
 );
 int main()  {
     return hello(5, "world ");
 }

The output of the program on Linux is:

    hello world 0
    hello world 1
    hello world 2
    hello world 3
    hello world 4

    Process returned 5 (0x5)

Anyone who guesses what the definition of FUNCTION() is, gets a cookie!

Edited by ModernC++

2
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by Cross213
0

Another sample in C++. It uses template metaprogramming to calculate Fib at compile time. It uses tree recursion so it is not really efficient, but it works :)

#include <cstdio>

template <int i>
struct Fib {
   const static int value = Fib<i-1>::value + Fib<i-2>::value;
};

template <>
struct Fib<0> {
   const static int value = 1;
};
template <>
struct Fib<1> {
   const static int value = 1;
};

int main()  {
    printf("%i", Fib<25>::value);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.