8000 Cpp17/markdown/src/ch12.md at master · MeouSker77/Cpp17 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
< 66DB script type="application/json" data-target="react-app.embeddedData">{"payload":{"allShortcutsEnabled":false,"fileTree":{"markdown/src":{"items":[{"name":"ch01.md","path":"markdown/src/ch01.md","contentType":"file"},{"name":"ch02.md","path":"markdown/src/ch02.md","contentType":"file"},{"name":"ch03.md","path":"markdown/src/ch03.md","contentType":"file"},{"name":"ch04.md","path":"markdown/src/ch04.md","contentType":"file"},{"name":"ch05.md","path":"markdown/src/ch05.md","contentType":"file"},{"name":"ch06.md","path":"markdown/src/ch06.md","contentType":"file"},{"name":"ch07.md","path":"markdown/src/ch07.md","contentType":"file"},{"name":"ch08.md","path":"markdown/src/ch08.md","contentType":"file"},{"name":"ch09.md","path":"markdown/src/ch09.md","contentType":"file"},{"name":"ch10.md","path":"markdown/src/ch10.md","contentType":"file"},{"name":"ch11.md","path":"markdown/src/ch11.md","contentType":"file"},{"name":"ch12.md","path":"markdown/src/ch12.md","contentType":"file"},{"name":"ch13.md","path":"markdown/src/ch13.md","contentType":"file"},{"name":"ch14.md","path":"markdown/src/ch14.md","contentType":"file"},{"name":"ch15.md","path":"markdown/src/ch15.md","contentType":"file"},{"name":"ch16.md","path":"markdown/src/ch16.md","contentType":"file"},{"name":"ch17.md","path":"markdown/src/ch17.md","contentType":"file"},{"name":"ch18.md","path":"markdown/src/ch18.md","contentType":"file"},{"name":"ch19.md","path":"markdown/src/ch19.md","contentType":"file"},{"name":"ch20.md","path":"markdown/src/ch20.md","contentType":"file"},{"name":"ch21.md","path":"markdown/src/ch21.md","contentType":"file"},{"name":"ch22.md","path":"markdown/src/ch22.md","contentType":"file"},{"name":"ch23.md","path":"markdown/src/ch23.md","contentType":"file"},{"name":"ch24.md","path":"markdown/src/ch24.md","contentType":"file"},{"name":"ch25.md","path":"markdown/src/ch25.md","contentType":"file"},{"name":"ch26.md","path":"markdown/src/ch26.md","contentType":"file"},{"name":"ch27.md","path":"markdown/src/ch27.md","contentType":"file"},{"name":"ch28.md","path":"markdown/src/ch28.md","contentType":"file"},{"name":"ch29.md","path":"markdown/src/ch29.md","contentType":"file"},{"name":"ch30.md","path":"markdown/src/ch30.md","contentType":"file"},{"name":"ch31.md","path":"markdown/src/ch31.md","contentType":"file"},{"name":"ch32.md","path":"markdown/src/ch32.md","contentType":"file"},{"name":"ch33.md","path":"markdown/src/ch33.md","contentType":"file"},{"name":"ch34.md","path":"markdown/src/ch34.md","contentType":"file"},{"name":"ch35.md","path":"markdown/src/ch35.md","contentType":"file"}],"totalCount":35},"markdown":{"items":[{"name":"imgs","path":"markdown/imgs","contentType":"directory"},{"name":"src","path":"markdown/src","contentType":"directory"}],"totalCount":2},"":{"items":[{"name":".github","path":".github","contentType":"directory"},{"name":"latex","path":"latex","contentType":"directory"},{"name":"markdown","path":"markdown","contentType":"directory"},{"name":".gitignore","path":".gitignore","contentType":"file"},{"name":"README.md","path":"README.md","contentType":"file"}],"totalCount":5}},"fileTreeProcessingTime":5.522627,"foldersToFetch":[],"incompleteFileTree":false,"repo":{"id":213407059,"defaultBranch":"master","name":"Cpp17","ownerLogin":"MeouSker77","currentUserCanPush":false,"isFork":false,"isEmpty":false,"createdAt":"2019-10-07T14:34:27.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/34214113?v=4","public":true,"private":false,"isOrgOwned":false},"codeLineWrapEnabled":false,"symbolsExpanded":false,"treeExpanded":true,"refInfo":{"name":"master","listCacheKey":"v0:1682916991.0","canEdit":false,"refType":"branch","currentOid":"66dd028887eb962509a3a9f329435ecef6783278"},"path":"markdown/src/ch12.md","currentUser":null,"blob":{"rawLines":null,"stylingDirectives":null,"colorizedLines":null,"csv":null,"csvError":null,"dependabotInfo":{"showConfigurationBanner":false,"configFilePath":null,"networkDependabotPath":"/MeouSker77/Cpp17/network/updates","dismissConfigurationNoticePath":"/settings/dismiss-notice/dependabot_configuration_notice","configurationNoticeDismissed":null},"displayName":"ch12.md","displayUrl":"https://github.com/MeouSker77/Cpp17/blob/master/markdown/src/ch12.md?raw=true","headerInfo":{"blobSize":"1.65 KB","deleteTooltip":"You must be signed in to make or propose changes","editTooltip":"You must be signed in to make or propose changes","ghDesktopPath":"https://desktop.github.com","isGitLfs":false,"onBranch":true,"shortPath":"f0fda35","siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2FMeouSker77%2FCpp17%2Fblob%2Fmaster%2Fmarkdown%2Fsrc%2Fch12.md","isCSV":false,"isRichtext":true,"toc":[{"level":1,"text":"Chapter12 处理字符串字面量模板参数","anchor":"chapter12-处理字符串字面量模板参数","htmlText":"Chapter12 处理字符串字面量模板参数"},{"level":2,"text":"12.1 在模板中使用字符串","anchor":"121-在模板中使用字符串","htmlText":"12.1 在模板中使用字符串"}],"lineInfo":{"truncatedLoc":"50","truncatedSloc":"42"},"mode":"file"},"image":false,"isCodeownersFile":null,"isPlain":false,"isValidLegacyIssueTemplate":false,"issueTemplate":null,"discussionTemplate":null,"language":"Markdown","languageID":222,"large":false,"planSupportInfo":{"repoIsFork":null,"repoOwnedByCurrentUser":null,"requestFullPath":"/MeouSker77/Cpp17/blob/master/markdown/src/ch12.md","showFreeOrgGatedFeatureMessage":null,"showPlanSupportBanner":null,"upgradeDataAttributes":null,"upgradePath":null},"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_dockerfile","releasePath":"/MeouSker77/Cpp17/releases/new?marketplace=true","showPublishActionBanner":false},"rawBlobUrl":"https://github.com/MeouSker77/Cpp17/raw/refs/heads/master/markdown/src/ch12.md","renderImageOrRaw":false,"richText":"\u003carticle class=\"markdown-body entry-content container-lg\" itemprop=\"text\"\u003e\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eChapter12 处理字符串字面量模板参数\u003c/h1\u003e\u003ca id=\"user-content-chapter12-处理字符串字面量模板参数\" class=\"anchor\" aria-label=\"Permalink: Chapter12 处理字符串字面量模板参数\" href=\"#chapter12-处理字符串字面量模板参数\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e一直以来,不同版本的C++标准一直在放宽模板参数的标准,C++17也是如此。\n另外现在非类型模板参数的实参不需要再定义在使用处的外层作用域。\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003e12.1 在模板中使用字符串\u003c/h2\u003e\u003ca id=\"user-content-121-在模板中使用字符串\" class=\"anchor\" aria-label=\"Permalink: 12.1 在模板中使用字符串\" href=\"#121-在模板中使用字符串\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e非类型模板参数只能是常量整数值(包括枚举)、对象/函数/成员的指针、对象或函数的左值引用、\n\u003ccode\u003estd::nullptr_t\u003c/code\u003e(\u003ccode\u003enullptr\u003c/code\u003e的类型)。\u003c/p\u003e\n\u003cp dir=\"auto\"\u003e对于指针,在C++17之前需要外部或者内部链接。然而,自从C++17起,可以使用无链接的指针。\n然而,你仍然不能直接使用字符串字面量。例如:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-c++ notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"template\u0026lt;const char* str\u0026gt;\nclass Message {\n ...\n};\n\nextern const char hello[] = \u0026quot;Hello World!\u0026quot;; // 外部链接\nconst char hello11[] = \u0026quot;Hello World!\u0026quot;; // 内部链接\n\nvoid foo()\n{\n Message\u0026lt;hello\u0026gt; msg; // OK(所有C++标准)\n Message\u0026lt;hello11\u0026gt; msg11; // 自从C++11起OK\n\n static const char hello17[] = \u0026quot;Hello World!\u0026quot;; // 无链接\n Message\u0026lt;hello17\u0026gt; msg17; // 自从C++17起OK\n\n Message\u0026lt;\u0026quot;hi\u0026quot;\u0026gt; msgError; // ERROR\n}\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003etemplate\u003c/span\u003e\u0026lt;\u003cspan class=\"pl-k\"\u003econst\u003c/span\u003e \u003cspan class=\"pl-k\"\u003echar\u003c/span\u003e* str\u0026gt;\n\u003cspan class=\"pl-k\"\u003eclass\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eMessage\u003c/span\u003e {\n ...\n};\n\n\u003cspan class=\"pl-k\"\u003eextern\u003c/span\u003e \u003cspan class=\"pl-k\"\u003econst\u003c/span\u003e \u003cspan class=\"pl-k\"\u003echar\u003c/span\u003e hello[] = \u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003eHello World!\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e; \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e//\u003c/span\u003e 外部链接\u003c/span\u003e\n\u003cspan class=\"pl-k\"\u003econst\u003c/span\u003e \u003cspan class=\"pl-k\"\u003echar\u003c/span\u003e hello11[] = \u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003eHello World!\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e; \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e//\u003c/span\u003e 内部链接\u003c/span\u003e\n\n\u003cspan class=\"pl-k\"\u003evoid\u003c/span\u003e \u003cspan class=\"pl-en\"\u003efoo\u003c/span\u003e()\n{\n Message\u0026lt;hello\u0026gt; msg; \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e//\u003c/span\u003e OK(所有C++标准)\u003c/span\u003e\n Message\u0026lt;hello11\u0026gt; msg11; \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e//\u003c/span\u003e 自从C++11起OK\u003c/span\u003e\n\n \u003cspan class=\"pl-k\"\u003estatic\u003c/span\u003e \u003cspan class=\"pl-k\"\u003econst\u003c/span\u003e \u003cspan class=\"pl-k\"\u003echar\u003c/span\u003e hello17[] = \u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003eHello World!\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e; \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e//\u003c/span\u003e 无链接\u003c/span\u003e\n Message\u0026lt;hello17\u0026gt; msg17; \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e//\u003c/span\u003e 自从C++17起OK\u003c/span\u003e\n\n Message\u0026lt;\u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003ehi\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e\u0026gt; msgError; \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e//\u003c/span\u003e ERROR\u003c/span\u003e\n}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e也就是说自从C++17起你仍然需要至少两行才能把字符串字面量传给一个模板参数。\n然而,你现在可以把第一行写在和实例化代码相同的作用域内。\u003c/p\u003e\n\u003cp dir=\"auto\"\u003e这个特性还解决了一个不幸的约束:自从C++11起可以把一个指针作为模板实参:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-c++ notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"template\u0026lt;int* p\u0026gt; struct A {\n};\n\nint num;\nA\u0026lt;\u0026amp;num\u0026gt; a; // 自从C++11起OK\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003etemplate\u003c/span\u003e\u0026lt;\u003cspan class=\"pl-k\"\u003eint\u003c/span\u003e* p\u0026gt; \u003cspan class=\"pl-k\"\u003estruct\u003c/span\u003e \u003cspan class=\"pl-en\"\u003eA\u003c/span\u003e {\n};\n\n\u003cspan class=\"pl-k\"\u003eint\u003c/span\u003e num;\nA\u0026lt;\u0026amp;num\u0026gt; a; \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e//\u003c/span\u003e 自从C++11起OK\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e但不能用一个返回指针的编译期函数作为模板实参,然而现在可以了:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-source-c++ notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"int num;\n...\nconstexpr int* pNum() {\n return \u0026amp;num;\n}\nA\u0026lt;pNum()\u0026gt; b; // C++17之前ERROR,现在OK\"\u003e\u003cpre\u003e\u003cspan class=\"pl-k\"\u003eint\u003c/span\u003e num;\n...\n\u003cspan class=\"pl-k\"\u003econstexpr\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eint\u003c/span\u003e* \u003cspan class=\"pl-en\"\u003epNum\u003c/span\u003e() {\n \u003cspan class=\"pl-k\"\u003ereturn\u003c/span\u003e \u0026amp;num;\n}\nA\u0026lt;pNum()\u0026gt; b; \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e//\u003c/span\u003e C++17之前ERROR,现在OK\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003c/article\u003e","renderedFileInfo":null,"shortPath":null,"symbolsEnabled":true,"tabSize":8,"topBannersInfo":{"overridingGlobalFundingFile":false,"globalPreferredFundingPath":null,"showInvalidCitationWarning":false,"citationHelpUrl":"https://docs.github.com/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-citation-files","actionsOnboardingTip":null},"truncated":false,"viewable":true,"workflowRedirectUrl":null,"symbols":null},"copilotInfo":null,"copilotAccessAllowed":false,"modelsAccessAllowed":false,"modelsRepoIntegrationEnabled":false,"isMarketplaceEnabled":true,"csrf_tokens":{"/MeouSker77/Cpp17/branches":{"post":"LXaIXYseIQgtwUNwh1iDnBLQ1541f9CtwySOqHu5XhAhtlRLBiy4u7f0M05fDSEJLWENL9BFNNKJRXATiSJADw"},"/repos/preferences":{"post":"4Ltv-X0TmjtE38cZZgiTBqo5nK89SkXWKYQ6VhkD9Bgbt9tolgjczLWdkXMZoQzCq7I6MMRz7Gb8bL_fVxJqiQ"}}},"title":"Cpp17/markdown/src/ch12.md at master · MeouSker77/Cpp17","appPayload":{"helpUrl":"https://docs.github.com","findFileWorkerPath":"/assets-cdn/worker/find-file-worker-263cab1760dd.js","findInFileWorkerPath":"/assets-cdn/worker/find-in-file-worker-41d93d549163.js","githubDevUrl":null,"enabled_features":{"code_nav_ui_events":false,"react_blob_overlay":false,"accessible_code_button":true}}}
0