{"id":1816,"date":"2014-06-10T09:59:25","date_gmt":"2014-06-10T08:59:25","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1816"},"modified":"2014-06-10T10:02:45","modified_gmt":"2014-06-10T09:02:45","slug":"auto-and-lambda","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2014\/06\/10\/auto-and-lambda\/","title":{"rendered":"auto and lambda"},"content":{"rendered":"<p>The C++ 11 standards are starting to take more and more cue&#8217;s from other languages where you are able to create functions on the fly (lambda).  With also adding the &#8220;auto&#8221; keyword which will allow the program to automatically figure out what type of variable will be returned from lambda function.<\/p>\n<p>So here the break up of the lambda function is<\/p>\n<pre lang=\"cpp\">\r\n<return type> <[optional local variables to be used]>(optional parameters);\r\n<\/pre>\n<p>The optional local variables are abit like the &#8220;use&#8221; within PHP (<a href=\"http:\/\/www.codingfriends.com\/index.php\/2014\/04\/28\/lambda\/\">use<\/a>) <\/p>\n<p>So the full code is<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <vector>\r\n#include <algorithm>\r\n\r\nusing namespace std;\r\n\r\nint main(int argc, char* argv[])\r\n{\r\n\tint num = 30;\r\n\tint anothernum = 0;\r\n\r\n\tauto func = [&anothernum](int i) {\r\n\t\t\tcout << \"Parameter passed in :\" << i << \"\\n\";\r\n\t\t\tanothernum = anothernum + 10; \/\/ alter the value passed in.\r\n\t\t\treturn 50;\t\/\/ return a value and use the auto keyword.\r\n\t};\r\n\tint ret = func(num);\r\n\tcout << \"Return from func :\" << ret << \"\\n\";\r\n\tcout << \"Another num :\" << anothernum << \"\\n\";\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>and the output would be <\/p>\n<pre lang=\"bash\">\r\nParameter passed in : 30\r\nReturn from func : 50\r\nAnother num : 10;\r\n<\/pre>\n<p>To compile with g++ I used these parameters<\/p>\n<pre lang=\"bash\">\r\ng++ <filename> -o <outfilename> -std=c++11\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The C++ 11 standards are starting to take more and more cue&#8217;s from other languages where you are able to create functions on the fly (lambda). With also adding the &#8220;auto&#8221; keyword which will allow the program to automatically figure out what type of variable will be returned from lambda function. So here the break &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2014\/06\/10\/auto-and-lambda\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">auto and lambda<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[394],"class_list":["post-1816","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-auto"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1816"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1816\/revisions"}],"predecessor-version":[{"id":1820,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1816\/revisions\/1820"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}