{"id":420765,"date":"2021-04-01T14:21:47","date_gmt":"2021-04-01T06:21:47","guid":{"rendered":"http:\/\/4563.org\/?p=420765"},"modified":"2021-04-01T14:21:47","modified_gmt":"2021-04-01T06:21:47","slug":"%e8%af%b7%e6%95%99%e4%b8%8b-lambda-%e5%87%bd%e6%95%b0%e7%9a%84%e6%8d%95%e8%8e%b7%e5%80%bc%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=420765","title":{"rendered":"\u8bf7\u6559\u4e0b lambda \u51fd\u6570\u7684\u6355\u83b7\u503c\u5217\u8868"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e0b lambda \u51fd\u6570\u7684\u6355\u83b7\u503c\u5217\u8868               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : auto8888 <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>class Testc { public:     void fun1(int *&amp;p)     {         p = new int();         *p =2;     }     void fun2()     {         std::thread t([=] {(fun1)( p);});         if(t.joinable())             t.detach();     }     int *p; }; <\/code><\/pre>\n<p>fun1 \u7684\u53c2\u6570\u662f\u5f15\u7528\u4f20\u9012\uff0c\u4e3a\u4ec0\u4e48 fun2 \u7528[=]\u6355\u83b7\u4e5f\u7f16\u8bd1\u901a\u8fc7\uff0c\u7279\u6b8a\u5728\u4e8e\u53c2\u6570\u662f\u7c7b\u7684\u6210\u5458\u3002<\/p>\n<p>\u5982\u679c\u628a fun1 \u79fb\u5230\u5168\u5c40\u51fd\u6570\uff0c\u4e3a\u4ec0\u4e48 fun2 \u7528[this]\u6355\u83b7\u4e5f\u7f16\u8bd1\u901a\u8fc7<\/p>\n<pre><code>void fun1(int *&amp;p) {     p = new int();     *p =2; } class Testc { public:     void fun2()     {         std::thread t([this] {(fun1)( p);});         if(t.joinable())             t.detach();     }     int *p; }; <\/code><\/pre>\n<p>\u5c31\u6709\u70b9\u61f5\u903c\u4e86<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5610936\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : auto8888 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u521a\u8bd5\u4e86\u4e00\u4e0b\uff0c\u8c8c\u4f3c\u7528\u5f15\u7528\u6355\u83b7\u4e5f\u4e0d\u80fd\u4fee\u6539\u5916\u90e8\u53d8\u91cf\uff1f\uff1f\uff1f<\/p>\n<p>\u6211\uff0c\u88c2\u5f00\u6765                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5610937\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : morningtzh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/zh.cppreference.com\/w\/cpp\/language\/lambda<\/p>\n<p>\u76f4\u63a5\u770b cppreference \u5427\u3002<\/p>\n<p>lambda \u63a5\u6536\u7684\u662f\u503c\uff0c\u5f53\u7136\u4e0d\u4f1a\u6539\u5916\u90e8\u53d8\u91cf\u54af                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5610938\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mogg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u592a\u61c2\uff0c\u4e3b\u60f3\u95ee\u4ec0\u4e48\uff1f<br \/>\u8fd9\u91cc\u6709\u4e00\u4e2a\u4f5c\u7528\u57df\u7684\u6982\u5ff5\uff0c\u95ed\u5305\u91cc\u7684 fun1,p \u5176\u5b9e\u662f this.fun1,this.p<br \/>\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff08 detach \u6539\u6210 join \uff09\uff0c\u6ca1\u4ec0\u4e48\u95ee\u9898\u554a\u2026\u2026<br \/>\u6d4b\u8bd5\u4ee3\u7801 https:\/\/paste.ubuntu.com\/p\/HrsHf5zSXS\/                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5610939\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : auto8888 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mogg \u6211\u4e5f\u8bd5\u4e86\u4e00\u4e0b\uff0c\u90a3\u8fd9\u6837\u7684\u8bdd\u95ed\u5305\u80fd\u6539\u53d8\u4fd8\u83b7\u7684\u503c\u554a\uff08 this.p \uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e0b lambda \u51fd\u6570\u7684\u6355\u83b7\u503c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/420765"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=420765"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/420765\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=420765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=420765"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=420765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}