Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

I was bored

Name: Ikari Yui 2005-01-14 17:29

...so I wrote something that I won't understand five bits of in 5 years.
See if you can understand what it does without compiling it. It's valid C++, except for the __int64 type, which is Microsoft-specific (replace with whatever your compiler uses).

Have fun.

#include <iostream>
using namespace std;
typedef __int64 pszint; /* FOR 64-BIT POINTERS */
typedef unsigned __int64 uint64;
typedef const char* charptr;

template<typename T> inline void For( T val, const T& end, T (*inc)( const T& ), void (*proc)( const T& ) ) { if( val < end ) { proc( val ); For( inc( val ), end, inc, proc ); } }
template<typename T> inline T If( bool cond, T (*proc)(), T (*eproc)() ) { return ( cond ? proc() : eproc() ); }
template<typename T, typename Q> inline Q Comp( T (*one)(), Q (*two)( const T& ) ) { return two( one() ); }
//template<typename T> inline pszint TypeId() { static T tag; return (pszint) &tag; }

void NullProc() {}
int IncrementInt( const int& i ) { return i + 1; }
uint64 GetValue() { return 36762444129608; /* BIG-ENDIAN */ }
charptr ToString( const uint64& str ) { static uint64 temp; temp = str; return (charptr) &temp; }
charptr GetString() { return Comp<uint64, charptr>( GetValue, ToString ); }
void PrintAndPause( const charptr& str ) { cout << str; getchar(); }
void InnerProc() { Comp<charptr, void>( GetString, PrintAndPause ); }
void Body( const int &i ) { If<void>( i <= 5, InnerProc, NullProc ); }
int main() { For<int>( 1, 10, IncrementInt, Body ); }

Name: Ikari Yui 2005-01-15 15:47

TypeId() declares a static variable of type T, T being the template argument that it was called with (for instance, calling TypeId<int>() will make "tag" have type int). Due to the way templates work, this means that there will be one unique instance of "tag" for each type, allocated on the first call to TypeId with that type. Now, since all instances are unique, they will have unique memory addresses which are not used for anything else = one unique ID for each type. In effect, TypeId enables run-time type identification without RTTI (which creates overhead). I commented it out because my shitty little app doesn't use it.

That make any sense?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List