# Monday, 13 October 2008

Believe it or not, this code (snipped from something larger) compiles:

  unsigned int c = ( o-----o
                     |     !
                     !     !
                     !     !
                     o-----o ).area;
  assert( c == (I-----I) * (I-------I) );
  assert( ( o-------------o
            |L             \
            | L             \
            |  L             \
            |   o-------------o
            |   !             !
            !   !             !
            o   |             !
             L  |             !
              L |             !
               L|             !
                o-------------o ).volume == ( o-------------o
                                              |             !
                                              !             !
                                              !             !
                                              o-------------o ).area * int(I-------------I) );

Not only does it compile, the asserts succeed. What's more, o is not some instance of a class, and neither is I. As you might imagine, there's some bizarro stuff behind this. Templates, operator overloads, ... it's all reasonably silly and impressive at the same time. Needless to say, the ! and -- operators do not "do as the ints do" in this case.

The files you need are at http://www.xs4all.nl/~weegen/eelis/tutorial.cpp and http://www.xs4all.nl/~weegen/eelis/analogliterals.hpp. There's an explanation of sorts at http://www.xs4all.nl/~weegen/eelis/analogliterals.xhtml but my IE7 refused to load it, so I saved it on my machine and renamed .xhtml to .html. To test the code I used VS 2008, made a Win32 console app and just pasted the body of Eelis' main into my generated main, and the body of the header into an empty header file. No strange settings required and no warnings on compile.

Try it yourself!

Kate

 

Monday, 13 October 2008 07:52:20 (Eastern Daylight Time, UTC-04:00)  #