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

The C++ <random> Lame List

7 December, 2015

Network programmers of a certain age may remember the Windows Sockets Lame List. I previously wrote a short “don’t-do-that-do-this” guide for modern C++ randomness, and I was recently reading another Reddit exchange featuring STL, author of many parts of Microsoft’s STL implementation, when it struck me that use of C++…

Read More

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

Clang/GCC weirdness

5 February, 201530 June, 2015

Consider the following code: #include #include using namespace std; // base template template struct what_type { void operator()() { cout

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