July/August 1999 Obfuscated C++

Last Month's Obfuscated C++

Last month we started with a simple template class that holds an integer matrix:
template <int A, int B> struct m {
	int _[A][B];
You were asked to explain the semantics of this template function:

template <int R, int B, int C>
f(const m < R,B > & x, const m<B,C> & y) {
	m<R,C> rc;
	for(int c=R-1;c>=0;-c) 
	for(int r=0;r<C;++r){
	int k=0;
	return rc;
The function f() performs matrix multiplication on the two matrices passed in as arguments. The RxB matrix x and the BxC matrix y are multiplied, giving an RxC matrix result. Each element is computed using the formula:


To obfuscate this calculation, we've used improper indenting, and we've taken care to name the row index c and the column index r. We've also walked through the rows in descending order but walked each column in ascending order. Also note the initialization in the inner for loop:

Although the initial value of the cell is indeterminate (because rc is an automatic POD structure), it has some value, and exclusive oring that value with itself will zero out the cell.

This Month's Obfuscated C++

This month we ask you to play lexical analyzer. What value is returned from this function?

int x=2,y=3,z=4;
int h(){
	*b /*//
	*c /*//
	*d /*/
	*e */
	*f *
No fair using text editors that chromacode comments!

Rob Murray is Director, Engineering at the Irvine office of Net Explorer, an object-oriented software consulting company based in Houston, TX. He has taught C++ at technical conferences since 1987, and is the author of C++ Strategies and Tactics. He was the founding editor of the C++ Report and can be contacted at [email protected].

Quantity reprints of this article can be purchased by phone: 717.560.2001, ext.39 or by email: [email protected].