{"id":886,"date":"2013-08-26T22:26:55","date_gmt":"2013-08-27T05:26:55","guid":{"rendered":"http:\/\/www.elbeno.com\/blog\/?p=886"},"modified":"2015-06-30T21:18:36","modified_gmt":"2015-07-01T04:18:36","slug":"an-interesting-c-technique","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/blog\/?p=886","title":{"rendered":"An Interesting C++ Technique"},"content":{"rendered":"<p>I recently discovered a C++ technique I haven&#8217;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:<\/p>\n<pre lang=\"cpp\">#include <functional>\r\n#include <iostream>\r\nusing namespace std;\r\n\r\n\/\/---------------------------------------------------------------\r\nstruct Foo\r\n{\r\n  template <typename T>\r\n  Foo(const T& t)\r\n  {\r\n    m_outputFunc = [=] (ostream& s) -> ostream& { return s << t; };\r\n  };\r\n\r\n  function<ostream&#038; (ostream&#038;)> m_outputFunc;\r\n};\r\n\r\nostream& operator<<(ostream&#038; s, const Foo&#038; f)\r\n{\r\n  return f.m_outputFunc(s);\r\n}\r\n\r\n\/\/---------------------------------------------------------------\r\nint main(int argc, char *argv[])\r\n{\r\n  Foo f(1);\r\n  cout << f;\r\n\r\n  return 0;\r\n}<\/pre>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently discovered a C++ technique I haven&#8217;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; \/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; struct Foo { template Foo(const&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,8],"tags":[],"class_list":["post-886","post","type-post","status-publish","format-standard","hentry","category-cpp","category-programming"],"_links":{"self":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=886"}],"version-history":[{"count":6,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/886\/revisions"}],"predecessor-version":[{"id":892,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/886\/revisions\/892"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}