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

C++23’s new function syntax

5 September, 20225 September, 2022

We’ve had a couple of ways to spell functions for a long time: And we’ve had a shortcut for that second one for a while, too: (Aside: notice where [[nodiscard]] is positioned, since C++23’s P2173, to apply to the lambda’s call operator.) All these ways to spell functions look the…

Read More

How to print anything in C++ (part 1)

1 February, 201530 June, 2015

Part 1 Part 2 Part 3 Part 4 Part 5 Postscript I thought I’d have a go at writing some code that could print things. A pretty-printer, if you like. What I want to be able to do is this: // Print x correctly, where x is ANY type. cout

Read More

A Crossword for CppCon 2025 – Solutions

22 September, 202522 September, 2025

See https://www.elbeno.com/blog/?p=1795 (or https://crosshare.org/crosswords/aZ0lpCTDSztWEsvvuboa/cppcon-2025) if you want to try it. If you’re here for solutions, read on. Definitions are given in italics. My personal likes are 4D and 13D. 1A BABBAGE Supergroup among leaders to back grant endowment for computing pioneer (7) ABBA (“supergroup”) inserted into (“among”) BGE (“leaders to…

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