Skip to content
Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

An Interesting C++ Technique

elbeno, 26 August, 201330 June, 2015

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 
#include 
using namespace std;

//---------------------------------------------------------------
struct Foo
{
  template 
  Foo(const T& t)
  {
    m_outputFunc = [=] (ostream& s) -> ostream& { return s << t; };
  };

  function 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.

C++ Programming

Post navigation

Previous post
Next post

Related Posts

CHRONO + RANDOM = ?

24 October, 201624 October, 2016

Being a quick sketch combining <chrono> and <random> functionality, with cryptarithmetic interludes… At CppCon this year there were several good talks about randomness and time calculations in C++. On randomness: Walter Brown’s What C++ Programmers Need to Know About Header <random> and Cheinan Marks’ I Just Wanted a Random Integer!…

Read More

And it was done

1 March, 2008

Since I already had the binary search and interpolation code, it was just a matter of writing different samplers for ellipses and Bézier curves. ;; make a sampler function for a bezier curve (defun make-bezier-sampler (p0 p1 p2 p3) (lambda (k) (decasteljau p0 p1 p2 p3 k))) ;; make a…

Read More

I wrote my first Python program

20 January, 200920 January, 2009

Last Friday. Knowing almost no Python at noon, by 5pm I had some code to munge XML and do something useful for my current project. So it’s not bad. It’s good for productivity. Mostly because: It has useful libraries. Bread-and-butter data structures are built in, i.e. lists and dictionaries. I…

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

©2026 Why is a raven like a writing desk? | WordPress Theme by SuperbThemes