{"id":2159,"date":"2020-11-15T20:20:04","date_gmt":"2020-11-15T19:20:04","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=2159"},"modified":"2023-06-30T12:04:45","modified_gmt":"2023-06-30T10:04:45","slug":"c-lambdas-in-arduino","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/","title":{"rendered":"C++ Lambdas in Arduino"},"content":{"rendered":"<p>I had the need to pass some <strong>callback functions<\/strong> to some of my generic C++ Arduino classes. So I was asking myself if we can use <strong>Lambdas in Arduino<\/strong>.<\/p>\n<p>Here is a small test sketch, to verify this.<\/p>\n<pre><code>#include \"TestLambda.h\"\n\nvoid setup() {\n    auto f1 = [](int number) { Serial.println(number); };\n    LambdaTest t(f1);\n\n    t.callLambda(123);\n}\n\nvoid loop() {\n}\n\n<\/code><\/pre>\n<p>And here is the related TestLambda.h content:<\/p>\n<pre><code>#include &lt;functional&gt;\n\nclass LambdaTest {\n  public:\n\n  LambdaTest(std::function&lt;void (int n)&gt; lambda){\n    lambda_ptr = &amp;lambda ;\n  }\n\n  void callLambda(int value) {\n    (*lambda_ptr)(value);\n  }\n\n  protected:\n    std::function&lt;void (int n)&gt; *lambda_ptr;\n\n};\n<\/code><\/pre>\n<p>This example compiled just fine for a <strong>ESP32<\/strong> &#8211; but when I tried to compile for a <strong>Arduino Nano<\/strong>, I was getting the error <strong>TestLambda.h:1:10: error: functional: No such file or directory<\/strong>. As a conclusion <strong>we can&#8217;t use std::function on Arduino Boards<\/strong>!<\/p>\n<p>The following alternative however was compiling w\/o issue:<\/p>\n<pre><code>typedef void (*func)(int n);\n\nclass LambdaTest {\n  public:\n\n  LambdaTest(func lambda){\n    lambda_func = lambda ;\n  }\n\n  void callLambda(int value) {\n    (*lambda_func)(value);\n  }\n\n  protected:\n    func lambda_func;\n\n};\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I had the need to pass some callback functions to some of my generic C++ Arduino classes. So I was asking myself if we can use Lambdas in Arduino. Here is a small test sketch, to verify this. #include &#8220;TestLambda.h&#8221; void setup() { auto f1 = [](int number) { Serial.println(number); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[20],"tags":[30,44],"class_list":["post-2159","post","type-post","status-publish","format-standard","hentry","category-arduino","tag-back-to-basics","tag-uno"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ Lambdas in Arduino - Phil Schatzmann<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Lambdas in Arduino - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"I had the need to pass some callback functions to some of my generic C++ Arduino classes. So I was asking myself if we can use Lambdas in Arduino. Here is a small test sketch, to verify this. #include &quot;TestLambda.h&quot; void setup() { auto f1 = [](int number) { Serial.println(number); [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-15T19:20:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-30T10:04:45+00:00\" \/>\n<meta name=\"author\" content=\"pschatzmann\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pschatzmann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/11\\\/15\\\/c-lambdas-in-arduino\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/11\\\/15\\\/c-lambdas-in-arduino\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"C++ Lambdas in Arduino\",\"datePublished\":\"2020-11-15T19:20:04+00:00\",\"dateModified\":\"2023-06-30T10:04:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/11\\\/15\\\/c-lambdas-in-arduino\\\/\"},\"wordCount\":104,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"keywords\":[\"Back to Basics\",\"UNO\"],\"articleSection\":[\"Arduino\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/11\\\/15\\\/c-lambdas-in-arduino\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/11\\\/15\\\/c-lambdas-in-arduino\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/11\\\/15\\\/c-lambdas-in-arduino\\\/\",\"name\":\"C++ Lambdas in Arduino - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"datePublished\":\"2020-11-15T19:20:04+00:00\",\"dateModified\":\"2023-06-30T10:04:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/11\\\/15\\\/c-lambdas-in-arduino\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/11\\\/15\\\/c-lambdas-in-arduino\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2020\\\/11\\\/15\\\/c-lambdas-in-arduino\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Lambdas in Arduino\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\",\"name\":\"Phil Schatzmann Consulting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\",\"name\":\"pschatzmann\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"width\":305,\"height\":305,\"caption\":\"pschatzmann\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Lambdas in Arduino - Phil Schatzmann","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/","og_locale":"en_US","og_type":"article","og_title":"C++ Lambdas in Arduino - Phil Schatzmann","og_description":"I had the need to pass some callback functions to some of my generic C++ Arduino classes. So I was asking myself if we can use Lambdas in Arduino. Here is a small test sketch, to verify this. #include \"TestLambda.h\" void setup() { auto f1 = [](int number) { Serial.println(number); [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/","og_site_name":"Phil Schatzmann","article_published_time":"2020-11-15T19:20:04+00:00","article_modified_time":"2023-06-30T10:04:45+00:00","author":"pschatzmann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pschatzmann","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"C++ Lambdas in Arduino","datePublished":"2020-11-15T19:20:04+00:00","dateModified":"2023-06-30T10:04:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/"},"wordCount":104,"commentCount":0,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"keywords":["Back to Basics","UNO"],"articleSection":["Arduino"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/","url":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/","name":"C++ Lambdas in Arduino - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"datePublished":"2020-11-15T19:20:04+00:00","dateModified":"2023-06-30T10:04:45+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2020\/11\/15\/c-lambdas-in-arduino\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"C++ Lambdas in Arduino"}]},{"@type":"WebSite","@id":"https:\/\/www.pschatzmann.ch\/home\/#website","url":"https:\/\/www.pschatzmann.ch\/home\/","name":"Phil Schatzmann Consulting","description":"","publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pschatzmann.ch\/home\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1","name":"pschatzmann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","width":305,"height":305,"caption":"pschatzmann"},"logo":{"@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png"}}]}},"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/2159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/comments?post=2159"}],"version-history":[{"count":15,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/2159\/revisions"}],"predecessor-version":[{"id":2161,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/2159\/revisions\/2161"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=2159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=2159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=2159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}