{"id":347,"date":"2017-12-22T15:08:53","date_gmt":"2017-12-22T07:08:53","guid":{"rendered":"https:\/\/himmel.tech\/wp\/?p=347"},"modified":"2017-12-22T15:08:53","modified_gmt":"2017-12-22T07:08:53","slug":"boostbind-%e4%b8%8e-operator","status":"publish","type":"post","link":"https:\/\/himmel.tech\/wp\/?p=347","title":{"rendered":"boost::bind \u4e0e operator()"},"content":{"rendered":"<p>\u4eca\u5929\u6765\u8bf4\u8bf4 boost::bind \u548c operator()\u3002\u8fd9\u4e24\u5929\u5728\u6298\u817e asio\uff0c\u88ab\u56de\u8c03\u51fd\u6570\u4f20\u53c2\u7684\u95ee\u9898\u641e\u5f97\u5934\u75db\u4e0d\u5df2\uff0c\u770b\u5230\u5b98\u65b9\u7684\u4f8b\u5b50\u4e0d\u5c11\u662f\u62ff boost::bind \u6765\u5904\u7406\u7684\uff0c\u89c9\u5f97\u633a\u65b9\u4fbf\u7684\uff0c\u7279\u610f\u8bb0\u5f55\u4e00\u4e0b\u3002\u53e6\u5916\uff0cboost::bind \u597d\u50cf\u662f\u5728 c++11 \u4e2d\u88ab\u7eb3\u5165\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u4f7f\u7528 std::bind\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4ec0\u4e48\u662f operator()\uff1f operator() \u6307\u7684\u662f\u5c0f\u62ec\u53f7\u8fd0\u7b97\u7b26\u3002\u90a3\u4e48\u54ea\u91cc\u4f1a\u7528\u5230\u5c0f\u62ec\u53f7\u8fd0\u7b97\u7b26\u5462\uff1f\u51fd\u6570\u3002\u5f53\u4e00\u4e2a\u7c7b\u91cd\u8f7d\u4e86 operator() \u8fd0\u7b97\u7b26\uff0c\u90a3\u4e48\u5b83\u4f1a\u53d8\u6210\u4e00\u4e2a\u5bf9\u8c61\u51fd\u6570\u3002<\/p>\n<p>\u90a3\u4e48\u8fd9\u6709\u4ec0\u4e48\u597d\u5904\u5462\uff1f\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5728 asio \u4e2d\u9700\u8981\u6ce8\u518c\u5f02\u6b65\u64cd\u4f5c\u7ed3\u675f\u65f6\u7684\u56de\u8c03\u51fd\u6570\uff0c\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u53ea\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u5373<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">void handler(const asio::error_code &amp;error, size_t bytes);<\/pre>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f20\u5165\u5176\u4ed6\u7684\u53c2\u6570\uff0c\u6bd4\u5982\u4f20\u5165\u5f53\u524dSocket\u7684\u5f15\u7528\u4ee5\u4fbf\u542f\u52a8\u4e0b\u4e00\u6b21\u5f02\u6b65\u64cd\u4f5c\uff0c\u8fd9\u65f6\u51fd\u6570\u5bf9\u8c61\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u7c7b\uff0c\u5c06\u5b83\u7684operator()\u91cd\u8f7d\u4e3a\u9700\u8981\u7684\u5f62\u5f0f\uff0c\u7136\u540e\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f20\u5165\u6240\u9700\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u5c06\u8fd9\u4e9b\u5f15\u7528\u4fdd\u5b58\u4e3a\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u5373\u53ef\u3002\u8fd9\u6837\uff0c\u5728\u901a\u8fc7\u6784\u9020\u51fd\u6570\u5c06\u7c7b\u5b9e\u4f8b\u5316\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u50cf\u4e00\u4e2a\u51fd\u6570\u4e00\u6837\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u4e86\u3002<\/p>\n<p>\u8bb2\u9053\u7406\uff0c\u8fd9\u4e2a operator() \u91cd\u8f7d\u6211\u4e0a\u5468\u770b C++ Primer \u7684\u65f6\u5019\u521a\u770b\u5230\u8fc7\uff0c\u4e0d\u8fc7\u4e07\u4e07\u6ca1\u60f3\u5230\u8fd8\u80fd\u8fd9\u4e48\u73a9\u5c31\u662f\u4e86\u2026\u2026<\/p>\n<p>boost::bind \u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u6a21\u677f\uff0c\u53ef\u4ee5\u4ece\u4e00\u4e2a\u51fd\u6570\u6216\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u4e2d\u6784\u9020\u6240\u9700\u7684\u51fd\u6570\u5bf9\u8c61\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">void CameraStreamer::tcpListener(const asio::error_code &amp;error, \n                                 size_t bytes_transferred, \n                                 tcp::socket &amp;tcpSocket,\n                                 vector&lt;uint8_t&gt; &amp;tcpBuffer) {    \n    \/\/ Remote close TCP connection -&gt; log, stop ioService then return\n    if (error == asio::error::eof) {\n        LOG(INFO) &lt;&lt; \"Remote close TCP connection.\";\n        ioService.stop();\n        return;\n    }\n\n    if (error) {\n        DLOG(ERROR) &lt;&lt; \"Some Error Occurred.\";\n    } else {\n        DLOG(INFO) &lt;&lt; \"TCP received \" &lt;&lt; bytes_transferred \n                   &lt;&lt; \" bytes.\";\n    }\n    tcpSocket.async_receive(asio::buffer(tcpBuffer),\n                            bind(&amp;CameraStreamer::tcpListener, \n                                 this, \n                                 placeholders::_1, \n                                 placeholders::_2,\n                                 ref(tcpSocket), \n                                 ref(tcpBuffer)));\n}<\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u662f CameraStreamer \u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u5f53 asio \u89e6\u53d1\u6b64\u56de\u8c03\u7684\u65f6\u5019\uff0c\u5b83\u9996\u5148\u68c0\u67e5\u662f\u5426\u662fTCP\u8fde\u63a5\u5173\u95ed\u7684\u6d88\u606f\uff0c\u7136\u540e\u68c0\u67e5\u662f\u5426\u6709\u5176\u4ed6\u9519\u8bef\u53d1\u751f\uff0c\u968f\u540e\u5904\u7406\u6536\u5230\u7684\u5185\u5bb9\uff08\u8fd9\u91cc\u4ec5\u8bb0\u5f55\u4e86log\uff09\uff0c\u6700\u540e\u542f\u52a8\u4e0b\u4e00\u6b21\u5f02\u6b65\u6570\u636e\u63a5\u6536\u3002<\/p>\n<p>\u7531\u4e8e\u5728\u542f\u52a8\u4e0b\u4e00\u6b21\u5f02\u6b65\u63a5\u6536\u65f6\uff0c\u51fd\u6570\u7684\u5f62\u53c2\u8868\u5e76\u4e0d\u5339\u914d\uff0c\u56e0\u6b64\u4f7f\u7528\u4e86 std::bind\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u7684\u7ed1\u5b9a\u65b9\u6cd5\uff0c\u5176\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u51fd\u6570\u540d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u6240\u5c5e\u5bf9\u8c61\u6216\u6240\u5c5e\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5269\u4e0b\u7684\u53c2\u6570\u4e0e\u8be5\u51fd\u6570\u7684\u5f62\u53c2\u8868\u4f9d\u6b21\u5339\u914d\u3002std::placeholders::_1\u4e3a\u5360\u4f4d\u7b26\uff0c\u4ee3\u8868 std::bind \u751f\u6210\u5bf9\u8c61\u7684 operator() \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u5176\u4f59\u5360\u4f4d\u7b26\u4e5f\u662f\u540c\u7406\u3002std::ref() \u8868\u793a\u4ee5\u5f15\u7528\u7684\u5f62\u5f0f\u4f20\u5165\uff0c\u5426\u5219\u4f1a\u4ee5\u503c\u7684\u5f62\u5f0f\u4f20\u5165\u3002<\/p>\n<p>\u8fd9\u6837\uff0c\u8fd9\u4e2a\u51fd\u6570\u5bf9\u8c61\u5c31\u53ef\u4ee5\u8ddf tcp::socket \u6240\u8981\u6c42\u7684\u5f02\u6b65handler\u5f62\u5f0f\u6240\u5339\u914d\u4e86\uff0c\u987a\u5229\u89e3\u51b3\u4e86\u8fd9\u4e00\u95ee\u9898\u3002\u5f53\u7136\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86\u5f15\u7528\uff0c\u9700\u8981\u5c0f\u5fc3\u7684\u7ba1\u7406\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>\u8fd9\u79cd\u5904\u7406\u65b9\u5f0f\u6709\u4ec0\u4e48\u574f\u5904\u5417\uff1f\u5f53\u7136\u6709\uff0cbind\u4f5c\u4e3a\u4e00\u4e2a\u901a\u7528\u7684\u6a21\u677f\uff0c\u5305\u542b\u76f8\u5f53\u5927\u91cf\u7684\u91cd\u8f7d\u7b49\uff0c\u56e0\u6b64\u5728\u7f16\u8bd1\u65f6\u9700\u8981\u82b1\u8d39\u5927\u91cf\u65f6\u95f4\u5728\u6a21\u677f\u5339\u914d\u4e0a\u3002\u4e0d\u8fc7\u76f8\u6bd4\u4e8e\u7f16\u5199\u4ee3\u7801\u7684\u65b9\u4fbf\u6027\uff0c\u8fd9\u8fd8\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u5728\u6ca1\u6709\u8fd9\u79cd\u673a\u5236\u7684\u60c5\u51b5\u4e0b\uff0c\u5e93\u7684\u8bbe\u8ba1\u8005\u5982\u4f55\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u7b54\u6848\u662f void* \u6307\u9488\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e00\u822c\u5141\u8bb8\u7528\u6237\u4f20\u5165\u4e00\u4e2a void* \u6307\u9488\uff0c\u7136\u540e\u5f53\u4e8b\u4ef6\u89e6\u53d1\u65f6\uff0c\u4f1a\u518d\u628a\u8fd9\u4e2a\u6307\u9488\u4ea4\u7ed9 handler\u3002\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a struct \u6765\u5b58\u50a8\u8981\u4f20\u5165\u7684\u6570\u636e\uff0c\u7136\u540e\u5bf9 void* \u6307\u9488\u8fdb\u884c\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u6765\u8bf4\u8bf4 boost::bind \u548c operator()\u3002\u8fd9\u4e24\u5929\u5728\u6298\u817e asio\uff0c\u88ab\u56de\u8c03\u51fd\u6570\u4f20\u53c2\u7684\u95ee\u9898\u641e\u5f97 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/himmel.tech\/wp\/?p=347\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cboost::bind \u4e0e operator()\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-347","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p83CFH-5B","_links":{"self":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts\/347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=347"}],"version-history":[{"count":6,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts\/347\/revisions"}],"predecessor-version":[{"id":353,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts\/347\/revisions\/353"}],"wp:attachment":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}