{"id":147,"date":"2012-11-09T16:39:04","date_gmt":"2012-11-09T08:39:04","guid":{"rendered":"http:\/\/www.freezhongzi.info\/?p=147"},"modified":"2012-11-09T16:39:04","modified_gmt":"2012-11-09T08:39:04","slug":"c-trivial%e5%92%8cnon-trivial%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e5%8f%8apod%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/bl4kraven.com\/index.php\/2012\/11\/09\/c-trivial%e5%92%8cnon-trivial%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e5%8f%8apod%e7%b1%bb%e5%9e%8b\/","title":{"rendered":"C++ trivial\u548cnon-trivial\u6784\u9020\u51fd\u6570\u53caPOD\u7c7b\u578b"},"content":{"rendered":"<p>\u4eca\u5929\u770b\u4e66\u770b\u5230\u4faf\u6377\u7684\u300aSTL\u6e90\u7801\u5256\u6790\u300b\u91cc\u63d0\u5230trivial\u548cnon-trivial\u53caPOD\u7c7b\u578b\uff0c\u67e5\u4e86\u4e9b\u8d44\u6599\u7406\u89e3\u4e86\u4e00\u4e0b\u3002<\/p>\n<p>trivial\u610f\u601d\u662f\u65e0\u610f\u4e49\uff0c\u8fd9\u4e2atrivial\u548cnon-trivial\u662f\u5bf9\u7c7b\u7684\u56db\u79cd\u51fd\u6570\u6765\u8bf4\u7684\uff1a<\/p>\n<ul>\n<li>\u6784\u9020\u51fd\u6570(ctor)<\/li>\n<li>\u590d\u5236\u6784\u9020\u51fd\u6570(copy)<\/li>\n<li>\u8d4b\u503c\u51fd\u6570(assignment)<\/li>\n<li>\u6790\u6784\u51fd\u6570(dtor)<\/li>\n<\/ul>\n<p>\u5982\u679c\u81f3\u5c11\u6ee1\u8db3\u4e0b\u97623\u6761\u91cc\u7684\u4e00\u6761\uff1a<\/p>\n<ol>\n<li>\u663e\u5f0f(explict)\u5b9a\u4e49\u4e86\u8fd9\u56db\u79cd\u51fd\u6570\u3002<\/li>\n<li>\u7c7b\u91cc\u6709\u975e\u9759\u6001\u975ePOD\u7684\u6570\u636e\u6210\u5458\u3002<\/li>\n<li>\u6709\u57fa\u7c7b\u3002<\/li>\n<\/ol>\n<p>\u90a3\u4e48\u4e0a\u9762\u7684\u56db\u79cd\u51fd\u6570\u662fnon-trivial\u51fd\u6570\uff0c\u6bd4\u5982\u53ebnon-trivial ctor\u3001non-trivial copy&#8230;\uff0c\u4e5f\u5c31\u662f\u8bf4\u6709\u610f\u4e49\u7684\u51fd\u6570\uff0c\u91cc\u9762\u6709\u4e00\u4e0b\u5fc5\u8981\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u7c7b\u6210\u5458\u7684\u521d\u59cb\u5316\uff0c\u91ca\u653e\u5185\u5b58\u7b49\u3002<\/p>\n<p>\u90a3\u4e2aPOD\u610f\u601d\u662fPlain Old Data\uff0c\u4e5f\u5c31\u662fC++\u7684\u5185\u5efa\u7c7b\u578b\u6216\u4f20\u7edf\u7684C\u7ed3\u6784\u4f53\u7c7b\u578b\u3002POD\u7c7b\u578b\u5fc5\u7136\u6709trivial ctor\/dtor\/copy\/assignment\u56db\u79cd\u51fd\u6570\u3002<\/p>\n<pre><code>\/\/\u6574\u4e2aT\u662fPOD\u7c7b\u578b\nclass T\n{\n    \/\/\u6ca1\u6709\u663e\u5f0f\u5b9a\u4e49ctor\/dtor\/copy\/assignemt\u6240\u4ee5\u90fd\u662ftrivial\n    int a; \/\/POD\u7c7b\u578b\n};\n\n\/\/\u6574\u4e2aT1\u662f\u975ePOD\u7c7b\u578b\nclass T1\n{\n    T1() \/\/\u663e\u5f0f\u5b9a\u4e49\u4e86\u6784\u9020\u51fd\u6570\uff0c\u6240\u4ee5\u662fnon-trivial ctor\n    {}\n    \/\/\u6ca1\u6709\u663e\u5f0f\u5b9a\u4e49ctor\/dtor\/copy\/assignemt\u6240\u4ee5\u90fd\u662ftrivial\n    int a;\/\/POD\u7c7b\u578b\n    std::string b; \/\/\u975ePOD\u7c7b\u578b\n};\n<\/code><\/pre>\n<p>\u90a3\u8fd9\u6709\u4ec0\u4e48\u7528\u5904\u5462\uff1f<\/p>\n<p>\u5982\u679c\u8fd9\u4e2a\u7c7b\u90fd\u662ftrivial ctor\/dtor\/copy\/assignment\u51fd\u6570\uff0c\u6211\u4eec\u5bf9\u8fd9\u4e2a\u7c7b\u8fdb\u884c\u6784\u9020\u3001\u6790\u6784\u3001\u62f7\u8d1d\u548c\u8d4b\u503c\u65f6\u53ef\u4ee5\u91c7\u7528\u6700\u6709\u6548\u7387\u7684\u65b9\u6cd5\uff0c\u4e0d\u8c03\u7528\u65e0\u6240\u4e8b\u4e8b\u6b63\u771f\u7684\u90a3\u4e9bctor\/dtor\u7b49\uff0c\u800c\u76f4\u63a5\u91c7\u7528\u5185\u5b58\u64cd\u4f5c\u5982malloc()\u3001memcpy()\u7b49\u63d0\u9ad8\u6027\u80fd\uff0c\u8fd9\u4e5f\u662fSGI STL\u5185\u90e8\u5e72\u7684\u4e8b\u60c5\u3002<\/p>\n<p>\u6bd4\u5982STL\u7684copy\u7b97\u6cd5\u6700\u57fa\u672c\u7684\u60f3\u6cd5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code>\/\/ \u975ePOD\u91cd\u8f7d\u6307\u9488\u6570\u503c\ntemplate &lt;class T&gt; void copy(T* source, T* destination, int n, __false_type)\n{\n    \/\/ \u7701\u7565\u5f02\u5e38\u5904\u7406\n    for (; n &gt; 0; n--,source++,destination++)\n    {\n        \/\/ \u8c03\u7528source\u7684\u590d\u5236\u6784\u9020\u51fd\u6570\n        constructor(source, *destination);\n    }\n}\n\n\/\/ POD\u91cd\u8f7d\u6307\u9488\u6570\u503c\ntemplate &lt;class T&gt; void copy(T* source, T* destination, int n, __false_type)\n{\n    \/\/ \u7701\u7565\u5f02\u5e38\u5904\u7406\n    memmove(source, destination, n);\n}\n<\/code><\/pre>\n<p>\u5f53\u7136\u5b9e\u9645\u7684copy\u6bd4\u8fd9\u4e2a\u590d\u6742\u591a\u4e86\uff0c\u6709\u975e\u5e38\u591a\u7684\u7279\u5316\u7b49\uff0c\u8fd9\u4e2a\u53ea\u662f\u5176\u4e2d\u4e00\u65b9\u9762\u800c\u5df2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u770b\u4e66\u770b\u5230\u4faf\u6377\u7684\u300aSTL\u6e90\u7801\u5256\u6790\u300b\u91cc\u63d0\u5230trivial\u548cnon-trivial &hellip; <a href=\"https:\/\/bl4kraven.com\/index.php\/2012\/11\/09\/c-trivial%e5%92%8cnon-trivial%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e5%8f%8apod%e7%b1%bb%e5%9e%8b\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a> <a href=\"https:\/\/bl4kraven.com\/index.php\/2012\/11\/09\/c-trivial%e5%92%8cnon-trivial%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e5%8f%8apod%e7%b1%bb%e5%9e%8b\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-program"],"_links":{"self":[{"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":0,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"wp:attachment":[{"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bl4kraven.com\/index.php\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}