{"id":344590,"date":"2021-02-14T05:02:52","date_gmt":"2021-02-13T21:02:52","guid":{"rendered":"http:\/\/4563.org\/?p=344590"},"modified":"2021-02-14T05:02:52","modified_gmt":"2021-02-13T21:02:52","slug":"c%e4%b8%ad%e7%9a%84%e4%b8%80%e4%b8%aa-ambiguous-call-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=344590","title":{"rendered":"C++\u4e2d\u7684\u4e00\u4e2a ambiguous call \u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C++\u4e2d\u7684\u4e00\u4e2a ambiguous call \u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dangyuluo <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6700\u8fd1\u5de5\u4f5c\u4e2d\u9047\u5230\u4e86\u4e00\u4e2a ambiguous call \u7684\u95ee\u9898\uff0c\u60f3\u8bf7\u6559\u4e00\u4e0b\u5404\u4f4d\u3002<\/p>\n<p>\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a<code>myString<\/code>\u7c7b\uff08\u8fd9\u4e2a\u7c7b\u4e0d\u662f\u91cd\u70b9\uff09\u3002\u6211\u4eec\u60f3\u5b9e\u73b0 C String \u4e0e <code>myString<\/code>\u62fc\u63a5(<code>operator+<\/code>)\uff0c\u56e0\u6b64\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5168\u5c40\u51fd\u6570\u3002<\/p>\n<pre><code> template&lt;size_t CSTR_LEN&gt; auto operator+(   const char (&amp; lhs)[CSTR_LEN],   const myString&amp; rhs) {   ... }  auto operator+(   const char* const lhs,   const myString&amp; rhs) {   ... } <\/code><\/pre>\n<p>\u7b2c\u4e00\u4e2a<code>operaot+<\/code>\u6709\u6a21\u7248\u53c2\u6570\uff08\u53c2\u89c1link\uff09\uff0c\u51fd\u6570\u53c2\u6570\u4e3a\u5bf9\u4e00\u4e2a<code>const char[]<\/code>\u7684\u5f15\u7528\u3002\u7b2c\u4e8c\u4e2a\u76f4\u63a5\u63a5\u6536\u4e00\u4e2a<code>const char* const<\/code>\u3002<\/p>\n<p>\u5728\u6d4b\u8bd5\u4ee3\u7801\u91cc\uff0c\u5982\u4e0b\u4ee3\u7801\u4f1a\u53d1\u751f ambiguous call<\/p>\n<pre><code>auto test2 = \"hello\" + myString(\"world\"); <\/code><\/pre>\n<p>\u7f16\u8bd1\u5668\u4f1a\u8ba4\u4e3a<code>\"hello\"<\/code>\u65e2\u53ef\u4ee5\u5f53\u4f5c\u4e00\u4e2a<code>const char (&amp; lhs)[CSTR_LEN]<\/code>\u5e76\u81ea\u52a8\u63a8\u5bfc\u51fa\u4e86<code>CSTR_LEN<\/code>\uff0c\u53c8\u53ef\u4ee5\u5f53\u4f5c\u4e00\u4e2a<code>const char* const<\/code>\u3002<\/p>\n<p>\u8bf7\u95ee\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6709\u6ca1\u6709\u4ec0\u4e48\u529e\u6cd5\u80fd\u8ba9\u7f16\u8bd1\u5668\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\uff0c\u56e0\u4e3a\u4ed6\u66f4\u5408\u9002\u5462\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5313699\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ysc3839 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             std::enable_if?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313700\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : geelaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u66f4\u597d\u7684\u95ee\u9898\u662f\uff1a\u77e5\u9053\u6570\u7ec4\u957f\u5ea6\u6709\u4ec0\u4e48\u7528\uff1f<\/p>\n<p>\u65e2\u7136\u662f C string\uff0c\u90a3\u5fc5\u7136\u662f null-terminated\uff0c\u77e5\u9053\u6570\u7ec4\u957f\u5ea6\u65e0\u610f\u4e49\uff0c\u53cd\u6b63\u90fd\u9700\u8981\u901a\u8fc7\u904d\u5386\u627e\u5230\u6b63\u786e\u957f\u5ea6\u3002\u5982\u679c\u6570\u7ec4\u957f\u5ea6\u5185\u6ca1\u6709\u51fa\u73b0\u7a7a\u5b57\u7b26\uff0c\u8bf4\u660e\u4f20\u5165\u7684\u4e0d\u662f C string\uff0c\u6839\u636e garbage-in garbage-out \u539f\u5219\u6b64\u65f6\u76f4\u63a5\u5b9a\u4e49\u4e3a\u672a\u5b9a\u4e49\u884c\u4e3a\u5373\u53ef\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313701\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : inhzus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u540c\u610f\u4e0a\uff0c\u4e0d\u8fc7\u5982\u679c\u771f\u7684\u8981\u8fd9\u6837\u5b9e\u73b0\u7684\u8bdd\uff0c\u7528 SFINAE is_pointer \u6216\u8005\u504f\u7279\u5316 T* \u548c T [N] \u4e5f\u662f\u53ef\u4ee5\u5b9e\u73b0\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313702\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : framlog <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76f4\u63a5\u628a\u7b2c\u4e00\u4e2a\u7c7b\u578b\u505a\u6210\u6a21\u7248\uff0c\u7136\u540e enable_if                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313703\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wutiantong <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u518d\u63d0\u4f9b\u4e00\u4e2a\u89d2\u5ea6\uff0cC++17 \u7684 string_view \u5c31\u662f\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5313704\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : linux40 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528\u4e24\u4e2a\u91cd\u8f7d\u51fd\u6570 execute_operator_add\uff0c\u7b2c\u4e00\u4e2a\u91cd\u8f7d\u63a5\u6536\u6570\u7ec4\uff0c\u591a\u4e00\u4e2a\u53c2\u6570 int\uff0c\u800c\u7b2c\u4e8c\u4e2a\u91cd\u8f7d\u63a5\u6536\u6307\u9488\uff0c\u5728\u4e24\u4e2a\u53c2\u6570\u540e\u9762\u52a0 &#8230; \u3002\u7136\u540e\u628a operator+ \u4f5c\u4e3a\u5305\u88f9\u51fd\u6570\u8c03\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002<\/p>\n<p>execute_operator_add(lhs, rhs, 0);                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C++\u4e2d\u7684\u4e00\u4e2a ambiguous&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\/344590"}],"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=344590"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344590\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344590"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}