06 March, 2010

ASCII art C++ constants

I came across Multi-Dimensional Analog Literals in C++. This is an implementation in standard C++ templates that lets you write constants out using ASCII art (in 1, 2 or 3 dimensions):

unsigned int c = ( o-----o
| !
! !
! !
o-----o ).area;

assert( c == (I-----I) * (I-------I) );

I think the same would be straightforward to implement using Template Haskell's quasiquoting mechanism, which allows you to embed parsers for fairly arbitrary sublanguages inside Haskell source. I wonder what other languages you could also implement something like this in.

No comments:

Post a Comment