{"id":418,"date":"2018-07-19T18:06:31","date_gmt":"2018-07-19T18:06:31","guid":{"rendered":"https:\/\/yazilimperver.net\/?p=418"},"modified":"2018-07-26T14:30:18","modified_gmt":"2018-07-26T14:30:18","slug":"modern-c-6-lambda-ifadeleri","status":"publish","type":"post","link":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/","title":{"rendered":"[:tr]Modern C++ (6) : Lambda \u0130fadeleri[:en]Modern C++ (6) : Lambda Expressions[:]"},"content":{"rendered":"<p>[:tr]Evet arkada\u015flar, C++ yaz\u0131lar\u0131m\u0131za kald\u0131\u011f\u0131m\u0131z yerden devam ediyoruz.<\/p>\n<p>Lambda yaz\u0131m ile temel C++ 11 \u00f6zelliklerini tamamlam\u0131\u015f olaca\u011f\u0131z. Sonras\u0131nda belki STL i\u00e7in de bir yaz\u0131 yaz\u0131p, daha sonra C++ 14\/17\/20&#8217;e yelken a\u00e7abiliriz. Asl\u0131nda olduk\u00e7a geni\u015f konu, fakat ben bu yaz\u0131mda sizlere temel noktalar\u0131 aktarmaya \u00e7al\u0131\u015faca\u011f\u0131m. \u00d6ncelikle\u00a0 benim gibi bilgisayar bilimleri ile u\u011fra\u015fanlar\u0131m\u0131z Lambda denilince hemen akl\u0131n\u0131za Lambda calculus gelecektir ve bunlar aras\u0131nda bir ili\u015fki var m\u0131 diye de d\u00fc\u015f\u00fcnebilirsiniz. K\u0131sa cevap: bir Lisp kadar ilintili de\u011fil ama uzun cevap i\u00e7in a\u015fa\u011f\u0131daki sonu\u00e7lar\u0131 i\u00e7in <a href=\"https:\/\/www.google.com.tr\/search?q=lambda+calculus+vs+lambda+expressions+in+c%2B%2B&amp;rlz=1C1SQJL_trTR803TR803&amp;oq=lambda+calculus+vs+lambda+expressions+in+c%2B%2B&amp;aqs=chrome..69i57.13431j0j4&amp;sourceid=chrome&amp;ie=UTF-8\">google amcam\u0131za dan\u0131\u015fabiliriz<\/a>. Bu arada Lisp &#8220;Lambda Calculus&#8221; &#8216;\u0131n ger\u00e7ek anlamda bir ger\u00e7eklemesi ve dilin b\u00fct\u00fcn yap\u0131s\u0131 zaten jenerik programlamay\u0131 kolayla\u015ft\u0131rmaya y\u00f6nelik tasarlanm\u0131\u015ft\u0131r.<\/p>\n<p>Ayr\u0131ca &#8220;Lambda Calculus&#8221; ile ilgili de referanslar b\u00f6l\u00fcm\u00fcne bir ka\u00e7 kaynak ekliyorum [6]. Bu konuda daha derinlemesine bilgi almak isteyen arkada\u015flar\u0131m oraya y\u00f6nelebilirler.<\/p>\n<h2><strong><span style=\"color: #3366ff;\">Lambda \u0130fadeleri<\/span><\/strong><\/h2>\n<p>Gelelim as\u0131l mevzumuza <em>Lambda \u0130fadeleri<\/em>. Hepimiz yaz\u0131l\u0131m geli\u015ftirirken \u00e7e\u015fitli i\u015fler i\u00e7in k\u0131sa k\u0131sa metotlar ve fonksiyonlar yazma ihtiyac\u0131 duymu\u015fuzdur, hele de STL ile ha\u015f\u0131r ne\u015firseniz, \u00f6zellikle STL algoritma k\u00fct\u00fcphanesinde bu tarz kullan\u0131mlara \u00e7ok s\u0131k ihtiya\u00e7 duyars\u0131n\u0131z. Bunlar bir iki tane olunca s\u0131k\u0131nt\u0131 olmasa da, say\u0131lar\u0131 artt\u0131k\u00e7a hem kod bu tarz sat\u0131rlarla dolmakta, okunabilirlik azalmakta ve de bu kodlar\u0131n idamesi zorla\u015fmaktad\u0131r. \u0130\u015fte C++ 11 ile gelen Lambda ifadeleri ile art\u0131k:<br \/>\n&#8211; Kullan\u0131lacak olan fonksiyonlar ihtiya\u00e7 duyulan yerlerde tan\u0131mlayabilir,<br \/>\n&#8211; Yukar\u0131dan a\u015fa\u011f\u0131ya olan mant\u0131ksal ve do\u011fal ak\u0131\u015f\u0131 koruyabilir (fonksiyonlar genelde farkl\u0131 yerlerde tan\u0131mlan\u0131rlar, bu sebeple git geller okunabilirli\u011fi d\u00fc\u015f\u00fcrebiliyor. \u00d6zellikle tek seferlik),<br \/>\n&#8211; Mevcut kapsam i\u00e7erisindeki de\u011fi\u015fkenlere de eri\u015fim sunulabilmektedir.<\/p>\n<p>Lambda ifadelerini tan\u0131mlayacak olursak. Lambda ifadeleri k\u0131saca <em><strong>anonim fonksiyon<\/strong><\/em> olarak ifade edilebilir. Hatta baz\u0131 kaynaklar (cppreference) &#8220;Mevcut kapsamdaki de\u011fi\u015fkenleri<br \/>\nyakalayabilen isimsiz fonksiyon nesneleri olarak da tan\u0131mlar&#8221;. Bir di\u011fer deyi\u015fle, lambda ifadeleri asl\u0131nda \u00e7a\u011fr\u0131labilecek olan bir kod birimini ifade etmektedirler. C++&#8217;dan \u00f6nce asl\u0131nda Haskell, C#, Java gibi bir \u00e7ok dilde buna benzer, ihtiya\u00e7 duyulan yerde fonksiyon tan\u0131mlamas\u0131na olanak sa\u011flayan yap\u0131lar sunulmaktayd\u0131 (tam ya da daha geni\u015f bir liste i\u00e7in <a href=\"https:\/\/www.wikiwand.com\/en\/Anonymous_function\">Wikipedia<\/a> ya alal\u0131m sizi).<\/p>\n<p>Merakl\u0131lar i\u00e7in lambdalar\u0131n alt\u0131nda yatan mekanizmay\u0131 (derleyici lambda ifadelerini nas\u0131l anlamland\u0131r\u0131yor veya arkada \u00fcretilen kod nedir, performansa bir etkisi var m\u0131 vs.) yaz\u0131m\u0131n sonuna ekledi\u011fim ayr\u0131 bir b\u00f6l\u00fcmde anlataca\u011f\u0131m.<\/p>\n<p>Lambda ifadelerinin genel \u015fablonu a\u015fa\u011f\u0131daki gibi \u00f6zetlenebilir. Burada * ile i\u015faretlenen k\u0131s\u0131mlar opsiyonel olan par\u00e7alard\u0131r.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">[ Yakalama Tan\u0131mlar\u0131 ] *(Parametreler) *mutable -&gt; d\u00f6n\u00fc\u015f tipi*\r\n{\r\n     lambda i\u015flevi tan\u0131mlamas\u0131\r\n}<\/pre>\n<p>Detaylara inmeden \u00f6nce a\u015fa\u011f\u0131da basit baz\u0131 lambda tan\u0131mlamalar\u0131 ve bunlar\u0131n nas\u0131l \u00e7a\u011fr\u0131ld\u0131\u011f\u0131na ili\u015fkin bir ka\u00e7 \u00f6rnek sizlere aktaray\u0131m.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">#include &lt;iostream&gt;\r\nusing namespace std;\r\n \r\nint main()\r\n{\r\n    \/\/ Parametre almayan ve d\u00f6n\u00fc\u015f yapmayan bir lambda ifadesi\r\n    auto lambda = []() { cout &lt;&lt; \"Lambda ifadesi i\u00e7eren kod!\" &lt;&lt; endl; };\r\n    lambda();\r\n    \r\n    \/\/ Yukar\u0131daki ifade ile a\u015fa\u011f\u0131daki ifadeler tamamen ayn\u0131 i\u015fi yap\u0131yorlar\r\n    \/\/ auto lambda = [] { cout &lt;&lt; Code within a lambda expression\" &lt;&lt; endl; };\r\n    \/\/ auto lambda = [](void) { cout &lt;&lt; Code within a lambda expression\" &lt;&lt; endl; }; \r\n    \/\/ auto lambda = [](void) -&gt; void { cout &lt;&lt; \"Code within a lambda expression\" &lt;&lt; endl; };\r\n   \r\n    \/\/ \u0130ki parametre alan bir lambda ifadesi \u00f6rne\u011fi\r\n    auto sum = [](int x, int y) { return x + y; };\r\n    cout &lt;&lt; sum(5, 2) &lt;&lt; endl;\r\n    \r\n    \/\/ Ayr\u0131ca lambda ifadelerini direk \u00e7a\u011fr\u0131ld\u0131\u011f\u0131 yerde de tan\u0131mlayabilirsiniz\r\n    cout &lt;&lt; [](int x, int y) { return x + y; }(5, 2) &lt;&lt; endl;\r\n}<\/pre>\n<p>Yukar\u0131da auto de\u011fi\u015fkenleri i\u00e7erisinde lambda ifadelerine ili\u015fkin i\u015faret\u00e7iler tutulmakta. Bir de STL&#8217;e ili\u015fkin \u00f6rne\u011fe bakal\u0131m hemen. \u00d6nce STL de mevcut durumda 5 ten b\u00fcy\u00fck say\u0131lar\u0131 nas\u0131l sayd\u0131raca\u011f\u0131m\u0131za bakal\u0131m:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">#include &lt;iostream&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;vector&gt;\r\nusing namespace std;\r\n\/\/ Bunun yerine fonksiyon nesnesi de olu\u015fturabilirsiniz\r\nbool is_greater_than_5(int value)\r\n{\r\n      return (value &gt; 5);\r\n}\r\n\r\nint main()\r\n{\r\n        vector&lt;int&gt; numbers { 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 };\r\n\r\n        \/\/ Besten buyuk say\u0131lar\u0131 say\r\n        auto greater_than_5_count = count_if(numbers.begin(), numbers.end(), is_greater_than_5);\r\n\r\n        cout &lt;&lt; \"5 ten buyuk sayi adeti: \" &lt;&lt; greater_than_5_count &lt;&lt; endl;\r\n}<\/pre>\n<p>\u015eimdi de ayn\u0131 i\u015fi lambdalar ile yapal\u0131m. Siz hangisi tercih edersiniz \ud83d\ude42<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">#include &lt;iostream&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;vector&gt;\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n       vector&lt;int&gt; numbers { 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 }; \r\n\r\n       \/\/ Besten buyuk say\u0131lar\u0131 say\r\n       cout &lt;&lt; \"5 ten buyuk sayi adeti: \" &lt;&lt; count_if(numbers.begin(), numbers.end(), [](int x) { return (x &gt; 5); })  &lt;&lt; endl;\r\n}<\/pre>\n<p>\u015eimdi biraz daha derinlere inebiliriz. \u00d6nce lambdalar ile de\u011fi\u015fken yakalama mekanizmas\u0131na bakaca\u011f\u0131z daha sonra da parametre ge\u00e7irme ve d\u00f6n\u00fc\u015f mekanizmalar\u0131n\u0131 inceleyece\u011fiz. Bu arada lambda ifadelerinin arka plan\u0131 i\u00e7in ise son b\u00f6l\u00fcme ba\u015fvurabilirsiniz.<\/p>\n<h2><strong><span style=\"color: #3366ff;\">Lambda ifadeleri ile de\u011fi\u015fken yakalama (&#8220;Variable Capture Mechanism&#8221;):<\/span><\/strong><\/h2>\n<p>Normal fonksiyonlardan farkl\u0131 olarak lambda ifadeleri, mevcut tan\u0131mland\u0131\u011f\u0131 blokta\/kapsamda (&#8220;scope&#8221;) tan\u0131mlanm\u0131\u015f olan de\u011fi\u015fkenlere de eri\u015fim sa\u011flayabilmektedir. Buna <strong>&#8220;Variable Capture Mechanism&#8221;<\/strong> di\u011fer bir ifade ile <em>De\u011fi\u015fkenleri Yakalama Mekanizmas\u0131<\/em> diyebiliriz.<\/p>\n<p>Bu mekanizma ile a\u015fa\u011f\u0131daki \u015fekillerde de\u011fi\u015fkenleri yakalayabiliriz:<br \/>\n&#8211; <em>De\u011fer\/Kopya yakalama<\/em> (&#8220;Capture by value&#8221;)<br \/>\n&#8211; <em>Referans yakalama<\/em> (&#8220;Capture by reference&#8221;)<br \/>\n&#8211; <em>Hem de\u011fer hem de referans yakalama<\/em> (&#8220;Capture by both value and reference (mixed capture) &#8220;)<\/p>\n<p>Detaylar\u0131n\u0131 \u00f6rnekler ile a\u015fa\u011f\u0131da anlataca\u011f\u0131m ama \u00f6zetleme ad\u0131na bu kullan\u0131mlara ili\u015fkin yaz\u0131m \u015fekillerinin \u00f6zetini a\u015fa\u011f\u0131da bulabilirsiniz:<\/p>\n<ol>\n<li><strong>[&amp;]<\/strong> : tan\u0131mland\u0131\u011f\u0131 kapsam i\u00e7erisindeki <strong>b\u00fct\u00fcn de\u011fi\u015fkenlerin <em>referanslar\u0131<\/em> <\/strong>yakalan\u0131r\/kullan\u0131labilir (capture all external variables by reference)<\/li>\n<li><strong>[=]<\/strong> : tan\u0131mland\u0131\u011f\u0131 kapsam i\u00e7erisindeki <strong>b\u00fct\u00fcn de\u011fi\u015fkenlerin<\/strong> <em><strong>kopyalar\u0131<\/strong><\/em> yakalan\u0131r\/kullan\u0131labilr (capture all external variables by value)<\/li>\n<li><strong>[a, &amp;b]<\/strong> : <strong>a<\/strong> de\u011fi\u015fkeninin kopyas\u0131n\u0131, <strong>b<\/strong> de\u011fi\u015fkeninin de referans\u0131n\u0131 yakalar\/kullan\u0131r<\/li>\n<li><strong>[]<\/strong> : herhangi bir ortam de\u011fi\u015fkeni yakalanmaz ve sadece lambda ifadeleri i\u00e7erisindeki yerel de\u011fi\u015fkenlere eri\u015fiminiz olur<\/li>\n<\/ol>\n<p>\u015eimdi bu zamana kadar yazd\u0131klar\u0131m\u0131za ili\u015fkin yakama \u00f6rnekleri \u00fczerinden kullan\u0131mlar\u0131na bakal\u0131m.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">int x=1, y=1, z=1;\r\n\r\n\/\/ 1. Ornek\r\n[ ] ( ) { cout &lt;&lt; \u201cExample 1\u201d; } ( );\r\n\r\n\/\/ 2. Ornek\r\n[ ] ( ) { cout &lt;&lt; x; } ();\r\n\r\n\/\/ 3. Ornek\r\n[x] ( ) { cout &lt;&lt; x+1; } ();\r\n\r\n\/\/ 4. Ornek\r\n[=] ( ) { cout &lt;&lt; x &lt;&lt; y; } ();\r\n\r\n\/\/ 5. Ornek\r\nauto tmp = [ ] ( ) { cout &lt;&lt; \"merhaba\"; } ();<\/pre>\n<p>Yukar\u0131daki 1. \u00f6rnekte herhangi bir de\u011fi\u015fken yakalanmad\u0131\u011f\u0131 duruma \u00f6rnek (kullan\u0131m 4). 2. \u00f6rnek ise ge\u00e7erli bir tan\u0131mlama de\u011fil, \u00e7\u00fcnk\u00fc <strong>x<\/strong> de\u011fi\u015fkeni lambda ifadesine herhangi bir \u015fekilde ge\u00e7irilmemi\u015f. 3. \u00f6rnek bir \u00f6nceki \u00f6rne\u011fin ge\u00e7erli hali ve sadece kopyas\u0131n\u0131n ge\u00e7irildi\u011fi duruma \u00f6rnek te\u015fkil etmektedir. 4. \u00f6rnek de benzer \u015fekilde <strong>z<\/strong> d\u0131\u015f\u0131ndaki b\u00fct\u00fcn de\u011fi\u015fkenler lambda ifadesi i\u00e7erisine kopyalanarak ge\u00e7irilmekte. <strong>z<\/strong> kullan\u0131lmad\u0131\u011f\u0131 i\u00e7in ge\u00e7irilmemektedir. Son \u00f6rnek te ge\u00e7erli bir sat\u0131r de\u011fil. Neden sizce? Biraz d\u00fc\u015f\u00fcn\u00fcn sebebi yaz\u0131n\u0131n sonunda (ipucu di\u011fer sat\u0131rlardan fark\u0131 nedir &#8220;()&#8221; ne yapar).<\/p>\n<p>\u015eimdi biraz da de\u011fi\u015fkenlerin referans olarak yakalanmas\u0131na ili\u015fkin bir ka\u00e7 \u00f6rnek inceleyelim.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">int x=1, y=1, z=1;\r\n\r\n\/\/ B\u00fct\u00fcn de\u011fi\u015fkenler referans olarak ge\u00e7irilmekte\r\n[&amp;] ( ) { cout &lt;&lt; x &lt;&lt; y &lt;&lt; z; }( );\r\n\r\n\/\/ z referans olarak ge\u00e7irilmekte, di\u011ferlerinin kopyas\u0131 ge\u00e7irilmekte\r\n[=,&amp;z] ( ) { z = x + y; }( );\r\n\r\n\/\/ x 'in kopyas\u0131 ge\u00e7irilmekte, di\u011ferleri referans olarak ge\u00e7irilmekte\r\n[&amp;,x] ( ) { y = x; z = x; }( );<\/pre>\n<p>Bu kullan\u0131mlar\u0131n yan\u0131nda lambda ifadelerini s\u0131n\u0131f metotlar\u0131 i\u00e7erisinde de kullanabilirsiniz. Fakat burada bir hususa dikkat etmeniz gerekiyor. Lambdalar\u0131n her bir i\u00e7in derleyici \u00f6zel ve ayr\u0131 bir s\u0131n\u0131f olu\u015fturmakta ve bu sebeple \u00e7al\u0131\u015fma zaman\u0131nda hepsinin kendi kapsamlar\u0131 oluyor. Bir di\u011fer ifade ile s\u0131n\u0131f metotlar\u0131 i\u00e7erisindeki lambda ifadeleri i\u00e7erisinde bulunduklar\u0131 s\u0131n\u0131f de\u011fi\u015fkenlerine ula\u015famazlar.\u00a0 Bunun i\u00e7in ilgili s\u0131n\u0131f\u0131n i\u015faret\u00e7isi <em>&#8220;this&#8221;<\/em> ile ge\u00e7irilmesi gerekmektedir.\u00a0 Hemen bir \u00f6rnek ile inceleyelim:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">class Filtreleyici\r\n{\r\npublic:\r\n   Filtreleyici(vector&lt;int&gt;&amp; src) : mDataToFilter(src)\r\n   {\r\n   }\r\n   \r\n   void Filter()\r\n   {\r\n       remove_if(mDataToFilter.begin(), mDataToFilter.end(), \r\n                [this](int i){ return ( i &lt; mFilterReference); });\r\n                \/\/ Asagidaki kullanim gecerli degil\r\n                \/\/ [](int i){ return ( i &lt; mFilterReference); });\r\nprivate:\r\n    vector&lt;int&gt; mDataToFilter;\r\n    int mFilterReference;\r\n};<\/pre>\n<h2><strong><span style=\"color: #3366ff;\">Parametre Ge\u00e7irme ve D\u00f6n\u00fc\u015f De\u011ferleri:<\/span><\/strong><\/h2>\n<p>Kapsam yakalama ve de\u011fi\u015fkenlerin lambda ifadelerine ge\u00e7irilmelerinden sonra lambda ifadelerine parametrelerin ge\u00e7irilmesi hususuna e\u011filelim. Daha \u00f6nce de bahsetti\u011fim gibi lambda ifadelerine normal fonksiyonlar gibi parametreler ge\u00e7irebiliyoruz. Lambda ifadeleri i\u00e7in de normal fonksiyonlara uygulanan parametre ge\u00e7irme kurallar\u0131 uygulanmakta. C++ 14 e kadar normal fonksiyonlardan farkl\u0131 olarak varsay\u0131lan parametre de\u011ferlerini lambdalarda kullanam\u0131yoruz, C++ 14 de bu k\u0131s\u0131tlama da ortadan kalkmakta. Lambda ifadelerindeki parantezlerde asl\u0131nda iste\u011fe ba\u011fl\u0131 e\u011fer herhangi bir parametre ge\u00e7irilmiyor ise &#8220;[] {}&#8221; tamamen ge\u00e7erli bir kullan\u0131m (hatta en basit kullan\u0131m diyebiliriz).<\/p>\n<p>Lambda ifadelerinde normal fonksiyonlarda oldu\u011fu gibi de\u011ferler d\u00f6n\u00fclebilmektedir. Fonksiyonlardan farkl\u0131 olarak, lambdalarda d\u00f6n\u00fc\u015f de\u011ferleri direk ifade edilebilmeleri yan\u0131nda derleyici taraf\u0131ndan otomatik olarak da belirlenebiliyor. E\u011fer lambda tan\u0131mlamas\u0131 i\u00e7erisinde birden fazla d\u00f6n\u00fc\u015f ifadesi var ise ya da d\u00f6n\u00fc\u015f tipi \u00e7\u0131kar\u0131m\u0131nda bulunamayaca\u011f\u0131 durumlarda d\u00f6n\u00fc\u015f tipi de lambda ifadesinde belirtilmelidir.\u00a0 Ayr\u0131ca yaz\u0131lan kodta bir d\u00f6n\u00fc\u015f i\u015flemi &#8220;return deyimi&#8221; yok ise lambda i\u015flevin geri d\u00f6n\u00fc\u015f de\u011ferinin t\u00fcr\u00fc void kabul edilir.<\/p>\n<p>Not olarak lambda ifadelerinde <em><strong>throw<\/strong> <\/em>kullan\u0131m\u0131 da m\u00fcmk\u00fcn (normal C++ standard\u0131nda throw kullan\u0131m\u0131 &#8220;depreceated&#8221; olarak i\u015faretlense de dilden tamamen hen\u00fcz \u00e7\u0131kar\u0131lmad\u0131).<\/p>\n<p>\u015eimdi bu son hususlara ili\u015fkin \u00f6rnek kodlar\u0131 inceleyelim:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">\/\/ \u0130ki tane kopyalama y\u00f6ntemi kullan\u0131larak parametreler ge\u00e7iriliyor\r\n\/\/ Bu durum i\u00e7in d\u00f6n\u00fc\u015f de\u011feri belirtmeye gerek yok (int olarak derleyici kendisi bu \u00e7\u0131kar\u0131mda bulunabilir)\r\n[ ] (int x, int y) {return x + y; } (1,2);\r\n\r\n\/\/ Hatal\u0131 ifade\r\n\/\/ \u0130lk parametre referans olarak ge\u00e7irilece\u011fi ifade edilmi\u015f ve bu rvalue parametreye sabit bir de\u011fer ge\u00e7irilmi\u015f\r\n[ ] (int &amp;x, int y) {return x + y; } (1,2);\r\n\r\n\/\/ Lambda ifadesi ilk parametreyi sabit referans olarak al\u0131yor ikincinin ise kopyas\u0131n\u0131 al\u0131yor\r\n\/\/ Bu durumda da d\u00f6n\u00fc\u015f tipi belirtilmeli\r\n[ ] (const int &amp;x, int y) -&gt; int {int z = 3; return x + y + z; } (1,2);\r\n\r\n\/\/ Yerel de tan\u0131mlanm\u0131\u015f bir de\u011fi\u015fken referans olarak d\u00f6n\u00fclm\u00fc\u015f (tabi bu do\u011fru ve tercih edilen bir kullan\u0131m de\u011fil ;)\r\n[ ] (int x, int y) -&gt; int&amp; {int z = 0; return z; } (1,2);\r\n\r\nBirazda d\u00f6n\u00fc\u015f de\u011ferlerine ili\u015fkin \u00f6rnekleri inceleyelim:\r\n\/\/ A\u00e7\u0131k\u00e7a d\u00f6n\u00fc\u015f de\u011feri belirtildi\u011fi durum\r\nint p1 = [ ] (int x, int y) -&gt; int {return x+y;} (1,2);\r\n\r\n\/\/ D\u00f6n\u00fc\u015f de\u011feri belirtilmeyen durumlar. Burada derleyici tipi belirliyor (int)\r\nint p2 = [ ] (int x, int y) -&gt; {return x+y;}(1,2);\r\n\r\n\/\/ Derleme hatas\u0131. Belirtilen ve d\u00f6n\u00fclen tipler aras\u0131nda uyumsuzluk var\r\nint p4 = [ ] () -&gt; int {return false;}();\r\n\r\n\/\/ Derleme hatas\u0131. Belirtilen kodtan d\u00f6n\u00fc\u015f de\u011ferinin belirlenmesi m\u00fcmk\u00fcn de\u011fil.\r\n\/\/ A\u00e7\u0131k\u00e7a tipinin ifade edilmesi gerekiyor\r\nint p5 = [ ] (int x) { if x &gt; 5 return x; else return true;}(1);<\/pre>\n<h2><strong><span style=\"color: #3366ff;\">STL Kullan\u0131m:<\/span><\/strong><\/h2>\n<p>Yukar\u0131da asl\u0131nda STL&#8217;e kullan\u0131ma ili\u015fkin bir ka\u00e7 \u00f6rnek vermeye \u00e7al\u0131\u015ft\u0131m. A\u015fa\u011f\u0131da \u00e7ok bilindik s\u0131ralama fonksiyonu i\u00e7in olan kullan\u0131ma bakal\u0131m:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">\/\/ Klasik yakla\u015f\u0131m\r\nclass comparator\r\n{\r\n   public:\r\n     \r\n     bool operator() (int x, int y) const\r\n     {\r\n         return x &gt; y;\r\n     }\r\n};\r\n\r\nsort (v.begin(), v.end(), comparator());\r\n\r\n\/\/ Lambda kullan\u0131m\u0131 ile gelen yeni yakla\u015f\u0131m\r\nsort(v.begin(), v.end(), [ ] (int x, int y) { return x &gt; y;});\r\n<\/pre>\n<p>Bu kullan\u0131m yan\u0131nda lambda ifadeleri \u015fablonlara (&#8220;template&#8221;) parametre olarak ta ge\u00e7irilebilirler. A\u015fa\u011f\u0131da buna ili\u015fkin bir \u00f6rnek kullan\u0131m g\u00f6rebilirsiniz:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">#include &lt;set&gt;\r\n#include &lt;functional&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n    \/\/ recommended \r\n    std::set&lt; int, std::function&lt; bool(int,int) &gt; &gt; exampleSetWithLambda([] ( int x, int y ) { return x &gt; y ; } ) ; \r\n    exampleSetWithLambda.insert(2);\r\n    exampleSetWithLambda.insert(1);\r\n    exampleSetWithLambda.insert(5);\r\n    \r\n    std::cout &lt;&lt; \"Sirali:\" &lt;&lt; std::endl;\r\n    for(auto item : exampleSetWithLambda)    \r\n    {\r\n        std:: cout &lt;&lt; item &lt;&lt; \" \";\r\n    }\r\n    \r\n     std::cout&lt;&lt; std::endl;\r\n    \r\n    std::set&lt; int &gt; exampleSetWithNoLambda; \r\n    exampleSetWithNoLambda.insert(2);\r\n    exampleSetWithNoLambda.insert(1);\r\n    exampleSetWithNoLambda.insert(5);\r\n    std::cout&lt;&lt; \"Sirasiz:\" &lt;&lt; std::endl;\r\n    for(auto item : exampleSetWithNoLambda)    \r\n    {\r\n        std:: cout &lt;&lt; item &lt;&lt; \" \";\r\n    }\r\n}<\/pre>\n<p>Bunlar\u0131n yan\u0131nda std::function kullanarak lambda lari metotlara da ge\u00e7irebilirsiniz.<\/p>\n<h2><strong><span style=\"color: #3366ff;\">C++ Lambda lar\u0131n\u0131n \u00c7al\u0131\u015fma Mekanizmas\u0131<\/span><\/strong><\/h2>\n<p>Lambda&#8217;lar yaz\u0131m\u0131n ba\u015f\u0131nda da bahsetti\u011fim \u00fczere asl\u0131nda \u00e7a\u011fr\u0131labilir k\u00fc\u00e7\u00fck kod par\u00e7alar\u0131 olarak adland\u0131rabiliyor. Di\u011fer fonksiyonlardan farkl\u0131 olarak i\u00e7erisinde bulundu\u011fu kapsamda tan\u0131ml\u0131 olan de\u011fi\u015fkenlere eri\u015fim de sa\u011flayabilmekte. Peki mevcut mekanizmalardan fark\u0131 nedir? Yani normal fonksiyon, functor s\u0131n\u0131flar\u0131ndan ( operator() tan\u0131mlayan s\u0131n\u0131flar) ne fark\u0131 var? Bu ba\u015fl\u0131kta buna bakaca\u011f\u0131z.<\/p>\n<p>Basit olarak asl\u0131nda siz bir lambda tan\u0131mlamas\u0131 yapt\u0131\u011f\u0131n\u0131z zaman, derleyici arka planda sizler i\u00e7in bir &#8220;functor&#8221; s\u0131n\u0131f\u0131 olu\u015fturuyor. Bu s\u0131n\u0131flar\u0131n her biri e\u015fsiz ve her tan\u0131mlama i\u00e7in ayr\u0131 ayr\u0131 olu\u015fturuluyor. \u00d6rne\u011fin:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">[](X&amp; elem) { elem.op(); }<\/pre>\n<p>i\u00e7in derleyici:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">class _DerleyiciTarafindanAtanmisIsim_\r\n{\r\npublic:\r\nvoid operator()(X&amp; elem) const\r\n{\r\nelem.op();\r\n}\r\n};<\/pre>\n<p>tan\u0131m\u0131 olu\u015fturuyor. Bu bir anlamda &#8220;<em>syntatic sugar<\/em>&#8221; dedi\u011fimiz yani i\u015fimiz kolayla\u015ft\u0131ran bir mekanizma olarak d\u00fc\u015f\u00fcnebiliriz. \u015eimdi biraz daha derine inelim ve assembly kodlar\u0131n\u0131 kar\u015f\u0131la\u015ft\u0131ral\u0131m :).<\/p>\n<p>\u00d6nce hi\u00e7 bir de\u011fi\u015fkenin yakalanmad\u0131\u011f\u0131 duruma bakal\u0131m. Bu arada bu kodlar (<em><strong>x86-64 gcc 8.2 derleyicisinden<\/strong><\/em> \u00e7\u0131kan kodlar).<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">int function (int a)\r\n{\r\n\u00a0 \u00a0 return a + 3;\r\n}\r\n\r\nclass Functor {\r\npublic:\r\nint operator()(int a)\r\n{\r\nreturn a + 3;\r\n}\r\n};\r\n\r\nint main()\r\n{\r\nauto lambda = [] (int a) { return a + 3; };\r\n\r\nFunctor functor;\r\n\r\nvolatile int y1 = function(5);\r\nvolatile int y2 = functor(5);\r\nvolatile int y3 = lambda(5);\r\n\r\nreturn 0;\r\n}<\/pre>\n<p>Normal fonksiyon tan\u0131m\u0131 i\u00e7in a\u015fa\u011f\u0131daki gibi bir kod \u00fcretilmekte:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">traditionalFunction(int):\r\npush rbp\r\nmov rbp, rsp\r\nmov DWORD PTR [rbp-4], edi\r\nmov eax, DWORD PTR [rbp-4]\r\nadd eax, 3\r\npop rbp\r\nret<\/pre>\n<p>&#8220;Functor&#8221; i\u00e7in ise a\u015fa\u011f\u0131daki gibi kod \u00fcretilmekte:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">Functor::operator()(int):\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\nmov eax, DWORD PTR [rbp-12]\r\nadd eax, 3\r\npop rbp\r\nret<\/pre>\n<p>Lambda i\u00e7in ise a\u015fa\u011f\u0131daki gibi:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">main::{lambda(int)#1}::operator()(int) const:\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\nmov eax, DWORD PTR [rbp-12]\r\nadd eax, 3\r\npop rbp\r\nret<\/pre>\n<p>herhangi bir de\u011fi\u015fken yakalanmad\u0131\u011f\u0131 durumda g\u00f6r\u00fclece\u011fi \u00fczere &#8220;functor&#8221; ile lambda ifadelerine ili\u015fkin \u00fcretilen kod ayn\u0131. Standart metot ile ise ufak bir fark var.<\/p>\n<p>\u015eimdi ortam de\u011fi\u015fkenlerini kopyalayarak yakalama durumunu inceleyelim. Bu durumda normal metotlar\u0131 kullanamayaca\u011f\u0131z. Sadece &#8220;functor&#8221; lar ile lambdalar\u0131 kar\u015f\u0131la\u015ft\u0131raca\u011f\u0131z.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">class Functor\r\n{\r\npublic:\r\nFunctor(const int x)\r\n: m_x(x)\r\n{\r\n\r\n}\r\n\r\nint operator()(int a)\r\n{\r\nreturn a + m_x;\r\n}\r\n\r\nprivate:\r\nint m_x;\r\n};\r\n\r\nint main()\r\n{\r\nint x = 3;\r\n\r\nauto lambda = [=] (int a) { return a + x; };\r\nFunctor functor(x);\r\n\r\nvolatile int y1 = functor(5);\r\nvolatile int y2 = lambda(5);\r\n\r\nreturn 0;\r\n}<\/pre>\n<p>Burada fonksiyon nesneleri i\u00e7in bizi ilgilendiren iki metot var. Bunlar <em>yap\u0131c\u0131<\/em> ve <em>() operat\u00f6r\u00fc<\/em>. \u015eimdi bunlar i\u00e7in<br \/>\n\u00fcretilen kodlara bakal\u0131m:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">Functor::Functor(int):\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\nmov rax, QWORD PTR [rbp-8]\r\nmov edx, DWORD PTR [rbp-12]\r\nmov DWORD PTR [rax], edx\r\nnop\r\npop rbp\r\nret<\/pre>\n<p>Yap\u0131c\u0131ya ili\u015fkin kod k\u0131saca asl\u0131nda <em>esi<\/em> yazmac\u0131 i\u00e7eri\u011fini <em>rdi<\/em> yazmac\u0131 ile ifade edilen belle\u011fe kopyalamaya kar\u015f\u0131l\u0131k geliyor.<br \/>\nBuna ge\u00e7irilen de\u011ferleri g\u00f6rmek i\u00e7in de <em>main()<\/em> i\u00e7erisinde buna ili\u015fkin \u00fcretilen koda bakal\u0131m.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">\/\/ int x = 3;\r\nmov DWORD PTR [rbp-4], 3\r\n\r\n\/\/ Functor functor(x);\r\nmov edx,DWORD PTR [rbp-0x4]\r\nlea rax,[rbp-0x20]\r\nmov esi,edx\r\nmov rdi,rax\r\ncall Functor::Functor(int)<\/pre>\n<p>Buradan x&#8217;in <em>rbp-0x4<\/em> yazmac\u0131nda sakland\u0131\u011f\u0131n\u0131 ve daha sonra dolayl\u0131 yoldan <em>esi<\/em> yazmac\u0131na yaz\u0131ld\u0131\u011f\u0131n\u0131 g\u00f6r\u00fcyoruz. <em>rbp-0x20<\/em> adresini <em>rdi<\/em> at\u0131ld\u0131\u011f\u0131n\u0131 g\u00f6r\u00fcyoruz. <em>lea<\/em> komutu ve sonras\u0131ndaki sat\u0131rlar ile bizim fonksiyon nesnemizi saklayan adres.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">Functor::operator()(int):\r\n\/\/ int operator()(int a)\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\n\r\n\/\/ return a + m_x;\r\nmov rax, QWORD PTR [rbp-8]\r\nmov edx, DWORD PTR [rax]\r\nmov eax, DWORD PTR [rbp-12]\r\nadd eax, edx\r\npop rbp\r\nret\r\n<\/pre>\n<p>As\u0131l<em> () operat\u00f6r\u00fc<\/em> i\u00e7in \u00fcretilen kod ile yap\u0131c\u0131 i\u00e7in \u00fcretilen kod birbirine olduk\u00e7a benzemekte. En b\u00fcy\u00fck fark ise <strong>m_x<\/strong> in de\u011ferini okumak i\u00e7in \u00e7a\u011fr\u0131lan iki sat\u0131r komut.<\/p>\n<p>\u015eimdi lambda ifadesi i\u00e7in \u00fcretilen koda bakal\u0131m:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">main::{lambda(int)#1}::operator()(int) const:\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\nmov rax, QWORD PTR [rbp-8]\r\nmov edx, DWORD PTR [rax]\r\nmov eax, DWORD PTR [rbp-12]\r\nadd eax, edx\r\npop rbp\r\nret<\/pre>\n<p>Bu kod par\u00e7as\u0131n\u0131n da fonksiyon nesne objesi i\u00e7in \u00fcretilen ile ayn\u0131 oldu\u011fu g\u00f6r\u00fclebilir. Burada tabi lambda ifadesinin olu\u015fturulmas\u0131na ili\u015fkin kodunun nerede oldu\u011fu sorulabilir.<br \/>\nOnun i\u00e7in <em>main<\/em> i\u00e7erisine bak\u0131yoruz:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">mov eax, DWORD PTR [rbp-4]\r\nmov DWORD PTR [rbp-16], eax<\/pre>\n<p>G\u00f6r\u00fclece\u011fi \u00fczere fonksiyon nesne objesinin yap\u0131c\u0131s\u0131 i\u00e7in \u00fcretilen kod, lambda i\u00e7in \u00fcretilen kodtan olduk\u00e7a fazla. Bunun da sebebi yap\u0131c\u0131 i\u00e7in olan olu\u015fturucu kodu normal \u00fcretilen kodun i\u00e7erisine g\u00f6m\u00fclmekte. Ortam de\u011fi\u015fkenlerinin referans olarak ge\u00e7irildi\u011fi durumda asl\u0131nda yukar\u0131dakine benzer tek fark de\u011fer yerine i\u015faret\u00e7ilerin ge\u00e7irilmesi.<\/p>\n<p>Bu kullan\u0131mlara bakt\u0131\u011f\u0131m\u0131zda \u00f6zellikle fonksiyon nesneleri ile lambda ifadeleri birbirlerinin hemen hemen ayn\u0131s\u0131. Ana farkl\u0131l\u0131klar:<br \/>\n&#8211; &#8220;functor&#8221; lar ve lambda ifadeleri fazladan bir this g\u00f6stergeci ge\u00e7iriyorlar (fazladan 8 byte),<br \/>\n&#8211; Lambda ifadelerine ili\u015fkin yap\u0131c\u0131 kodlar\u0131 lambda ifadelerinin i\u00e7erisine yediriliyor ve bu sayede kopyalamaya ili\u015fkin fazladan olu\u015fturulan kod miktar\u0131 azalt\u0131lm\u0131\u015f oluyor.<\/p>\n<p>Sonu\u00e7 olarak lambda ifadelerinin performans anlam\u0131nda bir y\u00fck getirmedi\u011fini ifade edebiliriz ([8] de daha detayl\u0131 bir performans kar\u015f\u0131la\u015ft\u0131rmas\u0131 g\u00f6rebilirsiniz).<\/p>\n<p>Yine k\u0131sa dedik s\u00f6z\u00fc uzatt\u0131k \ud83d\ude42 olsun lambda ifadeleri olduk\u00e7a \u00f6nemli ve nispeten yeni bir kabiliyet, siz yaz\u0131l\u0131mperverlere faydas\u0131 olduysa ne ala \ud83d\ude42<\/p>\n<h2><strong><span style=\"color: #3366ff;\">Kaynaklar:<\/span><\/strong><\/h2>\n<ol>\n<li>https:\/\/www.cprogramming.com\/c++11\/c++11-lambda-closures.html<\/li>\n<li>https:\/\/www.geeksforgeeks.org\/lambda-expression-in-c\/<\/li>\n<li>https:\/\/web.mst.edu\/~nmjxv3\/articles\/lambdas.html<\/li>\n<li>https:\/\/blog.feabhas.com\/2014\/03\/demystifying-c-lambdas\/<\/li>\n<li>https:\/\/www.wikiwand.com\/en\/Lambda_calculus<\/li>\n<li>https:\/\/www.inf.fu-berlin.de\/lehre\/WS03\/alpi\/lambda.pdf<\/li>\n<li>https:\/\/en.cppreference.com\/w\/cpp\/language\/lambda<\/li>\n<li>https:\/\/vittorioromeo.info\/index\/blog\/passing_functions_to_functions.html<\/li>\n<\/ol>\n<p>[:en]Yes folks, we are continuing to our C++ posts where we left.<\/p>\n<p>We will complete the core C++ 11 features with this Lambda Expression post. Then I may write a post about STL features that come with C++ 11, after that we will explore the C++ 14\/17\/20.<br \/>\nIn fact, lambda expression is very wide topic in my opinion, but I will try to focus on most important aspects of lambda expression in a brief manner (well at the end of post, brief word may lost is meaning). If you are also a computer scientist as me, when I say lambda, the first thing that will bear in your mind would be the lambda calculus. You may also start to wonder if lambda calculus and lambda expressions that come with c++ 11 are same or somehow related. The short answer is: not as much as Lisp, but for more detailed answer I will kindly request you to <a href=\"https:\/\/www.google.com.tr\/search?q=lambda+calculus+vs+lambda+expressions+in+c%2B%2B&amp;rlz=1C1SQJL_trTR803TR803&amp;oq=lambda+calculus+vs+lambda+expressions+in+c%2B%2B&amp;aqs=chrome..69i57.13431j0j4&amp;sourceid=chrome&amp;ie=UTF-8\">consult google<\/a>. By the way, the Lisp is one real implementation of lambda calculus and all language structure is designed specifically to make generic programming easy.<\/p>\n<p>I am also adding a reference [6] for `Lambda Calculus` for curious Yazilimperver fellows \ud83d\ude09<\/p>\n<h2><strong><span style=\"color: #3366ff;\">Lambda Expressions<\/span><\/strong><\/h2>\n<p>Now let us start to talk about <em>Lambda Expressions<\/em>. As you know and experience, most of us needed to write short methods and functions during programming. If you are using STL library (I hope you are using it), especially, the algorithm library, you will require such short methods, functors more often. When there is one or two short method, this may not be an issue, but as the number of methods increases, the code bloated with this kind of short methods, eventually its readibility, maintainibaility decreases. With C++ 11 lambda expressions, now you can:<br \/>\n&#8211; Define anonymous methos where they required,<br \/>\n&#8211; The natural execution flow from up to down is preserved (in case of standalone functions or functors which are usually defined in different places, you may need to jump to there and jump back),<br \/>\n&#8211; You may access the variables defined in given scope.<\/p>\n<p>Let us now define the lambda expressions. Briefly, they can be defined as <strong><em>anonymous function<\/em><\/strong>. Some references (<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/lambda\">cppreference<\/a>) define them as &#8220;An unnamed function object capable of capturing variables in scope&#8221;. In other words, lambda expressions define anonymous callable function definitions. Before C++ 11, other programming languages also provide lambda expression as their language feature such as C#, Java, Haskell (for a more complete list of programming languages and corresponding structs you can consult to sir <a href=\"https:\/\/www.wikiwand.com\/en\/Anonymous_function\">Wikipedia<\/a>).<\/p>\n<p>Enthusiastic readers that are curious about underlying lambda expressions mechanism (how compiler interpret them and the code generated for lambda expressions as well as perfomance considerations), I prepared a separate section for you at the end of my post, check it out.<\/p>\n<p>General syntax of lambda expressions can be outlined as follow. Here the items that are marked as &#8220;*&#8221; are optional.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">[ Capture Statements ] *(Parameters) *mutable -&gt; Return Type*\r\n{\r\n      Lambda epxression statement\r\n}<\/pre>\n<p>Before delving into details, let us explore some simple lambda expressions and their usage through simple example codes.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n\/\/ A lambda expression with no return type and parameters\r\nauto lambda = []() { cout &lt;&lt; \"A code with lambda expression!\" &lt;&lt; endl; };\r\nlambda();\r\n\r\n\/\/ The following statements are completely same with above expression\r\n\/\/ auto lambda = [] { cout &lt;&lt; Code within a lambda expression\" &lt;&lt; endl; };\r\n\/\/ auto lambda = [](void) { cout &lt;&lt; Code within a lambda expression\" &lt;&lt; endl; }; \r\n\/\/ auto lambda = [](void) -&gt; void { cout &lt;&lt; \"Code within a lambda expression\" &lt;&lt; endl; };\r\n\r\n\/\/ A lambda expression that takes two parameters\r\nauto sum = [](int x, int y) { return x + y; };\r\ncout &lt;&lt; sum(5, 2) &lt;&lt; endl;\r\n\r\n\/\/ We can also call the lambda expression where they defined\r\ncout &lt;&lt; [](int x, int y) { return x + y; }(5, 2) &lt;&lt; endl;\r\n}<\/pre>\n<p>The above auto variables hold pointers to defined lambda expressions. Now let us look at simple STL usage example where code will count the numbers that are greater than five:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">#include &lt;iostream&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;vector&gt;\r\nusing namespace std;\r\n\r\n\/\/ We can also define a functor for this method \r\nbool is_greater_than_5(int value)\r\n{\r\n      return (value &gt; 5);\r\n}\r\n\r\nint main()\r\n{\r\n        vector&lt;int&gt; numbers { 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 };\r\n\r\n        \/\/ Count the numbers greater than 5\r\n        auto greater_than_5_count = count_if(numbers.begin(), numbers.end(), is_greater_than_5);\r\n\r\n        cout &lt;&lt; \"The count of numbers greater than 5: \" &lt;&lt; greater_than_5_count &lt;&lt; endl;\r\n}<\/pre>\n<p>Now let us to the same thing with lambda expression and see which one do you prefer \ud83d\ude42<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">#include &lt;iostream&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;vector&gt;\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n       vector&lt;int&gt; numbers { 1, 2, 3, 4, 5, 10, 15, 20, 25, 35, 45, 50 }; \r\n\r\n       \/\/ Count the numbers greater than 5\r\n       cout &lt;&lt; \"The count of numbers greater than 5: \" &lt;&lt; count_if(numbers.begin(), numbers.end(), [](int x) { return (x &gt; 5); })  &lt;&lt; endl;\r\n}<\/pre>\n<p>Now we can dive into more details. First of all, we will look at the variable capture mechanism, then we will explore the parameter passing and return mechanisms. By the way, as I said before, you can check the last section for underlying of lambda expressions.<\/p>\n<h2><strong><span style=\"color: #3366ff;\">Variable Capturing with Lambda Expressions (&#8220;Variable Capture Mechanism&#8221;):<\/span><\/strong><\/h2>\n<p>Different from traditional functions, lambda expressions can also capture\/access to variables that are defined in their scope. This is also called as &#8220;<strong>Variable Capture Mechanism<\/strong>&#8220;.<\/p>\n<p>Using this mechanism, the variables can be captured as follow (the names should be familiar to one that knows parameter passing mechanisms):<br \/>\n<em>&#8211; Capture by value<\/em><br \/>\n<em>&#8211; Capture by reference<\/em><br \/>\n<em>&#8211; Mixed capture, capture by both value and reference<\/em><\/p>\n<p>These usage of these mechanisms can be summarized as follow. We will later look into some examples about these:<\/p>\n<ol>\n<li><strong>[&amp;]<\/strong> : all variables in scope where lambda expression defined can be captured\/used by <em>reference<\/em> (capture all external variables by reference)<\/li>\n<li><strong>[=]<\/strong> : all variables in scope where lambda expression defined can be captured\/used by <em>value<\/em> (capture all external variables by value)<\/li>\n<li><strong>[a, &amp;b]<\/strong> : copy of a and reference of b are captured<\/li>\n<li><strong>[]<\/strong> : no variables can be accessed only the local variables defined in lambda expression statement can be used.<\/li>\n<\/ol>\n<p>Now let us look at some variable capture mechanism examples:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">int x=1, y=1, z=1;\r\n\r\n\/\/ Example 1\r\n[ ] ( ) { cout &lt;&lt; \u201cExample 1\u201d; } ( );\r\n\r\n\/\/ Example 2\r\n[ ] ( ) { cout &lt;&lt; x; } ();\r\n\r\n\/\/ Example 3\r\n[x] ( ) { cout &lt;&lt; x+1; } ();\r\n\r\n\/\/ Example 4\r\n[=] ( ) { cout &lt;&lt; x &lt;&lt; y; } ();\r\n\r\n\/\/ Example 5\r\nauto tmp = [ ] ( ) { cout &lt;&lt; \"Hello\"; } ();<\/pre>\n<p>Example 1 is an example of no parameter passing (Capture mechanism 4). Example 2 is not a valid definition, because x variable is not passed\/captured through parameters or capture list. Example 3 is valid version of previous example and problem is resolved by passing mentioned x variable through capture list. In example 4, all variables except z is passed to lambda definition. z is not passed because it is not used in lambda expression. Last example is also not valid. Why do you think? I will give the answer at the end of post (clue: what does () at the end does).<\/p>\n<p>Now let us see some examples for capturing variables by reference.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">int x=1, y=1, z=1;\r\n\r\n\/\/ All variables are captured by reference\r\n[&amp;] ( ) { cout &lt;&lt; x &lt;&lt; y &lt;&lt; z; }( );\r\n\r\n\/\/ z is captured by reference, other variables are passed by value\r\n[=,&amp;z] ( ) { z = x + y; }( );\r\n\r\n\/\/ x is captured by value, others are captured by reference\r\n[&amp;,x] ( ) { y = x; z = x; }( );<\/pre>\n<p>In addition to these usage examples, the lambda expressions can also be used in class method definitions. There is one important point that should be careful about. For each lambda expression, a separate and unique class is created and as a result each of lambda expression have their own scopes. In other words, lambda expressions that are defined in class members can not access the class data members that are defined. To do so, &#8220;this&#8221; keyword is used inside capture brackets. Let us see an example:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">class Filtreleyici\r\n{\r\npublic:\r\n   Filtreleyici(vector&lt;int&gt;&amp; src) : mDataToFilter(src)\r\n   {\r\n   }\r\n   \r\n   void Filter()\r\n   {\r\n       remove_if(mDataToFilter.begin(), mDataToFilter.end(), \r\n                [this](int i){ return ( i &lt; mFilterReference); });\r\n                \/\/ Asagidaki kullanim gecerli degil\r\n                \/\/ [](int i){ return ( i &lt; mFilterReference); });\r\nprivate:\r\n    vector&lt;int&gt; mDataToFilter;\r\n    int mFilterReference;\r\n};<\/pre>\n<h2><strong><span style=\"color: #3366ff;\">Passing Parameters and Return Types:<\/span><\/strong><\/h2>\n<p>In this section, we will look at parameter passing and returning value mechanisms. As I said above, parameters can be passed to lambda expression like traditional functions. The rules for function parameter passing is also same for lambda expression parameter passing. Till C++ 14 you can not use default values with lambda parameters but with C++ 14, this is also possible. The paranthesises &#8220;()&#8221; in lambda expressions are also opitonal and `[]{}` is a valid lambda expression (simplest lambda expression).<\/p>\n<p>As in traditional functions, lambda expression can also return values. Different from functions, compiler can deduce the return data type automatically for you, but of course you can explicitly define them. If there are more than one return types then you should explicitly define return type. Besides, if there is no return keyword in lambda body statement then its return type is marked as void.<\/p>\n<p>As an additional note, `throw` can also be used by lambda expressions, but is marked as depreceated so might be removed in future standards.<\/p>\n<p>Let us now examine example codes for this topic:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">\/\/ \u0130ki tane kopyalama y\u00f6ntemi kullan\u0131larak parametreler ge\u00e7iriliyor\r\n\/\/ Bu durum i\u00e7in d\u00f6n\u00fc\u015f de\u011feri belirtmeye gerek yok (int olarak derleyici kendisi bu \u00e7\u0131kar\u0131mda bulunabilir)\r\n[ ] (int x, int y) {return x + y; } (1,2);\r\n\r\n\/\/ Hatal\u0131 ifade\r\n\/\/ \u0130lk parametre referans olarak ge\u00e7irilece\u011fi ifade edilmi\u015f ve bu rvalue parametreye sabit bir de\u011fer ge\u00e7irilmi\u015f\r\n[ ] (int &amp;x, int y) {return x + y; } (1,2);\r\n\r\n\/\/ Lambda ifadesi ilk parametreyi sabit referans olarak al\u0131yor ikincinin ise kopyas\u0131n\u0131 al\u0131yor\r\n\/\/ Bu durumda da d\u00f6n\u00fc\u015f tipi belirtilmeli\r\n[ ] (const int &amp;x, int y) -&gt; int {int z = 3; return x + y + z; } (1,2);\r\n\r\n\/\/ Yerel de tan\u0131mlanm\u0131\u015f bir de\u011fi\u015fken referans olarak d\u00f6n\u00fclm\u00fc\u015f (tabi bu do\u011fru ve tercih edilen bir kullan\u0131m de\u011fil ;)\r\n[ ] (int x, int y) -&gt; int&amp; {int z = 0; return z; } (1,2);\r\n\r\nBirazda d\u00f6n\u00fc\u015f de\u011ferlerine ili\u015fkin \u00f6rnekleri inceleyelim:\r\n\/\/ A\u00e7\u0131k\u00e7a d\u00f6n\u00fc\u015f de\u011feri belirtildi\u011fi durum\r\nint p1 = [ ] (int x, int y) -&gt; int {return x+y;} (1,2);\r\n\r\n\/\/ D\u00f6n\u00fc\u015f de\u011feri belirtilmeyen durumlar. Burada derleyici tipi belirliyor (int)\r\nint p2 = [ ] (int x, int y) -&gt; {return x+y;}(1,2);\r\n\r\n\/\/ Derleme hatas\u0131. Belirtilen ve d\u00f6n\u00fclen tipler aras\u0131nda uyumsuzluk var\r\nint p4 = [ ] () -&gt; int {return false;}();\r\n\r\n\/\/ Derleme hatas\u0131. Belirtilen kodtan d\u00f6n\u00fc\u015f de\u011ferinin belirlenmesi m\u00fcmk\u00fcn de\u011fil.\r\n\/\/ A\u00e7\u0131k\u00e7a tipinin ifade edilmesi gerekiyor\r\nint p5 = [ ] (int x) { if x &gt; 5 return x; else return true;}(1);<\/pre>\n<h2><strong><span style=\"color: #3366ff;\">STL Usage:<\/span><\/strong><\/h2>\n<p>In above lines, I tried to give you some examples about STL usage. Below you can find an example lambda expression usage in STL for sorting:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">\/\/ Classic approach\r\nclass comparator\r\n{\r\n   public:\r\n     \r\n     bool operator() (int x, int y) const\r\n     {\r\n         return x &gt; y;\r\n     }\r\n};\r\n\r\nsort (v.begin(), v.end(), comparator());\r\n\r\n\/\/ New Lambda expression approach. Much cleaner!\r\nsort(v.begin(), v.end(), [ ] (int x, int y) { return x &gt; y;});\r\n<\/pre>\n<p>In addition to above usage, lambda expressions can be used as template parameter. An example is provided below:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">#include &lt;set&gt;\r\n#include &lt;functional&gt;\r\n#include &lt;iostream&gt;\r\n\r\nint main()\r\n{\r\n    \/\/ recommended \r\n    std::set&lt; int, std::function&lt; bool(int,int) &gt; &gt; exampleSetWithLambda([] ( int x, int y ) { return x &gt; y ; } ) ; \r\n    exampleSetWithLambda.insert(2);\r\n    exampleSetWithLambda.insert(1);\r\n    exampleSetWithLambda.insert(5);\r\n    \r\n    std::cout &lt;&lt; \"Ordered:\" &lt;&lt; std::endl;\r\n    for(auto item : exampleSetWithLambda)    \r\n    {\r\n        std:: cout &lt;&lt; item &lt;&lt; \" \";\r\n    }\r\n    \r\n     std::cout&lt;&lt; std::endl;\r\n    \r\n    std::set&lt; int &gt; exampleSetWithNoLambda; \r\n    exampleSetWithNoLambda.insert(2);\r\n    exampleSetWithNoLambda.insert(1);\r\n    exampleSetWithNoLambda.insert(5);\r\n    std::cout&lt;&lt; \"Unordered:\" &lt;&lt; std::endl;\r\n    for(auto item : exampleSetWithNoLambda)    \r\n    {\r\n        std:: cout &lt;&lt; item &lt;&lt; \" \";\r\n    }\r\n}<\/pre>\n<p>You can also pass lambda expressions to functions through std::function.<\/p>\n<h2><strong><span style=\"color: #3366ff;\">C++ Lambda Expression Underhood<\/span><\/strong><\/h2>\n<p>As you may understand, lambda expression are nothing but simple executable code fragments. Different from traditional functions, they can capture\/access\/cahnge the variables in their scope (Variable capture mechanism).<br \/>\nSo what is the difference? In other words, what make lambda expression different than tradition functions, functors? In this section, we will investigate this question.<\/p>\n<p>In its simplest form, when you define a lambda expression, compiler generate a functor class for you at backstage. All these classes are unique and separately generated. For instance:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">[](X&amp; elem) { elem.op(); }<\/pre>\n<p>For above line, compiler generate following class:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">class _CompilerAssignedName_\r\n{\r\npublic:\r\nvoid operator()(X&amp; elem) const\r\n{\r\n    elem.op();\r\n}\r\n};<\/pre>\n<p>From this perspective, lambda expressions can be considered as syntatic sugar for functors. Now let us dive into more deeper parts and compare the generated assembly codes :).<br \/>\nLet us first look at the case where no variable is capture. By the way, for <em><strong>assembly x86-64 gcc 8.2<\/strong><\/em> compiler is employed.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">int function (int a)\r\n{\r\n\u00a0 \u00a0 return a + 3;\r\n}\r\n\r\nclass Functor {\r\npublic:\r\nint operator()(int a)\r\n{\r\nreturn a + 3;\r\n}\r\n};\r\n\r\nint main()\r\n{\r\nauto lambda = [] (int a) { return a + 3; };\r\n\r\nFunctor functor;\r\n\r\nvolatile int y1 = function(5);\r\nvolatile int y2 = functor(5);\r\nvolatile int y3 = lambda(5);\r\n\r\nreturn 0;\r\n}<\/pre>\n<p>The generated code for tradition function definition is provided below:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">traditionalFunction(int):\r\npush rbp\r\nmov rbp, rsp\r\nmov DWORD PTR [rbp-4], edi\r\nmov eax, DWORD PTR [rbp-4]\r\nadd eax, 3\r\npop rbp\r\nret<\/pre>\n<p>The generated code for functor definition is provided below:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">Functor::operator()(int):\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\nmov eax, DWORD PTR [rbp-12]\r\nadd eax, 3\r\npop rbp\r\nret<\/pre>\n<p>And finally, the generated code for lambda expression is provided below:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true\">main::{lambda(int)#1}::operator()(int) const:\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\nmov eax, DWORD PTR [rbp-12]\r\nadd eax, 3\r\npop rbp\r\nret<\/pre>\n<p>In case of no variable capturing, the code generated for functors and lambda expressions are completely same. There are some minor difference amont traditional function call.<\/p>\n<p>Now let us explore the case where variables are captured by value. In this case, no tradition method is used as it is not possible for them to access the variables defined in given scope. Only functors will be employed for comparison.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:c++ decode:true \">class Functor\r\n{\r\npublic:\r\nFunctor(const int x)\r\n: m_x(x)\r\n{\r\n\r\n}\r\n\r\nint operator()(int a)\r\n{\r\nreturn a + m_x;\r\n}\r\n\r\nprivate:\r\nint m_x;\r\n};\r\n\r\nint main()\r\n{\r\nint x = 3;\r\n\r\nauto lambda = [=] (int a) { return a + x; };\r\nFunctor functor(x);\r\n\r\nvolatile int y1 = functor(5);\r\nvolatile int y2 = lambda(5);\r\n\r\nreturn 0;\r\n}<\/pre>\n<p>Here, two methods are important for us which are constructor and () operator. Let us explore generated code for these two methods:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true\">Functor::Functor(int):\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\nmov rax, QWORD PTR [rbp-8]\r\nmov edx, DWORD PTR [rbp-12]\r\nmov DWORD PTR [rax], edx\r\nnop\r\npop rbp\r\nret<\/pre>\n<p>Briefly, constructor code copies the content of esi register to the memory represented with rdi register.<br \/>\nTo see the variables passed, let us look at the code generated inside main() function<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">\/\/ int x = 3;\r\nmov DWORD PTR [rbp-4], 3\r\n\r\n\/\/ Functor functor(x);\r\nmov edx,DWORD PTR [rbp-0x4]\r\nlea rax,[rbp-0x20]\r\nmov esi,edx\r\nmov rdi,rax\r\ncall Functor::Functor(int)<\/pre>\n<p>This code tells us x is stored at rbp-0x4 register and finally stored at esi register and rbp-0x20 address is stored at rdi register. The lines that start with lea command is about storing our function objects.<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">Functor::operator()(int):\r\n\/\/ int operator()(int a)\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\n\r\n\/\/ return a + m_x;\r\nmov rax, QWORD PTR [rbp-8]\r\nmov edx, DWORD PTR [rax]\r\nmov eax, DWORD PTR [rbp-12]\r\nadd eax, edx\r\npop rbp\r\nret\r\n<\/pre>\n<p>The code that is generated for constructor and () operator is very similar. The most obvious difference is additional two lines that are used for reading m_x.<\/p>\n<p>Now let us look at code generated for lambda expression:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">main::{lambda(int)#1}::operator()(int) const:\r\npush rbp\r\nmov rbp, rsp\r\nmov QWORD PTR [rbp-8], rdi\r\nmov DWORD PTR [rbp-12], esi\r\nmov rax, QWORD PTR [rbp-8]\r\nmov edx, DWORD PTR [rax]\r\nmov eax, DWORD PTR [rbp-12]\r\nadd eax, edx\r\npop rbp\r\nret<\/pre>\n<p>As you can see, this generated code is same as the one generated for functors. Here, you may ask about the code used for creating lambda expression. We are looking into <em>main<\/em>() for that portion:<\/p>\n<pre class=\"top-set:false bottom-set:false lang:asm decode:true \">mov eax, DWORD PTR [rbp-4]\r\nmov DWORD PTR [rbp-16], eax<\/pre>\n<p>As it can be seen from the code portions generated, the code that is generated for functor creation is much more than one that is generated for lambda expression. Main reason for this is embedding lambda construction code directly into lambda expression code. In case of capturing by reference, instead of passing variables, pointers are captured.<\/p>\n<p>After having look at the code generated, it can be seen that generated code for lambda expression and functor is very similar. The most important differences are:<br \/>\n&#8211; functors and lambda expressions are passing an extra this pointer which is not the case for traditional functions (additional 8 byte)<br \/>\n&#8211; Lambda construction code is embedded into code generated for lambda expression statements which prevent extra copy cost and amount of code generated.<\/p>\n<p>In conclusion, we can say that lambda expressions does not impose additional performance overhead. (A more detailed performance analysis can be found in [8]).<\/p>\n<h2><strong><span style=\"color: #3366ff;\">References:<\/span><\/strong><\/h2>\n<ol>\n<li>https:\/\/www.cprogramming.com\/c++11\/c++11-lambda-closures.html<\/li>\n<li>https:\/\/www.geeksforgeeks.org\/lambda-expression-in-c\/<\/li>\n<li>https:\/\/web.mst.edu\/~nmjxv3\/articles\/lambdas.html<\/li>\n<li>https:\/\/blog.feabhas.com\/2014\/03\/demystifying-c-lambdas\/<\/li>\n<li>https:\/\/www.wikiwand.com\/en\/Lambda_calculus<\/li>\n<li>https:\/\/www.inf.fu-berlin.de\/lehre\/WS03\/alpi\/lambda.pdf<\/li>\n<li>https:\/\/en.cppreference.com\/w\/cpp\/language\/lambda<\/li>\n<li>https:\/\/vittorioromeo.info\/index\/blog\/passing_functions_to_functions.html<\/li>\n<\/ol>\n<p>[:]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[:tr]Evet arkada\u015flar, C++ yaz\u0131lar\u0131m\u0131za kald\u0131\u011f\u0131m\u0131z yerden devam ediyoruz. Lambda yaz\u0131m ile temel C++ 11 \u00f6zelliklerini tamamlam\u0131\u015f olaca\u011f\u0131z. Sonras\u0131nda belki STL i\u00e7in de bir yaz\u0131 yaz\u0131p, daha sonra C++ 14\/17\/20&#8217;e yelken a\u00e7abiliriz. Asl\u0131nda olduk\u00e7a geni\u015f konu, fakat ben bu yaz\u0131mda sizlere temel noktalar\u0131 aktarmaya \u00e7al\u0131\u015faca\u011f\u0131m. \u00d6ncelikle\u00a0 benim gibi bilgisayar bilimleri ile u\u011fra\u015fanlar\u0131m\u0131z Lambda denilince hemen akl\u0131n\u0131za&#8230; <a class=\"more-link\" href=\"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/\">Continue reading <span class=\"meta-nav\">&#8594;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":174,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"dsgo_overlay_header":false,"dsgo_overlay_header_text_color":"","dsgo_overlay_skip_top_bar":false,"_designsetgo_exclude_llms":false,"footnotes":""},"categories":[10,41],"tags":[13,47,46,139,42,48],"class_list":["post-418","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","category-modern-c","tag-c","tag-c-0x","tag-c-11","tag-lambda","tag-modern-c","tag-stl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[:tr]Modern C++ (6) : Lambda \u0130fadeleri[:en]Modern C++ (6) : Lambda Expressions[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131<\/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:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[:tr]Modern C++ (6) : Lambda \u0130fadeleri[:en]Modern C++ (6) : Lambda Expressions[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131\" \/>\n<meta property=\"og:description\" content=\"[:tr]Evet arkada\u015flar, C++ yaz\u0131lar\u0131m\u0131za kald\u0131\u011f\u0131m\u0131z yerden devam ediyoruz. Lambda yaz\u0131m ile temel C++ 11 \u00f6zelliklerini tamamlam\u0131\u015f olaca\u011f\u0131z. Sonras\u0131nda belki STL i\u00e7in de bir yaz\u0131 yaz\u0131p, daha sonra C++ 14\/17\/20&#8217;e yelken a\u00e7abiliriz. Asl\u0131nda olduk\u00e7a geni\u015f konu, fakat ben bu yaz\u0131mda sizlere temel noktalar\u0131 aktarmaya \u00e7al\u0131\u015faca\u011f\u0131m. \u00d6ncelikle\u00a0 benim gibi bilgisayar bilimleri ile u\u011fra\u015fanlar\u0131m\u0131z Lambda denilince hemen akl\u0131n\u0131za... Continue reading &#8594;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/\" \/>\n<meta property=\"og:site_name\" content=\"Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-19T18:06:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-07-26T14:30:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png\" \/>\n\t<meta property=\"og:image:width\" content=\"32\" \/>\n\t<meta property=\"og:image:height\" content=\"32\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"yaz\u0131l\u0131mperver\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Yazan:\" \/>\n\t<meta name=\"twitter:data1\" content=\"yaz\u0131l\u0131mperver\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data2\" content=\"31 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/\"},\"author\":{\"name\":\"yaz\u0131l\u0131mperver\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#\\\/schema\\\/person\\\/534a27860d56170658018ac4c6f1af16\"},\"headline\":\"[:tr]Modern C++ (6) : Lambda \u0130fadeleri[:en]Modern C++ (6) : Lambda Expressions[:]\",\"datePublished\":\"2018-07-19T18:06:31+00:00\",\"dateModified\":\"2018-07-26T14:30:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/\"},\"wordCount\":3906,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#\\\/schema\\\/person\\\/534a27860d56170658018ac4c6f1af16\"},\"image\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/FeaturedImage-e1580417052514.png\",\"keywords\":[\"c++\",\"C++ 0x\",\"C++ 11\",\"lambda\",\"Modern C++\",\"STL\"],\"articleSection\":[\"C++\",\"Modern C++\"],\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/\",\"url\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/\",\"name\":\"[:tr]Modern C++ (6) : Lambda \u0130fadeleri[:en]Modern C++ (6) : Lambda Expressions[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/FeaturedImage-e1580417052514.png\",\"datePublished\":\"2018-07-19T18:06:31+00:00\",\"dateModified\":\"2018-07-26T14:30:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"tr\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/#primaryimage\",\"url\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/FeaturedImage-e1580417052514.png\",\"contentUrl\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2017\\\/12\\\/FeaturedImage-e1580417052514.png\",\"width\":32,\"height\":32},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/index.php\\\/2018\\\/07\\\/19\\\/modern-c-6-lambda-ifadeleri\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\\\/\\\/yazilimperver.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Modern C++ (6) : Lambda \u0130fadeleri\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#website\",\"url\":\"https:\\\/\\\/yazilimperver.net\\\/\",\"name\":\"Yaz\u0131l\u0131mperver'in D\u00fcnyas\u0131\",\"description\":\"Payla\u015fmak g\u00fczeldir.\",\"publisher\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#\\\/schema\\\/person\\\/534a27860d56170658018ac4c6f1af16\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/yazilimperver.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"tr\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/#\\\/schema\\\/person\\\/534a27860d56170658018ac4c6f1af16\",\"name\":\"yaz\u0131l\u0131mperver\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"tr\",\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/cropped-OnlyIcon-1.png\",\"url\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/cropped-OnlyIcon-1.png\",\"contentUrl\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/cropped-OnlyIcon-1.png\",\"width\":512,\"height\":512,\"caption\":\"yaz\u0131l\u0131mperver\"},\"logo\":{\"@id\":\"https:\\\/\\\/yazilimperver.net\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/cropped-OnlyIcon-1.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[:tr]Modern C++ (6) : Lambda \u0130fadeleri[:en]Modern C++ (6) : Lambda Expressions[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131","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:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/","og_locale":"tr_TR","og_type":"article","og_title":"[:tr]Modern C++ (6) : Lambda \u0130fadeleri[:en]Modern C++ (6) : Lambda Expressions[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131","og_description":"[:tr]Evet arkada\u015flar, C++ yaz\u0131lar\u0131m\u0131za kald\u0131\u011f\u0131m\u0131z yerden devam ediyoruz. Lambda yaz\u0131m ile temel C++ 11 \u00f6zelliklerini tamamlam\u0131\u015f olaca\u011f\u0131z. Sonras\u0131nda belki STL i\u00e7in de bir yaz\u0131 yaz\u0131p, daha sonra C++ 14\/17\/20&#8217;e yelken a\u00e7abiliriz. Asl\u0131nda olduk\u00e7a geni\u015f konu, fakat ben bu yaz\u0131mda sizlere temel noktalar\u0131 aktarmaya \u00e7al\u0131\u015faca\u011f\u0131m. \u00d6ncelikle\u00a0 benim gibi bilgisayar bilimleri ile u\u011fra\u015fanlar\u0131m\u0131z Lambda denilince hemen akl\u0131n\u0131za... Continue reading &#8594;","og_url":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/","og_site_name":"Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131","article_published_time":"2018-07-19T18:06:31+00:00","article_modified_time":"2018-07-26T14:30:18+00:00","og_image":[{"width":32,"height":32,"url":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","type":"image\/png"}],"author":"yaz\u0131l\u0131mperver","twitter_card":"summary_large_image","twitter_misc":{"Yazan:":"yaz\u0131l\u0131mperver","Tahmini okuma s\u00fcresi":"31 dakika"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/#article","isPartOf":{"@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/"},"author":{"name":"yaz\u0131l\u0131mperver","@id":"https:\/\/yazilimperver.net\/#\/schema\/person\/534a27860d56170658018ac4c6f1af16"},"headline":"[:tr]Modern C++ (6) : Lambda \u0130fadeleri[:en]Modern C++ (6) : Lambda Expressions[:]","datePublished":"2018-07-19T18:06:31+00:00","dateModified":"2018-07-26T14:30:18+00:00","mainEntityOfPage":{"@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/"},"wordCount":3906,"commentCount":4,"publisher":{"@id":"https:\/\/yazilimperver.net\/#\/schema\/person\/534a27860d56170658018ac4c6f1af16"},"image":{"@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/#primaryimage"},"thumbnailUrl":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","keywords":["c++","C++ 0x","C++ 11","lambda","Modern C++","STL"],"articleSection":["C++","Modern C++"],"inLanguage":"tr","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/","url":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/","name":"[:tr]Modern C++ (6) : Lambda \u0130fadeleri[:en]Modern C++ (6) : Lambda Expressions[:] - Yaz\u0131l\u0131mperver&#039;in D\u00fcnyas\u0131","isPartOf":{"@id":"https:\/\/yazilimperver.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/#primaryimage"},"image":{"@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/#primaryimage"},"thumbnailUrl":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","datePublished":"2018-07-19T18:06:31+00:00","dateModified":"2018-07-26T14:30:18+00:00","breadcrumb":{"@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/#breadcrumb"},"inLanguage":"tr","potentialAction":[{"@type":"ReadAction","target":["https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/"]}]},{"@type":"ImageObject","inLanguage":"tr","@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/#primaryimage","url":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","contentUrl":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2017\/12\/FeaturedImage-e1580417052514.png","width":32,"height":32},{"@type":"BreadcrumbList","@id":"https:\/\/yazilimperver.net\/index.php\/2018\/07\/19\/modern-c-6-lambda-ifadeleri\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Ana sayfa","item":"https:\/\/yazilimperver.net\/"},{"@type":"ListItem","position":2,"name":"Modern C++ (6) : Lambda \u0130fadeleri"}]},{"@type":"WebSite","@id":"https:\/\/yazilimperver.net\/#website","url":"https:\/\/yazilimperver.net\/","name":"Yaz\u0131l\u0131mperver'in D\u00fcnyas\u0131","description":"Payla\u015fmak g\u00fczeldir.","publisher":{"@id":"https:\/\/yazilimperver.net\/#\/schema\/person\/534a27860d56170658018ac4c6f1af16"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/yazilimperver.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"tr"},{"@type":["Person","Organization"],"@id":"https:\/\/yazilimperver.net\/#\/schema\/person\/534a27860d56170658018ac4c6f1af16","name":"yaz\u0131l\u0131mperver","image":{"@type":"ImageObject","inLanguage":"tr","@id":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2018\/04\/cropped-OnlyIcon-1.png","url":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2018\/04\/cropped-OnlyIcon-1.png","contentUrl":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2018\/04\/cropped-OnlyIcon-1.png","width":512,"height":512,"caption":"yaz\u0131l\u0131mperver"},"logo":{"@id":"https:\/\/yazilimperver.net\/wp-content\/uploads\/2018\/04\/cropped-OnlyIcon-1.png"}}]}},"_links":{"self":[{"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/posts\/418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/comments?post=418"}],"version-history":[{"count":0,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/posts\/418\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/media\/174"}],"wp:attachment":[{"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/categories?post=418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yazilimperver.net\/index.php\/wp-json\/wp\/v2\/tags?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}