{"id":5444,"date":"2026-04-25T13:20:35","date_gmt":"2026-04-25T04:20:35","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5444"},"modified":"2026-04-25T13:26:57","modified_gmt":"2026-04-25T04:26:57","slug":"basic-lambda","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/04\/25\/basic-lambda\/","title":{"rendered":"[Basic]\ub78c\ub2e4\ud568\uc218\/Lambda Function"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb  \ub2e4\uc74c\ucf54\ub4dc\ucc98\ub7fc \ub78c\ub2e4\ud568\uc218\ub780 \uc774\ub984\uc5c6\ub294 \uc775\uba85\ud568\uc218\ub97c \uc758\ubbf8\ud569\ub2c8\ub2e4.<br>As shown in the following code, a lambda function refers to an unnamed anonymous function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;&amp;](const std::string&amp; req) -&gt; std::string {\n        return router(req);\n    }<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc704\uc640 \uac19\uc740 \ub78c\ub2e4\ud568\uc218\ub97c \ud30c\ub77c\uba54\ud130\ub85c \ub2e4\ub978 \ud568\uc218\uc5d0 \uc804\ub2ec \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774\uac83\uc744 \ucf5c\ubc31\ud568\uc218\ub77c\uace0 \ud569\ub2c8\ub2e4.<br>You can pass a lambda function like the one above as a parameter to another function. This is called a callback function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    my_sv.set_handler(&#91;&amp;](const std::string&amp; req) -&gt; std::string {\n        return router(req);\n    });<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ub78c\ub2e4\ud568\uc218\uc5d0\uc11c [&amp;] \uc774\uac83\uc744 \ucea1\ucdb0\ub9ac\uc2a4\ud2b8\ub77c\uace0 \ud569\ub2c8\ub2e4.<br>In lambda functions, [&amp;] is called a capture list.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucea1\ucdb0 \ub9ac\uc2a4\ud2b8\uc5d0\ub294 \ub2e4\uc74c\uacfc \uac19\uc740 \uc885\ub958\uac00 \uc788\uc2b5\ub2c8\ub2e4.<br>The capture list includes the following types.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\uad6c\ubb38\/Syntax<\/th><th class=\"has-text-align-left\" data-align=\"left\">\uc758\ubbf8\/<strong>Meaning<\/strong><\/th><th class=\"has-text-align-left\" data-align=\"left\">\ud2b9\uc9d5\/Features<\/th><th class=\"has-text-align-left\" data-align=\"left\">\uc0ac\uc6a9 \uc608\uc2dc\/Usage Examples<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>[]<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\uc544\ubb34\uac83\ub3c4 \ucea1\ucc98\ud558\uc9c0 \uc54a\uc74c \/ Capturing nothing<\/td><td class=\"has-text-align-left\" data-align=\"left\">\ucea1\ucc98\ud560 \uc678\ubd80 \ubcc0\uc218\uac00 \uc5c6\uc744 \ub54c \uc0ac\uc6a9 \/ <br>Used when there are no external variables to capture.<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>[](){ ... }<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>[var1, var2]<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\ud2b9\uc815 \ubcc0\uc218\ub9cc \uba85\uc2dc\uc801 \ucea1\ucc98 \/ Explicitly capture only specific variables<\/td><td class=\"has-text-align-left\" data-align=\"left\">\ud544\uc694\ud55c \ubcc0\uc218\ub9cc \uace8\ub77c \uac00\uc838\uc62c \ub54c \/ <br>When selecting and retrieving only the necessary variables<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>[i, j]()<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>[=]<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\ubaa8\ub4e0 \uc678\ubd80 \ubcc0\uc218\ub97c \uac12\uc73c\ub85c \ubcf5\uc0ac \/ Copy all external variables by value<\/td><td class=\"has-text-align-left\" data-align=\"left\">\ud604\uc7ac \ubc94\uc704\uc758 \ubaa8\ub4e0 \ubcc0\uc218\ub97c&nbsp;\uac12\uc73c\ub85c \ubcf5\uc0ac\ud558\uc5ec \uc0ac\uc6a9\ud569\ub2c8\ub2e4. \/ <br>Copy all variables in the current range by value and use them.<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>[=]()<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>[&amp;]<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\ubaa8\ub4e0 \uc678\ubd80 \ubcc0\uc218\ub97c \ucc38\uc870\ub85c \uc0ac\uc6a9 \/ Use all external variables by reference<\/td><td class=\"has-text-align-left\" data-align=\"left\">\ud604\uc7ac \ubc94\uc704\uc758 \ubaa8\ub4e0 \ubcc0\uc218\ub97c&nbsp;\ucc38\uc870\ub85c \uc0ac\uc6a9\ud569\ub2c8\ub2e4. \/ <br>Uses all variables in the current range as references.<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>[&amp;]()<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>[&amp;var]<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\ud2b9\uc815 \ubcc0\uc218\ub97c \ucc38\uc870\ub85c \ucea1\ucc98 \/ Capturing a specific variable by reference<\/td><td class=\"has-text-align-left\" data-align=\"left\">\uc6d0\ud558\ub294 \ubcc0\uc218\ub9cc \uc9c0\uc815\ud558\uc5ec \ucc38\uc870\ub85c \uc0ac\uc6a9\ud560 \ub54c \/ <br>When using references by specifying only the desired variables<\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>[&amp;i]()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucea1\ucdb0 \ub9ac\uc2a4\ud2b8\uc758 \ubc94\uc704\ub294 main()\ud568\uc218 \uc804\uccb4 \ubc94\uc704 \uc785\ub2c8\ub2e4.<br>The range of the capture list is the entire range of the main() function.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb -&gt; std::string \uc774 \ubd80\ubd84\uc740 \ubc18\ud658\ub418\ub294 \uac12\uc758 \ud0c0\uc785\uc785\ub2c8\ub2e4.<br>-&gt; std::string This part is the type of the returned value.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\ucf54\ub4dc \/ Code<\/p>\n\n\n\n<p>\u2714\ufe0f \uc544\ub798\ub294 \ub78c\ub2e4\ud568\uc218\uc640 \ucf5c\ubc31\ud568\uc218 \ud328\ud134\uc744 \uc0ac\uc6a9\ud574\uc11c \uc790\uc8fc \uad6c\ud604\ub418\ub294 \ucf54\ub4dc \ud328\ud134\uc911\uc758 \ud558\ub098\uc785\ub2c8\ub2e4.<br>Below is one of the code patterns frequently implemented using lambda functions and callback function patterns.<\/p>\n\n\n\n<p>\u2714\ufe0f main.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;functional&gt;\n#include &lt;vector&gt;\n\nclass Server {\npublic:\n    \/\/ \ub85c\uc9c1\uc744 \ub2f4\uc744 \ud568\uc218 \uc800\uc7a5\uc18c (\ub78c\ub2e4\ub97c \ub2f4\ub294 \uadf8\ub987)\n    \/\/ Function repository to hold logic (container for Lambda)\n    std::function&lt;std::string(const std::string&amp;)&gt; logic_handler;\n\n    \/\/ \ud578\ub4e4\ub7ec \uc124\uc815 \uba54\uc11c\ub4dc(logic_handler\uc5d0\uc11c \uc678\ubd80 \ud568\uc218 handler \uc5f0\uacb0)\n    \/\/ handler\ud568\uc218\uc5d0\ub294 \uc775\uba85\ud568\uc218\ub85c\uc368 \ubb38\uc790\uc5f4 \uc785\ub825\ubc1b\uace0 router\ud568\uc218 \ub9ac\ud134\n    \/\/ Handler configuration method (connecting the external function handler in logic_handler)\n    \/\/ The handler function receives a string as an anonymous function and returns the router function\n\n    void set_handler(std::function&lt;std::string(const std::string&amp;)&gt; handler) {\n        logic_handler = handler;\n    }\n\n    \/\/ \ud074\ub77c\uc774\uc5b8\ud2b8 \uc694\uccad\uc744 \ucc98\ub9ac\ud558\ub294 \uac00\uc0c1 \uba54\uc11c\ub4dc\n    \/\/ Virtual method that handles client requests\n    void receive_request(const std::string&amp; message) {\n        std::cout &lt;&lt; \"&#91;Server] \uc694\uccad \ubc1b\uc74c \/ Requested: \" &lt;&lt; message &lt;&lt; std::endl;\n\n        if (logic_handler) {\n\n            \/\/ \uc678\ubd80\uc5d0\uc11c \uc815\uc758\ud55c \ub78c\ub2e4 \ud568\uc218\uac00 \uc5ec\uae30\uc11c \uc2e4\ud589\ub429\ub2c8\ub2e4!\n            \/\/ login_handler\ub294 router \ud568\uc218\uc758 \uac12\uc744 \ub9ac\ud134\n            \/\/ The externally defined Lambda function is executed here!\n            \/\/ login_handler returns the value of the router function\n\n            std::string response = logic_handler(message);\n            std::cout &lt;&lt; \"&#91;Server] \uc751\ub2f5 \ubcf4\ub0c4 \/ Reply sent: \" &lt;&lt; response &lt;&lt; \"\\n\" &lt;&lt; std::endl;\n        } else {\n            std::cout &lt;&lt; \"&#91;Server] \ucc98\ub9ac\ud560 \ud578\ub4e4\ub7ec\uac00 \uc5c6\uc2b5\ub2c8\ub2e4\/There is no handler to process..\\n\" &lt;&lt; std::endl;\n        }\n    }\n};\n\nstd::string router(const std::string&amp; msg) {\n    if (msg == \"aloy\") return \"horizon\";\n    if (msg == \"silverhand\") return \"cyberpunk\";\n    return \"unknown command\";\n}\n\nint main() {\n    Server my_sv;\n\n    \/\/ \ub78c\ub2e4 \ud568\uc218\ub97c \uc0ac\uc6a9\ud558\uc5ec \ub85c\uc9c1 \uc5f0\uacb0 \/ Connecting logic using lambda functions\n    my_sv.set_handler(&#91;&amp;](const std::string&amp; req) -&gt; std::string {\n        return router(req);\n    });\n\n    \/\/ \uc694\uccad \/ requests\n    my_sv.receive_request(\"aloy\");\n    my_sv.receive_request(\"silverhand\");\n    my_sv.receive_request(\"npc\");\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ucef4\ud30c\uc77c \/ Compiling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g++ main.cpp -o main -std=c++17<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uc2e4\ud589 \/ Run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/main<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uacb0\uacfc \/ Resutl<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rambda_function % .\/main\n&#91;Server] \uc694\uccad \ubc1b\uc74c: aloy\n&#91;Server] \uc751\ub2f5 \ubcf4\ub0c4\/Reply sent: horizon\n\n&#91;Server] \uc694\uccad \ubc1b\uc74c: silverhand\n&#91;Server] \uc751\ub2f5 \ubcf4\ub0c4\/Reply sent: cyberpunk\n\n&#91;Server] \uc694\uccad \ubc1b\uc74c: npc\n&#91;Server] \uc751\ub2f5 \ubcf4\ub0c4\/Reply sent: unknown command\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \ub2e4\uc74c\ucf54\ub4dc\ucc98\ub7fc \ub78c\ub2e4\ud568\uc218\ub780 \uc774\ub984\uc5c6\ub294 \uc775\uba85\ud568\uc218\ub97c \uc758\ubbf8\ud569\ub2c8\ub2e4.As shown in the following code, a lambda function refers to an unnamed anonymous function. \ud83d\udc49\ud83c\udffb \uc704\uc640 \uac19\uc740 \ub78c\ub2e4\ud568\uc218\ub97c \ud30c\ub77c\uba54\ud130\ub85c \ub2e4\ub978 \ud568\uc218\uc5d0 \uc804\ub2ec \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774\uac83\uc744 \ucf5c\ubc31\ud568\uc218\ub77c\uace0 \ud569\ub2c8\ub2e4.You can pass a lambda function like the one above as a parameter to another function. This is called a callback function. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,1],"tags":[],"class_list":["post-5444","post","type-post","status-publish","format-standard","hentry","category-cpp","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=5444"}],"version-history":[{"count":3,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5444\/revisions"}],"predecessor-version":[{"id":5447,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5444\/revisions\/5447"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}