Archive for August, 2013

An Interesting C++ Technique

Monday, August 26th, 2013

I recently discovered a C++ technique I haven’t seen recognized before in any books, articles, or mentioned online anywhere (search terms are difficult perhaps). For want of a better name I call it Structural Capture. Consider the following code:

#include <functional>
#include <iostream>
using namespace std;
 
//---------------------------------------------------------------
struct Foo
{
  template <typename T>
  Foo(const T& t)
  {
    m_outputFunc = [=] (ostream& s) -> ostream& { return s << t; };
  };
 
  function<ostream& (ostream&)> m_outputFunc;
};
 
ostream& operator<<(ostream& s, const Foo& f)
{
  return f.m_outputFunc(s);
}
 
//---------------------------------------------------------------
int main(int argc, char *argv[])
{
  Foo f(1);
  cout << f;
 
  return 0;
}

Foo is a regular struct(/class), but its constructor is a method template which wraps its argument in a lambda. When you construct a Foo, the you get the benefit of structural typing that templates bring: as long as the template argument implements a stream output operator, it’ll compile.

But through the use of a lambda, you also get the benefit of runtime polymorphism, and because Foo is a concrete type, you can put it in a data structure (like a container) without the template argument leaking out.

This seems like it could be quite useful in various scenarios. Sure, there’s a memory cost, and you could look at it as replacing a (per-type) vtable with per-instance member variables (ie. lambda functions and their associated captures). But as a way of mixing the benefits of compile-time and runtime polymorphism I think it’s pretty neat.