{"id":70773,"date":"2026-02-02T11:17:19","date_gmt":"2026-02-02T03:17:19","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/70773.html"},"modified":"2026-02-02T11:17:19","modified_gmt":"2026-02-02T03:17:19","slug":"%e7%b1%bb%e5%9e%8b%e6%93%a6%e9%99%a4%e7%9a%84%e4%bc%98%e9%9b%85%e5%ae%9e%e7%8e%b0%ef%bc%9ac-%ef%bc%9cany%ef%bc%9e-%e5%85%a8%e9%9d%a2%e6%b7%b1%e5%ba%a6%e8%a7%a3%e6%9e%90%e4%b8%8e%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/70773.html","title":{"rendered":"\u7c7b\u578b\u64e6\u9664\u7684\u4f18\u96c5\u5b9e\u73b0\uff1aC++ \uff1cany\uff1e \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u8fd0\u884c\u65f6\u591a\u6001\u5b9e\u6218\u6307\u5357"},"content":{"rendered":"<p>\u5728\u5f3a\u7c7b\u578b\u3001\u9759\u6001\u7f16\u8bd1\u7684 C&#043;&#043; \u4e16\u754c\u4e2d&#xff0c;\u5b89\u5168\u5730\u5b58\u50a8\u548c\u64cd\u4f5c\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\u59cb\u7ec8\u662f\u4e00\u9879\u6311\u6218\u3002\u4f20\u7edf\u65b9\u6848\u5982 void* \u7f3a\u4e4f\u7c7b\u578b\u5b89\u5168&#xff0c;union \u4ec5\u9650\u5e73\u51e1\u7c7b\u578b&#xff0c;\u800c\u7ee7\u627f\u4f53\u7cfb&#xff08;\u5982 boost::any \u7684\u65e9\u671f\u5b9e\u73b0&#xff09;\u53c8\u5f15\u5165\u865a\u51fd\u6570\u5f00\u9500\u4e0e\u8bbe\u8ba1\u8026\u5408\u3002\u4e3a\u89e3\u51b3\u8fd9\u4e00\u6839\u672c\u6027\u95ee\u9898&#xff0c;C&#043;&#043;17 \u6b63\u5f0f\u5f15\u5165\u4e86std::any\u2014\u2014 \u4e00\u4e2a\u7c7b\u578b\u5b89\u5168\u3001\u503c\u8bed\u4e49\u3001\u652f\u6301\u4efb\u610f\u62f7\u8d1d\u6784\u9020\u7c7b\u578b\u7684\u901a\u7528\u5bb9\u5668\u3002<\/p>\n<p>std::any \u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e\u8fd0\u884c\u65f6\u7c7b\u578b\u64e6\u9664&#xff08;Runtime Type Erasure&#xff09;&#xff1a;\u5b83\u5141\u8bb8\u4f60\u5728\u7f16\u8bd1\u671f\u672a\u77e5\u5177\u4f53\u7c7b\u578b\u7684\u60c5\u51b5\u4e0b&#xff0c;\u5b89\u5168\u5730\u5b58\u50a8\u3001\u4f20\u9012\u548c\u6062\u590d\u4efb\u610f\u5bf9\u8c61&#xff0c;\u540c\u65f6\u4fdd\u8bc1\u6790\u6784\u6b63\u786e\u6027\u4e0e\u5f02\u5e38\u5b89\u5168\u6027\u3002\u4ece\u914d\u7f6e\u7cfb\u7edf\u3001\u63d2\u4ef6\u67b6\u6784\u3001\u811a\u672c\u7ed1\u5b9a\u5230\u4e8b\u4ef6\u603b\u7ebf&#xff0c;std::any \u4e3a C&#043;&#043; \u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u7ea7\u3001\u6807\u51c6\u5316\u7684\u201c\u52a8\u6001\u7c7b\u578b\u201d\u80fd\u529b\u3002<\/p>\n<p>\u7136\u800c&#xff0c; \u5e76\u975e\u4e07\u80fd\u94f6\u5f39\u2014\u2014\u5176\u6027\u80fd\u7279\u6027\u3001\u5185\u5b58\u6a21\u578b\u4e0e\u4f7f\u7528\u8fb9\u754c\u9700\u8c28\u614e\u628a\u63e1\u3002\u672c\u6587\u5c06\u4ece\u8bbe\u8ba1\u539f\u7406\u3001\u6838\u5fc3\u63a5\u53e3\u3001\u5185\u5b58\u7ba1\u7406\u3001\u6027\u80fd\u5206\u6790\u3001\u5178\u578b\u573a\u666f\u53ca\u6700\u4f73\u5b9e\u8df5\u516d\u5927\u7ef4\u5ea6&#xff0c;\u5bf9 std::any \u8fdb\u884c\u7cfb\u7edf\u6027\u3001\u5de5\u7a0b\u5316\u3001\u6df1\u5ea6\u5316\u7684\u5168\u9762\u603b\u7ed3&#xff0c;\u52a9\u4f60\u771f\u6b63\u9a7e\u9a6d\u8fd9\u4e00\u201c\u7c7b\u578b\u4fdd\u9669\u7bb1\u201d\u3002<\/p>\n<h2>\u4e00\u3001\u4ec0\u4e48\u662f std::any&#xff1f;<\/h2>\n<h3>1.1 \u5b9a\u4f4d\u4e0e\u6838\u5fc3\u7279\u6027<\/h3>\n<ul>\n<li>\u5b9a\u4e49&#xff1a;std::any\u00a0\u662f\u4e00\u4e2a\u53ef\u6301\u6709\u4efb\u610f\u975e\u6570\u7ec4\u3001\u975e\u5f15\u7528\u3001\u975e cv-qualified\u00a0\u7c7b\u578b\u7684\u503c\u8bed\u4e49\u5bb9\u5668\u3002<\/li>\n<li>\u5173\u952e\u7279\u6027&#xff1a;\n<ul>\n<li>\u7c7b\u578b\u5b89\u5168&#xff1a;\u901a\u8fc7\u00a0std::any_cast\u00a0\u5b89\u5168\u63d0\u53d6&#xff0c;\u9519\u8bef\u7c7b\u578b\u629b\u51fa\u5f02\u5e38&#xff1b;<\/li>\n<li>\u503c\u8bed\u4e49&#xff1a;\u652f\u6301\u62f7\u8d1d\u3001\u79fb\u52a8\u3001\u8d4b\u503c&#xff08;\u8981\u6c42\u5185\u90e8\u5bf9\u8c61\u652f\u6301&#xff09;&#xff1b;<\/li>\n<li>\u5c0f\u5bf9\u8c61\u4f18\u5316&#xff08;Small Object Optimization, SOO&#xff09;&#xff1a;\u5c0f\u5bf9\u8c61\u76f4\u63a5\u5b58\u50a8\u4e8e\u5185\u90e8\u7f13\u51b2\u533a&#xff0c;\u907f\u514d\u5806\u5206\u914d&#xff1b;<\/li>\n<li>\u81ea\u52a8\u6790\u6784&#xff1a;\u6790\u6784\u65f6\u81ea\u52a8\u8c03\u7528\u5185\u90e8\u5bf9\u8c61\u7684\u6790\u6784\u51fd\u6570&#xff1b;<\/li>\n<li>\u7a7a\u72b6\u6001\u652f\u6301&#xff1a;\u9ed8\u8ba4\u6784\u9020\u7684\u00a0any\u00a0\u4e3a\u7a7a&#xff08;has_value() &#061;&#061; false&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>#include\u00a0&lt;any&gt;#include\u00a0&lt;string&gt;#include\u00a0&lt;vector&gt;<br \/>\nstd::any a &#061;\u00a042; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ inta &#061; std::string(&#034;hello&#034;); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ \u66ff\u6362\u4e3a stringa &#061; std::vector&lt;double&gt;{1.0,\u00a02.0}; \u00a0\/\/ \u66ff\u6362\u4e3a vector<br \/>\nif\u00a0(a.has_value()) {\u00a0 \u00a0\u00a0try\u00a0{\u00a0 \u00a0 \u00a0 \u00a0\u00a0auto&amp; s &#061; std::any_cast&lt;std::string&amp;&gt;(a);\u00a0\/\/ \u5b89\u5168\u63d0\u53d6\u00a0 \u00a0 \u00a0 \u00a0 std::cout &lt;&lt; s;\u00a0 \u00a0 }\u00a0catch\u00a0(const\u00a0std::bad_any_cast&amp;) {\u00a0 \u00a0 \u00a0 \u00a0 std::cout &lt;&lt;\u00a0&#034;Type mismatch!&#034;;\u00a0 \u00a0 }} <\/p>\n<p>\u2705\u00a0\u4e00\u53e5\u8bdd\u603b\u7ed3&#xff1a;std::any\u00a0&#061; \u5b89\u5168\u7684\u3001\u5e26\u7c7b\u578b\u7684\u00a0void*\u00a0&#043; \u81ea\u52a8\u5185\u5b58\u7ba1\u7406\u3002<\/p>\n<hr \/>\n<h2>\u4e8c\u3001\u6838\u5fc3\u63a5\u53e3\u8be6\u89e3<\/h2>\n<h3>2.1 \u6784\u9020\u4e0e\u8d4b\u503c<\/h3>\n<table>\n<tr>\u64cd\u4f5c\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>std::any a;<\/td>\n<td>\u9ed8\u8ba4\u6784\u9020&#xff08;\u7a7a&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>std::any a &#061; value;<\/td>\n<td>\u62f7\u8d1d\u6784\u9020&#xff08;\u5b58\u50a8\u00a0value\u00a0\u7684\u526f\u672c&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>std::any a{std::in_place_type&lt;T&gt;, args&#8230;};<\/td>\n<td>\u5c31\u5730\u6784\u9020&#xff08;\u907f\u514d\u4e34\u65f6\u5bf9\u8c61&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>a &#061; value;<\/td>\n<td>\u8d4b\u503c&#xff08;\u5148\u6790\u6784\u65e7\u503c&#xff0c;\u518d\u6784\u9020\u65b0\u503c&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>a.reset();<\/td>\n<td>\u6e05\u7a7a&#xff08;\u7b49\u4ef7\u4e8e\u00a0a &#061; std::any{}&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5c31\u5730\u6784\u9020\u793a\u4f8b&#xff1a;<\/p>\n<p>std::any a{std::in_place_type&lt;std::vector&lt;int&gt;&gt;,\u00a010,\u00a042};\u00a0\/\/ \u7b49\u4ef7\u4e8e vector&lt;int&gt;(10, 42) <\/p>\n<h3>2.2 \u67e5\u8be2\u4e0e\u8bbf\u95ee<\/h3>\n<table>\n<tr>\u64cd\u4f5c\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>a.has_value()<\/td>\n<td>\u662f\u5426\u5305\u542b\u503c<\/td>\n<\/tr>\n<tr>\n<td>a.type()<\/td>\n<td>\u8fd4\u56de\u00a0std::type_info&#xff08;\u53ef\u7528\u4e8e RTTI \u6bd4\u8f83&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>std::any_cast&lt;T&gt;(a)<\/td>\n<td>\u503c\u63d0\u53d6&#xff08;\u8fd4\u56de\u526f\u672c&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>std::any_cast&lt;T&amp;&gt;(a)<\/td>\n<td>\u5f15\u7528\u63d0\u53d6&#xff08;\u53ef\u4fee\u6539&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>std::any_cast&lt;T*&gt;(a)<\/td>\n<td>\u6307\u9488\u63d0\u53d6&#xff08;\u5931\u8d25\u8fd4\u56de\u00a0nullptr&#xff0c;\u4e0d\u629b\u5f02\u5e38&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f\u00a0\u91cd\u8981\u533a\u522b&#xff1a;<\/p>\n<ul>\n<li>any_cast&lt;T&gt;(a)&#xff1a;\u8fd4\u56de\u00a0T\u00a0\u526f\u672c&#xff0c;\u8981\u6c42\u00a0T\u00a0\u53ef\u62f7\u8d1d&#xff1b;<\/li>\n<li>any_cast&lt;T&amp;&gt;(a)&#xff1a;\u8fd4\u56de\u5de6\u503c\u5f15\u7528&#xff0c;\u53ef\u4fee\u6539\u5185\u90e8\u5bf9\u8c61&#xff1b;<\/li>\n<li>any_cast&lt;T*&gt;(a)&#xff1a;\u6700\u5b89\u5168&#xff0c;\u7528\u4e8e\u7c7b\u578b\u68c0\u67e5\u800c\u4e0d\u629b\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p>if\u00a0(auto* p &#061; std::any_cast&lt;int&gt;(&amp;a)) {\u00a0 \u00a0 *p &#043;&#061;\u00a010;\u00a0\/\/ \u5b89\u5168\u4fee\u6539} <\/p>\n<h2>\u4e09\u3001\u5185\u5b58\u6a21\u578b\u4e0e\u5c0f\u5bf9\u8c61\u4f18\u5316&#xff08;SOO&#xff09;<\/h2>\n<h3>3.1 \u5185\u90e8\u5b9e\u73b0\u673a\u5236<\/h3>\n<p>std::any\u00a0\u901a\u5e38\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565&#xff1a;<\/p>\n<ul>\n<li>\u5185\u90e8\u7f13\u51b2\u533a&#xff1a;\u56fa\u5b9a\u5927\u5c0f&#xff08;\u5e38\u89c1\u4e3a 16 \u6216 32 \u5b57\u8282&#xff09;&#xff1b;<\/li>\n<li>\u82e5\u5bf9\u8c61\u5c3a\u5bf8 \u2264 \u7f13\u51b2\u533a\u4e14\u6ee1\u8db3\u5bf9\u9f50\u8981\u6c42\u00a0\u2192\u00a0\u76f4\u63a5\u5b58\u50a8&#xff08;\u65e0\u5806\u5206\u914d&#xff09;&#xff1b;<\/li>\n<li>\u5426\u5219\u00a0\u2192\u00a0\u5728\u5806\u4e0a\u5206\u914d&#xff0c;\u5e76\u5b58\u50a8\u6307\u9488 &#043; \u865a\u51fd\u6570\u8868&#xff08;\u6216\u51fd\u6570\u6307\u9488&#xff09;\u00a0\u7528\u4e8e\u6790\u6784\/\u62f7\u8d1d\u3002<\/li>\n<\/ul>\n<p>static_assert(sizeof(std::any) &gt;&#061;\u00a0sizeof(void*) *\u00a02\u00a0&#043;\u00a0sizeof(size_t));\/\/ \u5178\u578b\u5e03\u5c40&#xff1a;[buffer or ptr][vtable or fn-ptrs][type_info?] <\/p>\n<h3>3.2 SOO \u8fb9\u754c\u6d4b\u8bd5&#xff08;\u5e73\u53f0\u76f8\u5173&#xff09;<\/h3>\n<p>std::cout\u00a0&lt;&lt;\u00a0sizeof(std::any) &lt;&lt;\u00a0&#034;\\\\n&#034;; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u901a\u5e38 32 \u6216 64std::cout\u00a0&lt;&lt;\u00a0sizeof(std::string) &lt;&lt;\u00a0&#034;\\\\n&#034;; \u00a0 \u00a0 \u00a0\u00a0\/\/ \u901a\u5e38 32&#xff08;SSO&#xff09;std::cout\u00a0&lt;&lt;\u00a0sizeof(std::vector&lt;int&gt;) &lt;&lt;\u00a0&#034;\\\\n&#034;; \u00a0\/\/ \u901a\u5e38 24std::any\u00a0a1 &#061; std::string(&#034;short&#034;); \u00a0\u00a0\/\/ SSO&#xff0c;\u53ef\u80fd\u65e0\u5806\u5206\u914dstd::any\u00a0a2 &#061; std::string(&#034;very long string&#8230;&#034;);\u00a0\/\/ \u53ef\u80fd\u5806\u5206\u914dstd::any\u00a0a3 &#061; std::vector&lt;int&gt;(1000);\u00a0\/\/ \u5fc5\u7136\u5806\u5206\u914d&#xff08;vector \u5185\u90e8\u6570\u636e\u5728\u5806&#xff09; <\/p>\n<p>&#x1f4cc;\u00a0\u6ce8\u610f&#xff1a;\u5373\u4f7f\u00a0any\u00a0\u672c\u8eab SOO&#xff0c;\u5176\u5185\u90e8\u5bf9\u8c61&#xff08;\u5982\u00a0vector&#xff09;\u4ecd\u53ef\u80fd\u81ea\u884c\u5206\u914d\u5806\u5185\u5b58\u3002<\/p>\n<hr \/>\n<h2>\u56db\u3001\u6027\u80fd\u5206\u6790\u4e0e\u5f00\u9500<\/h2>\n<table>\n<tr>\u64cd\u4f5c\u5f00\u9500<\/tr>\n<tbody>\n<tr>\n<td>\u6784\u9020&#xff08;\u5c0f\u5bf9\u8c61&#xff09;<\/td>\n<td>0 \u5806\u5206\u914d&#xff0c;memcpy<\/td>\n<\/tr>\n<tr>\n<td>\u6784\u9020&#xff08;\u5927\u5bf9\u8c61&#xff09;<\/td>\n<td>1 \u6b21\u5806\u5206\u914d &#043; \u62f7\u8d1d\u6784\u9020<\/td>\n<\/tr>\n<tr>\n<td>\u62f7\u8d1d<\/td>\n<td>\u82e5 SOO&#xff1a;memcpy&#xff1b;\u5426\u5219&#xff1a;\u5806\u5206\u914d &#043; \u62f7\u8d1d\u6784\u9020<\/td>\n<\/tr>\n<tr>\n<td>\u79fb\u52a8<\/td>\n<td>\u82e5 SOO&#xff1a;memcpy&#xff1b;\u5426\u5219&#xff1a;\u6307\u9488\u8f6c\u79fb&#xff08;\u5e38\u6570\u65f6\u95f4&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6790\u6784<\/td>\n<td>\u82e5 SOO&#xff1a;\u76f4\u63a5\u8c03\u7528\u6790\u6784&#xff1b;\u5426\u5219&#xff1a;delete &#043; \u6790\u6784<\/td>\n<\/tr>\n<tr>\n<td>any_cast&#xff08;\u6b63\u786e\u7c7b\u578b&#xff09;<\/td>\n<td>1 \u6b21 type_info \u6bd4\u8f83 &#043; \u6307\u9488\u8f6c\u6362<\/td>\n<\/tr>\n<tr>\n<td>any_cast&#xff08;\u9519\u8bef\u7c7b\u578b&#xff09;<\/td>\n<td>\u629b\u51fa\u00a0std::bad_any_cast&#xff08;\u6602\u8d35&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4ca;\u00a0\u6027\u80fd\u5efa\u8bae&#xff1a;<\/p>\n<ul>\n<li>\u907f\u514d\u5728\u70ed\u8def\u5f84\u4e2d\u9891\u7e41\u6784\u9020\/\u62f7\u8d1d\u5927\u5bf9\u8c61\u00a0any&#xff1b;<\/li>\n<li>\u4f18\u5148\u4f7f\u7528\u00a0any_cast&lt;T*&gt;\u00a0\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5&#xff1b;<\/li>\n<li>\u5bf9 move-only \u7c7b\u578b&#xff0c;\u8003\u8651\u00a0std::optional&lt;std::any&gt;\u00a0\u6216\u81ea\u5b9a\u4e49\u65b9\u6848&#xff08;C&#043;&#043;23 \u524d\u00a0any\u00a0\u4e0d\u652f\u6301 move-only&#xff09;\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2>\u4e94\u3001\u4e0e\u66ff\u4ee3\u65b9\u6848\u5bf9\u6bd4<\/h2>\n<table>\n<tr>\u65b9\u6848\u4f18\u70b9\u7f3a\u70b9\u9002\u7528\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>std::any<\/td>\n<td>\u6807\u51c6\u3001\u7c7b\u578b\u5b89\u5168\u3001\u503c\u8bed\u4e49<\/td>\n<td>\u6709\u8fd0\u884c\u65f6\u5f00\u9500\u3001\u4e0d\u652f\u6301 move-only&#xff08;C&#043;&#043;23 \u524d&#xff09;<\/td>\n<td>\u901a\u7528\u52a8\u6001\u5bb9\u5668\u3001\u914d\u7f6e\u7cfb\u7edf<\/td>\n<\/tr>\n<tr>\n<td>void*\u00a0&#043; \u624b\u52a8\u7ba1\u7406<\/td>\n<td>\u96f6\u5f00\u9500<\/td>\n<td>\u65e0\u7c7b\u578b\u5b89\u5168\u3001\u6613\u5185\u5b58\u6cc4\u6f0f<\/td>\n<td>\u6781\u81f4\u6027\u80fd\u3001\u5e95\u5c42\u7cfb\u7edf<\/td>\n<\/tr>\n<tr>\n<td>\u7ee7\u627f\u57fa\u7c7b&#xff08;\u5982\u00a0IValue&#xff09;<\/td>\n<td>\u591a\u6001\u6e05\u6670<\/td>\n<td>\u9700\u865a\u51fd\u6570\u3001\u4fb5\u5165\u5f0f\u8bbe\u8ba1<\/td>\n<td>\u5df2\u6709\u7ee7\u627f\u4f53\u7cfb<\/td>\n<\/tr>\n<tr>\n<td>std::variant<\/td>\n<td>\u96f6\u5f00\u9500\u3001\u7f16\u8bd1\u671f\u5df2\u77e5\u7c7b\u578b\u96c6<\/td>\n<td>\u7c7b\u578b\u96c6\u56fa\u5b9a<\/td>\n<td>\u6709\u9650\u7c7b\u578b\u679a\u4e3e&#xff08;\u5982 JSON \u503c&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>boost::any<\/td>\n<td>\u529f\u80fd\u7c7b\u4f3c<\/td>\n<td>\u975e\u6807\u51c6\u3001\u4f9d\u8d56 Boost<\/td>\n<td>\u65e7\u9879\u76ee\u517c\u5bb9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u2705\u00a0\u9009\u62e9\u539f\u5219&#xff1a;<\/p>\n<ul>\n<li>\u7c7b\u578b\u96c6\u56fa\u5b9a \u2192\u00a0std::variant&#xff1b;<\/li>\n<li>\u7c7b\u578b\u5b8c\u5168\u672a\u77e5 \u2192\u00a0std::any&#xff1b;<\/li>\n<li>\u6781\u81f4\u6027\u80fd &#043; \u53ef\u63a7\u73af\u5883 \u2192\u00a0void*&#xff08;\u614e\u7528&#xff09;\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2>\u516d\u3001\u5178\u578b\u5e94\u7528\u573a\u666f<\/h2>\n<h3>6.1 \u901a\u7528\u914d\u7f6e\u7cfb\u7edf<\/h3>\n<p>class\u00a0Config\u00a0{\u00a0 \u00a0 std::unordered_map&lt;std::string, std::any&gt; values;<br \/>\npublic:\u00a0 \u00a0\u00a0template&lt;typename\u00a0T&gt;\u00a0 \u00a0\u00a0void\u00a0set(const\u00a0std::string&amp; key, T value)\u00a0{\u00a0 \u00a0 \u00a0 \u00a0 values[key] &#061; std::move(value);\u00a0 \u00a0 }\u00a0 \u00a0\u00a0template&lt;typename\u00a0T&gt;\u00a0 \u00a0 T\u00a0get(const\u00a0std::string&amp; key)\u00a0const\u00a0{\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0std::any_cast&lt;T&gt;(values.at(key));\u00a0 \u00a0 }};<br \/>\nConfig cfg;cfg.set(&#034;port&#034;,\u00a08080);cfg.set(&#034;debug&#034;,\u00a0true);int\u00a0port &#061; cfg.get&lt;int&gt;(&#034;port&#034;);<\/p>\n<h3>6.2 \u4e8b\u4ef6\u7cfb\u7edf&#xff08;Event Bus&#xff09;<\/h3>\n<p>using\u00a0EventHandler &#061; std::function&lt;void(const\u00a0std::any&amp;)&gt;;<br \/>\nclass\u00a0EventBus\u00a0{\u00a0 \u00a0 std::unordered_map&lt;std::type_index, std::vector&lt;EventHandler&gt;&gt; handlers;<br \/>\npublic:\u00a0 \u00a0\u00a0template&lt;typename\u00a0Event&gt;\u00a0 \u00a0\u00a0void\u00a0subscribe(EventHandler handler)\u00a0{\u00a0 \u00a0 \u00a0 \u00a0 handlers[typeid(Event)].push_back(std::move(handler));\u00a0 \u00a0 }<br \/>\n\u00a0 \u00a0\u00a0template&lt;typename\u00a0Event&gt;\u00a0 \u00a0\u00a0void\u00a0publish(const\u00a0Event&amp; e)\u00a0{\u00a0 \u00a0 \u00a0 \u00a0\u00a0auto\u00a0it &#061; handlers.find(typeid(Event));\u00a0 \u00a0 \u00a0 \u00a0\u00a0if\u00a0(it !&#061; handlers.end()) {\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 std::any event_wrapper &#061; e;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0for\u00a0(auto&amp; h : it-&gt;second)\u00a0h(event_wrapper);\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0 \u00a0 }}; <\/p>\n<h3>6.3 \u63d2\u4ef6\/\u811a\u672c\u7ed1\u5b9a<\/h3>\n<p>\/\/ \u63d2\u4ef6\u8fd4\u56de\u4efb\u610f\u7ed3\u679cstd::any\u00a0call_plugin_function(const\u00a0std::string&amp; name,\u00a0const\u00a0std::vector&lt;std::any&gt;&amp; args);auto\u00a0result &#061;\u00a0call_plugin_function(&#034;calculate&#034;, {42,\u00a0&#034;mode&#034;});if\u00a0(auto* r &#061; std::any_cast&lt;double&gt;(&amp;result)) {\u00a0 \u00a0\u00a0use_result(*r);} <\/p>\n<h2>\u4e03\u3001C&#043;&#043;23 \u91cd\u8981\u66f4\u65b0&#xff1a;std::any \u652f\u6301 Move-Only \u7c7b\u578b<\/h2>\n<p>C&#043;&#043;23 \u901a\u8fc7 P0953R4 \u6269\u5c55\u4e86 std::any&#xff0c;\u4f7f\u5176\u652f\u6301\u4e0d\u53ef\u62f7\u8d1d\u4f46\u53ef\u79fb\u52a8\u7684\u7c7b\u578b&#xff08;\u5982 std::unique_ptr&#xff09;&#xff1a;<\/p>\n<p>\/\/ C&#043;&#043;23 \u8d77\u5408\u6cd5std::any a &#061; std::make_unique&lt;int&gt;(42);auto\u00a0up &#061; std::any_cast&lt;std::unique_ptr&lt;int&gt;&gt;(std::move(a));\u00a0\/\/ \u79fb\u52a8\u63d0\u53d6<\/p>\n<ul>\n<li>\u65b0\u589e\u79fb\u52a8\u6784\u9020\/\u8d4b\u503c\u91cd\u8f7d&#xff1b;<\/li>\n<li>any_cast\u00a0\u652f\u6301\u53f3\u503c\u5f15\u7528\u7248\u672c&#xff1b;<\/li>\n<li>\u5f7b\u5e95\u89e3\u51b3\u4e86 move-only \u7c7b\u578b\u65e0\u6cd5\u5b58\u50a8\u7684\u75db\u70b9\u3002<\/li>\n<\/ul>\n<p>&#x1f4cc;\u00a0\u8fc1\u79fb\u5efa\u8bae&#xff1a;C&#043;&#043;23 \u9879\u76ee\u53ef\u653e\u5fc3\u4f7f\u7528\u00a0any\u00a0\u5b58\u50a8\u667a\u80fd\u6307\u9488\u3001\u6587\u4ef6\u53e5\u67c4\u7b49\u8d44\u6e90\u3002<\/p>\n<hr \/>\n<h2>\u516b\u3001\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<p>\u274c \u9677\u96311&#xff1a;\u8bef\u7528 any_cast \u5bfc\u81f4\u5f02\u5e38<\/p>\n<p>\/\/ \u5371\u9669&#xff1a;\u7c7b\u578b\u9519\u8bef\u629b\u5f02\u5e38int\u00a0x &#061; std::any_cast&lt;int&gt;(a); <\/p>\n<p>\u2705 \u5b89\u5168\u505a\u6cd5&#xff1a;<\/p>\n<p>if\u00a0(auto* p &#061; std::any_cast&lt;int&gt;(&amp;a)) {\u00a0 \u00a0\u00a0int\u00a0x &#061; *p;} <\/p>\n<p>\u274c \u9677\u96312&#xff1a;\u5ffd\u7565 SOO \u8fb9\u754c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u200b\u200b\u200b\u200b\u200b\u200b\u200b<\/p>\n<p>\/\/ \u5b58\u50a8\u5927\u578b\u5bf9\u8c61\u9891\u7e41\u89e6\u53d1\u5806\u5206\u914dstd::any a &#061; huge_object; <\/p>\n<p>\u2705 \u4f18\u5316&#xff1a;\u8003\u8651\u5b58\u50a8\u6307\u9488&#xff08;\u5982 std::shared_ptr&#xff09;\u3002<\/p>\n<p>\u274c \u9677\u96313&#xff1a;\u5728 C&#043;&#043;23 \u524d\u5c1d\u8bd5\u5b58\u50a8 move-only \u7c7b\u578b\u200b\u200b\u200b\u200b\u200b\u200b\u200b<\/p>\n<p>\/\/ C&#043;&#043;20 \u53ca\u4e4b\u524d&#xff1a;\u7f16\u8bd1\u9519\u8bef&#xff01;std::any a &#061; std::make_unique&lt;int&gt;(42); <\/p>\n<p>\u2705 \u66ff\u4ee3\u65b9\u6848&#xff1a;\u4f7f\u7528 std::optional \u5305\u88c5&#xff0c;\u6216\u81ea\u5b9a\u4e49\u7c7b\u578b\u64e6\u9664\u5bb9\u5668\u3002<\/p>\n<p>\u2705 \u6700\u4f73\u5b9e\u8df5\u6e05\u5355&#xff1a;<\/p>\n<li>\u4f18\u5148\u4f7f\u7528\u00a0any_cast&lt;T*&gt;\u00a0\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5&#xff1b;<\/li>\n<li>\u5c0f\u5bf9\u8c61&#xff08;\u226416\u5b57\u8282&#xff09;\u53ef\u9ad8\u6548\u5b58\u50a8&#xff1b;<\/li>\n<li>\u907f\u514d\u5728\u6027\u80fd\u5173\u952e\u8def\u5f84\u4e2d\u9891\u7e41\u62f7\u8d1d\u00a0any&#xff1b;<\/li>\n<li>C&#043;&#043;23 \u8d77\u53ef\u5b89\u5168\u5b58\u50a8 move-only \u7c7b\u578b&#xff1b;<\/li>\n<li>\u4e0e\u00a0std::variant\u00a0\u4e92\u8865\u4f7f\u7528&#xff1a;\u5df2\u77e5\u7c7b\u578b\u96c6\u7528 variant&#xff0c;\u672a\u77e5\u7528 any\u3002<\/li>\n<hr \/>\n<h3><\/h3>\n<h2>\u7ed3\u8bed&#xff1a;\u5728\u9759\u6001\u4e0e\u52a8\u6001\u4e4b\u95f4\u67b6\u6865<\/h2>\n<p>std::any \u5e76\u975e\u8981\u5c06 C&#043;&#043; \u53d8\u6210\u52a8\u6001\u8bed\u8a00&#xff0c;\u800c\u662f\u4e3a\u5f3a\u7c7b\u578b\u7cfb\u7edf\u63d0\u4f9b\u4e00\u79cd\u53d7\u63a7\u7684\u3001\u5b89\u5168\u7684\u9003\u9038\u673a\u5236\u3002\u5b83\u627f\u8ba4\u73b0\u5b9e\u4e16\u754c\u7684\u590d\u6742\u6027\u2014\u2014\u6709\u65f6\u6211\u4eec\u786e\u5b9e\u65e0\u6cd5\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u6240\u6709\u7c7b\u578b&#xff0c;\u4f46\u53c8\u4e0d\u613f\u727a\u7272 C&#043;&#043; \u7684\u6838\u5fc3\u4f18\u52bf&#xff1a;\u6027\u80fd\u3001\u5b89\u5168\u4e0e\u63a7\u5236\u529b\u3002<\/p>\n<p>\u901a\u8fc7\u7cbe\u5de7\u7684\u7c7b\u578b\u64e6\u9664\u4e0e\u5c0f\u5bf9\u8c61\u4f18\u5316&#xff0c;std::any \u5728\u201c\u7075\u6d3b\u6027\u201d\u4e0e\u201c\u6548\u7387\u201d\u4e4b\u95f4\u627e\u5230\u4e86\u4f18\u96c5\u7684\u5e73\u8861\u3002\u638c\u63e1\u5b83&#xff0c;\u610f\u5473\u7740\u4f60\u80fd\u5728\u9700\u8981\u52a8\u6001\u884c\u4e3a\u7684\u573a\u666f\u4e2d&#xff0c;\u4f9d\u7136\u4fdd\u6301 C&#043;&#043; \u7684\u4e25\u8c28\u4e0e\u9ad8\u6548\u3002<\/p>\n<p>\u6b63\u5982\u6807\u51c6\u5e93\u7684\u8bbe\u8ba1\u54f2\u5b66\u6240\u5021\u5bfc&#xff1a;<\/p>\n<p>\u201cDon\u2019t pay for what you don\u2019t use.\u201d \u800c\u00a0std::any&#xff0c;\u6b63\u662f\u8fd9\u4e00\u7406\u5ff5\u5728\u8fd0\u884c\u65f6\u591a\u6001\u9886\u57df\u7684\u5b8c\u7f8e\u4f53\u73b0\u3002<\/p>\n<hr \/>\n<h2>\u9644\u5f55&#xff1a;\u901f\u67e5\u8868<\/h2>\n<table>\n<tr>\u9700\u6c42\u63a8\u8350\u5199\u6cd5<\/tr>\n<tbody>\n<tr>\n<td>\u5b89\u5168\u7c7b\u578b\u68c0\u67e5<\/td>\n<td>if (auto* p &#061; std::any_cast&lt;T&gt;(&amp;a))<\/td>\n<\/tr>\n<tr>\n<td>\u4fee\u6539\u5185\u90e8\u503c<\/td>\n<td>std::any_cast&lt;T&amp;&gt;(a) &#061; new_value;<\/td>\n<\/tr>\n<tr>\n<td>\u907f\u514d\u5f02\u5e38<\/td>\n<td>\u4f7f\u7528\u6307\u9488\u7248\u672c\u00a0any_cast&lt;T*&gt;<\/td>\n<\/tr>\n<tr>\n<td>\u5c31\u5730\u6784\u9020<\/td>\n<td>std::any{std::in_place_type&lt;T&gt;, args&#8230;}<\/td>\n<\/tr>\n<tr>\n<td>\u6e05\u7a7a<\/td>\n<td>a.reset()\u00a0\u6216\u00a0a &#061; {}<\/td>\n<\/tr>\n<tr>\n<td>\u83b7\u53d6\u7c7b\u578b<\/td>\n<td>a.type() &#061;&#061; typeid(T)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><\/h3>\n<h3>\u66f4\u591a\u7cbe\u5f69\u63a8\u8350&#xff1a;<\/h3>\n<p>Android\u5f00\u53d1\u96c6<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009\u4ece AIDL \u5230 HIDL&#xff1a;\u8de8\u8bed\u8a00 Binder \u901a\u4fe1\u7684\u81ea\u52a8\u5316\u6865\u63a5\u4e0e\u96f6\u62f7\u8d1d\u56de\u8c03\u4f18\u5316\u5168\u6808\u6307\u5357<\/p>\n<p>C\/C&#043;&#043;\u7f16\u7a0b\u7cbe\u9009<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009\u5b8f\u4e4b\u53cc\u5203\u5251&#xff1a;C\/C&#043;&#043; \u9884\u5904\u7406\u5668\u5b8f\u7684\u5a01\u529b\u3001\u9677\u9631\u4e0e\u73b0\u4ee3\u5316\u6f14\u8fdb\u5168\u89e3<\/p>\n<p>\u5f00\u6e90\u5de5\u573a\u4e0e\u5de5\u5177\u96c6<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009nlohmann\/json&#xff1a;\u73b0\u4ee3 C&#043;&#043; \u5f00\u53d1\u8005\u7684 JSON \u795e\u5668<\/p>\n<p>MCU\u5185\u6838\u5de5\u574a<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009STM32&#xff1a;\u5d4c\u5165\u5f0f\u4e16\u754c\u7684\u201c\u745e\u58eb\u519b\u5200\u201d\u2014\u2014\u6df1\u5ea6\u89e3\u6790\u610f\u6cd5\u534a\u5bfc\u4f5332\u4f4dMCU\u7684\u67b6\u6784\u6f14\u8fdb\u3001\u751f\u6001\u4f18\u52bf\u4e0e\u5168\u573a\u666f\u5e94\u7528<\/p>\n<p>\u62fe\u5149\u672d\u8bb0\u7c3f<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009\u5468\u672b\u905b\u5a03\u597d\u53bb\u5904&#xff01;\u9ec4\u6cb3\u4e4b\u5dc5\u7545\u4eab\u4eb2\u5b50\u6b22\u4e50\u65f6\u5149<\/p>\n<p>\u6570\u667a\u661f\u6cb3\u96c6<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009\u88ab\u7b97\u6cd5\u76ef\u4e0a\u7684\u5c97\u4f4d&#xff1a;\u4eba\u5de5\u667a\u80fd\u4f18\u5148\u53d6\u4ee3\u7684\u5341\u5927\u804c\u4e1a\u6df1\u5ea6\u89e3\u6790\u4e0e\u4eba\u7c7b\u7a81\u56f4\u8def\u5f84<\/p>\n<p>Docker \u5bb9\u5668<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009Docker \u539f\u7406\u53ca\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879&#xff08;\u7cbe\u8981\u7248&#xff09;<\/p>\n<p>linux\u5f00\u53d1\u96c6<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009\u96f6\u62f7\u8d1d\u4e4b\u738b&#xff1a;Linux splice() \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u9ad8\u6027\u80fd\u5b9e\u6218\u6307\u5357<\/p>\n<p>\u9752\u8863\u67d3\u971c\u534e<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009\u8111\u673a\u63a5\u53e3&#xff1a;\u4ece\u762b\u75ea\u60a3\u8005\u7684\u201c\u610f\u5ff5\u884c\u8d70\u201d\u5230\u4eba\u7c7b\u667a\u80fd\u7684\u4e0b\u4e00\u6b21\u8dc3\u8fc1<\/p>\n<p>QT\u5f00\u53d1\u8bb0\u5f55-\u4e13\u680f<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009Qt \u6837\u5f0f\u8868&#xff08;QSS&#xff09;\u7ec8\u6781\u6307\u5357&#xff1a;\u6253\u9020\u5ab2\u7f8e Web \u7684\u7cbe\u7f8e\u539f\u751f\u754c\u9762<\/p>\n<p>Web\/webassembly\u6280\u672f\u60c5\u62a5\u5c40<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009WebAssembly \u5168\u6808\u900f\u89c6&#xff1a;\u4ece\u5e94\u7528\u5f00\u53d1\u5230\u5e95\u5c42\u6267\u884c\u7684\u5b8c\u6574\u6280\u672f\u94fe\u8def\u4e0e\u6838\u5fc3\u539f\u7406\u6df1\u5ea6\u89e3\u6790<\/p>\n<p>\u6570\u636e\u5e93\u5f00\u53d1<\/p>\n<p>\u9752\u8863\u971c\u534e\u6e21\u767d\u9e3d&#xff0c;\u516c\u4f17\u53f7&#xff1a;\u6e05\u8377\u96c5\u96c6-\u58a8\u67d3\u4f18\u9009ARM Linux \u4e0b SQLite3 \u6570\u636e\u5e93\u4f7f\u7528\u5168\u65b9\u4f4d\u6307\u5357<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f3a\u7c7b\u578b\u3001\u9759\u6001\u7f16\u8bd1\u7684 C \u4e16\u754c\u4e2d&#xff0c;\u5b89\u5168\u5730\u5b58\u50a8\u548c\u64cd\u4f5c\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\u59cb\u7ec8\u662f\u4e00\u9879\u6311\u6218\u3002\u4f20\u7edf\u65b9\u6848\u5982 void* \u7f3a\u4e4f\u7c7b\u578b\u5b89\u5168&#xff0c;union \u4ec5\u9650\u5e73\u51e1\u7c7b\u578b&#xff0c;\u800c\u7ee7\u627f\u4f53\u7cfb&#xff08;\u5982 boost::any \u7684\u65e9\u671f\u5b9e\u73b0&#xff09;\u53c8\u5f15\u5165\u865a\u51fd\u6570\u5f00\u9500\u4e0e\u8bbe\u8ba1\u8026\u5408\u3002\u4e3a\u89e3\u51b3\u8fd9\u4e00\u6839\u672c\u6027\u95ee\u9898&#xff0c;C17 \u6b63\u5f0f\u5f15\u5165\u4e86std::any\u2014\u2014 \u4e00\u4e2a\u7c7b\u578b\u5b89\u5168\u3001\u503c\u8bed\u4e49\u3001\u652f\u6301\u4efb\u610f\u62f7\u8d1d\u6784\u9020\u7c7b\u578b\u7684\u901a\u7528\u5bb9\u5668\u3002<br \/>\nstd::any \u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e\u8fd0\u884c\u65f6\u7c7b\u578b\u64e6\u9664&amp;#xf<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[53,84,55,58,269,190,5029],"topic":[],"class_list":["post-70773","post","type-post","status-publish","format-standard","hentry","category-server","tag-ai","tag-android","tag-c","tag-linux","tag-269","tag-190","tag-5029"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7c7b\u578b\u64e6\u9664\u7684\u4f18\u96c5\u5b9e\u73b0\uff1aC++ \uff1cany\uff1e \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u8fd0\u884c\u65f6\u591a\u6001\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/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:\/\/www.wsisp.com\/helps\/70773.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7c7b\u578b\u64e6\u9664\u7684\u4f18\u96c5\u5b9e\u73b0\uff1aC++ \uff1cany\uff1e \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u8fd0\u884c\u65f6\u591a\u6001\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5728\u5f3a\u7c7b\u578b\u3001\u9759\u6001\u7f16\u8bd1\u7684 C \u4e16\u754c\u4e2d&#xff0c;\u5b89\u5168\u5730\u5b58\u50a8\u548c\u64cd\u4f5c\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\u59cb\u7ec8\u662f\u4e00\u9879\u6311\u6218\u3002\u4f20\u7edf\u65b9\u6848\u5982 void* \u7f3a\u4e4f\u7c7b\u578b\u5b89\u5168&#xff0c;union \u4ec5\u9650\u5e73\u51e1\u7c7b\u578b&#xff0c;\u800c\u7ee7\u627f\u4f53\u7cfb&#xff08;\u5982 boost::any \u7684\u65e9\u671f\u5b9e\u73b0&#xff09;\u53c8\u5f15\u5165\u865a\u51fd\u6570\u5f00\u9500\u4e0e\u8bbe\u8ba1\u8026\u5408\u3002\u4e3a\u89e3\u51b3\u8fd9\u4e00\u6839\u672c\u6027\u95ee\u9898&#xff0c;C17 \u6b63\u5f0f\u5f15\u5165\u4e86std::any\u2014\u2014 \u4e00\u4e2a\u7c7b\u578b\u5b89\u5168\u3001\u503c\u8bed\u4e49\u3001\u652f\u6301\u4efb\u610f\u62f7\u8d1d\u6784\u9020\u7c7b\u578b\u7684\u901a\u7528\u5bb9\u5668\u3002 std::any \u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e\u8fd0\u884c\u65f6\u7c7b\u578b\u64e6\u9664&amp;#xf\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/70773.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-02T03:17:19+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/70773.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/70773.html\",\"name\":\"\u7c7b\u578b\u64e6\u9664\u7684\u4f18\u96c5\u5b9e\u73b0\uff1aC++ \uff1cany\uff1e \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u8fd0\u884c\u65f6\u591a\u6001\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-02T03:17:19+00:00\",\"dateModified\":\"2026-02-02T03:17:19+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/70773.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/70773.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/70773.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7c7b\u578b\u64e6\u9664\u7684\u4f18\u96c5\u5b9e\u73b0\uff1aC++ \uff1cany\uff1e \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u8fd0\u884c\u65f6\u591a\u6001\u5b9e\u6218\u6307\u5357\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u7c7b\u578b\u64e6\u9664\u7684\u4f18\u96c5\u5b9e\u73b0\uff1aC++ \uff1cany\uff1e \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u8fd0\u884c\u65f6\u591a\u6001\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","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:\/\/www.wsisp.com\/helps\/70773.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7c7b\u578b\u64e6\u9664\u7684\u4f18\u96c5\u5b9e\u73b0\uff1aC++ \uff1cany\uff1e \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u8fd0\u884c\u65f6\u591a\u6001\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5728\u5f3a\u7c7b\u578b\u3001\u9759\u6001\u7f16\u8bd1\u7684 C \u4e16\u754c\u4e2d&#xff0c;\u5b89\u5168\u5730\u5b58\u50a8\u548c\u64cd\u4f5c\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\u59cb\u7ec8\u662f\u4e00\u9879\u6311\u6218\u3002\u4f20\u7edf\u65b9\u6848\u5982 void* \u7f3a\u4e4f\u7c7b\u578b\u5b89\u5168&#xff0c;union \u4ec5\u9650\u5e73\u51e1\u7c7b\u578b&#xff0c;\u800c\u7ee7\u627f\u4f53\u7cfb&#xff08;\u5982 boost::any \u7684\u65e9\u671f\u5b9e\u73b0&#xff09;\u53c8\u5f15\u5165\u865a\u51fd\u6570\u5f00\u9500\u4e0e\u8bbe\u8ba1\u8026\u5408\u3002\u4e3a\u89e3\u51b3\u8fd9\u4e00\u6839\u672c\u6027\u95ee\u9898&#xff0c;C17 \u6b63\u5f0f\u5f15\u5165\u4e86std::any\u2014\u2014 \u4e00\u4e2a\u7c7b\u578b\u5b89\u5168\u3001\u503c\u8bed\u4e49\u3001\u652f\u6301\u4efb\u610f\u62f7\u8d1d\u6784\u9020\u7c7b\u578b\u7684\u901a\u7528\u5bb9\u5668\u3002 std::any \u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e\u8fd0\u884c\u65f6\u7c7b\u578b\u64e6\u9664&amp;#xf","og_url":"https:\/\/www.wsisp.com\/helps\/70773.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-02T03:17:19+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/70773.html","url":"https:\/\/www.wsisp.com\/helps\/70773.html","name":"\u7c7b\u578b\u64e6\u9664\u7684\u4f18\u96c5\u5b9e\u73b0\uff1aC++ \uff1cany\uff1e \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u8fd0\u884c\u65f6\u591a\u6001\u5b9e\u6218\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-02T03:17:19+00:00","dateModified":"2026-02-02T03:17:19+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/70773.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/70773.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/70773.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7c7b\u578b\u64e6\u9664\u7684\u4f18\u96c5\u5b9e\u73b0\uff1aC++ \uff1cany\uff1e \u5168\u9762\u6df1\u5ea6\u89e3\u6790\u4e0e\u8fd0\u884c\u65f6\u591a\u6001\u5b9e\u6218\u6307\u5357"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/70773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=70773"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/70773\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=70773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=70773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=70773"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=70773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}