1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
|
<!DOCTYPE html>
<html lang="en" dir="ltr" class="client-nojs">
<head>
<title>Template argument deduction - cppreference.com</title>
<meta charset="UTF-8" />
<meta name="generator" content="MediaWiki 1.21.2" />
<link rel="alternate" type="application/x-wiki" title="Edit" href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit" />
<link rel="edit" title="Edit" href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit" />
<link rel="shortcut icon" href="../../../favicon.ico" />
<link rel="search" type="application/opensearchdescription+xml" href="../../../mwiki/opensearch_desc.php" title="cppreference.com (en)" />
<link rel="EditURI" type="application/rsd+xml" href="http://en.cppreference.com/mwiki/api.php?action=rsd" />
<link rel="alternate" type="application/atom+xml" title="cppreference.com Atom feed" href="http://en.cppreference.com/mwiki/index.php?title=Special:RecentChanges&feed=atom" />
<link rel="stylesheet" href="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=ext.gadget.ColiruCompiler%257Cext.rtlcite%257Cmediawiki.legacy.commonPrint%252Cshared%257Cskins.cppreference2&only=styles&skin=cppreference2&*.css" />
<meta name="ResourceLoaderDynamicStyles" content="" />
<link rel="stylesheet" href="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=site&only=styles&skin=cppreference2&*.css" />
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-css:7:472787eddcf4605d11de8c7ef047234f */</style>
<script src="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=startup&only=scripts&skin=cppreference2&*"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/template_argument_deduction","wgTitle":"cpp/language/template argument deduction","wgCurRevisionId":81905,"wgArticleId":13515,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Todo with reason"],"wgBreakFrames":false,"wgPageContentLanguage":"en","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgMonthNamesShort":["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"wgRelevantPageName":"cpp/language/template_argument_deduction","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"en","language":"en","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-js:7:ca03345b1e2c4d90a25d968753a73b92 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
line-height: normal; border: 0px none white;
}
/**
* GeSHi Dynamically Generated Stylesheet
* --------------------------------------
* Dynamically generated stylesheet for cpp
* CSS class: source-cpp, CSS id:
* GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
* (http://qbnz.com/highlighter/ and http://geshi.org/)
* --------------------------------------
*/
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }
/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_template_argument_deduction skin-cppreference2 action-view cpp-navbar">
<!-- header -->
<div id="mw-head" class="noprint">
<div id="cpp-head-first-base">
<div id="cpp-head-first">
<h5><a href="../../../index.html">
cppreference.com </a></h5>
<div id="cpp-head-search">
<!-- 0 -->
<div id="p-search">
<h5><label for="searchInput">Search</label></h5>
<form action="http://en.cppreference.com/mwiki/index.php" id="searchform">
<input type='hidden' name="title" value="Special:Search"/>
<div id="simpleSearch">
<input name="search" title="Search cppreference.com [f]" accesskey="f" id="searchInput" /> <button type="submit" name="button" title="Search the pages for this text" id="searchButton"><img src="../../../mwiki/skins/cppreference2/images/search-ltr.png%3F303" alt="Search" /></button> </div>
</form>
</div>
<!-- /0 -->
</div>
<div id="cpp-head-personal">
<!-- 0 -->
<div id="p-personal" class="">
<span id="pt-createaccount"><a href="http://en.cppreference.com/mwiki/index.php?title=Special:UserLogin&returnto=cpp%2Flanguage%2Ftemplate+argument+deduction&type=signup">Create account</a></span> <div class="menu">
<ul>
<li id="pt-login"><a href="http://en.cppreference.com/mwiki/index.php?title=Special:UserLogin&returnto=cpp%2Flanguage%2Ftemplate+argument+deduction" title="You are encouraged to log in; however, it is not mandatory [o]" accesskey="o">Log in</a></li> </ul>
</div>
</div>
<!-- /0 -->
</div>
</div>
</div>
<div id="cpp-head-second-base">
<div id="cpp-head-second">
<div id="cpp-head-tools-left">
<!-- 0 -->
<div id="p-namespaces" class="vectorTabs">
<h5>Namespaces</h5>
<ul>
<li id="ca-nstab-main" class="selected"><span><a href="template_argument_deduction.html" title="View the content page [c]" accesskey="c">Page</a></span></li>
<li id="ca-talk" class="new"><span><a href="http://en.cppreference.com/mwiki/index.php?title=Talk:cpp/language/template_argument_deduction&action=edit&redlink=1" title="Discussion about the content page [t]" accesskey="t">Discussion</a></span></li>
</ul>
</div>
<!-- /0 -->
<!-- 1 -->
<div id="p-variants" class="vectorMenu emptyPortlet">
<h5><span>Variants</span><a href="template_argument_deduction.html#"></a></h5>
<div class="menu">
<ul>
</ul>
</div>
</div>
<!-- /1 -->
</div>
<div id="cpp-head-tools-right">
<!-- 0 -->
<div id="p-views" class="vectorTabs">
<h5>Views</h5>
<ul>
<li id="ca-view" class="selected"><span><a href="template_argument_deduction.html" >View</a></span></li>
<li id="ca-edit"><span><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit" title="You can edit this page. Please use the preview button before saving [e]" accesskey="e">Edit</a></span></li>
<li id="ca-history" class="collapsible"><span><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=history" title="Past revisions of this page [h]" accesskey="h">History</a></span></li>
</ul>
</div>
<!-- /0 -->
<!-- 1 -->
<div id="p-cactions" class="vectorMenu emptyPortlet">
<h5><span>Actions</span><a href="template_argument_deduction.html#"></a></h5>
<div class="menu">
<ul>
</ul>
</div>
</div>
<!-- /1 -->
</div>
</div>
</div>
</div>
<!-- /header -->
<!-- content -->
<div id="cpp-content-base">
<div id="content">
<a id="top"></a>
<div id="mw-js-message" style="display:none;"></div>
<!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">Template argument deduction</h1>
<!-- /firstHeading -->
<!-- bodyContent -->
<div id="bodyContent">
<!-- tagline -->
<div id="siteSub">From cppreference.com</div>
<!-- /tagline -->
<!-- subtitle -->
<div id="contentSub"><span class="subpages">< <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.1.html" title="cpp/language">language</a></span></div>
<!-- /subtitle -->
<!-- bodycontent -->
<div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.1.html" title="cpp/language"> Language</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header"> Standard library headers</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concept.html" title="cpp/concept"> Concepts</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility"> Utilities library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string"> Strings library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container"> Containers library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm"> Algorithms library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator"> Iterators library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric"> Numerics library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io"> Input/output library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale"> Localizations library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex"> Regular expressions library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic"> Atomic operations library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread"> Thread support library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental"> Technical Specifications</a> </td></tr>
</table></div><div><span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/navbar_content&action=edit">[edit]</a></span></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.1.html" title="cpp/language"> C++ language</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="templates.html" title="cpp/language/templates"> Templates</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="template_parameters.html" title="cpp/language/template parameters"> parameters and arguments</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> class templates</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> function templates</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="member_template.html" title="cpp/language/member template"> class member templates</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="variable_template.html" title="cpp/language/variable template"> variable templates</a><span class="t-mark-rev t-since-cxx14">(C++14)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> template argument deduction</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> explicit (full) specialization</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="partial_specialization.html" title="cpp/language/partial specialization"> partial specialization</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="dependent_name.html" title="cpp/language/dependent name"> dependent names</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="parameter_pack.html" title="cpp/language/parameter pack"> parameter packs</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="sizeof....html" title="cpp/language/sizeof..."><span class="t-lines"><span>sizeof...</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="fold.html" title="cpp/language/fold"> fold-expressions</a><span class="t-mark-rev t-since-cxx17">(C++17)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sfinae.html" title="cpp/language/sfinae"> SFINAE</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="concepts.html" title="cpp/language/constraints"> Constraints and concepts</a><span class="t-mark-rev t-since-concepts-ts t-mark-ts">(concepts TS)</span> </td></tr>
</table></div>
</div><div><span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/language/templates/navbar_content&action=edit">[edit]</a></span></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>In order to instantiate a <a href="function_template.html" title="cpp/language/function template">function template</a>, every template argument must be known, but not every template argument has to be specified. When possible, the compiler will deduce the missing template arguments from the function arguments. This occurs when a function call is attempted, when an address of a function template is taken, and in some <a href="template_argument_deduction.html#Other_contexts">other contexts</a>:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span> To, <span class="kw1">typename</span> From<span class="sy1">></span> To convert<span class="br0">(</span>From f<span class="br0">)</span><span class="sy4">;</span>
 
<span class="kw4">void</span> g<span class="br0">(</span><span class="kw4">double</span> d<span class="br0">)</span>
<span class="br0">{</span>
<span class="kw4">int</span> i <span class="sy1">=</span> convert<span class="sy1"><</span><span class="kw4">int</span><span class="sy1">></span><span class="br0">(</span>d<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// calls convert<int,double>(double)</span>
<span class="kw4">char</span> c <span class="sy1">=</span> convert<span class="sy1"><</span><span class="kw4">char</span><span class="sy1">></span><span class="br0">(</span>d<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// calls convert<char,double>(double)</span>
<span class="kw4">int</span><span class="br0">(</span><span class="sy2">*</span>ptr<span class="br0">)</span><span class="br0">(</span><span class="kw4">float</span><span class="br0">)</span> <span class="sy1">=</span> convert<span class="sy4">;</span> <span class="co1">// instantiates convert<int, float>(float)</span>
<span class="br0">}</span></pre></div></div>
<p>This mechanism makes it possible to use template operators, since there is no syntax to specify template arguments for an operator other than by re-writing it as a function call expression.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include <iostream></span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> <span class="st0">"Hello, world"</span> <span class="sy1"><<</span> <a href="../io/manip/endl.html"><span class="kw1514">std::<span class="me2">endl</span></span></a><span class="sy4">;</span>
<span class="co1">// operator<< is looked up via ADL as std::operator<<,</span>
<span class="co1">// then deduced to operator<<<char, std::char_traits<char>> both times</span>
<span class="co1">// std::endl is deduced to &std::endl<char, std::char_traits<char>></span>
<span class="br0">}</span></pre></div></div>
<p>Template argument deduction takes place after the function template <a href="lookup.html" title="cpp/language/lookup">name lookup</a> (which may involve <a href="adl.html" title="cpp/language/adl">argument-dependent lookup</a>) and before <a href="function_template.html#Template_argument_substitution" title="cpp/language/function template">template argument substitution</a> (which may involve <a href="sfinae.html" title="cpp/language/sfinae">SFINAE</a>) and <a href="overload_resolution.html" title="cpp/language/overload resolution">overload resolution</a>.
</p>
<table id="toc" class="toc"><tr><td><div id="toctitle"><h2>Contents</h2></div>
<ul>
<li class="toclevel-1 tocsection-1"><a href="template_argument_deduction.html#Deduction_from_a_function_call"><span class="tocnumber">1</span> <span class="toctext">Deduction from a function call</span></a>
<ul>
<li class="toclevel-2 tocsection-2"><a href="template_argument_deduction.html#Non-deduced_contexts"><span class="tocnumber">1.1</span> <span class="toctext">Non-deduced contexts</span></a></li>
<li class="toclevel-2 tocsection-3"><a href="template_argument_deduction.html#Deduction_from_a_type"><span class="tocnumber">1.2</span> <span class="toctext">Deduction from a type</span></a></li>
</ul>
</li>
<li class="toclevel-1 tocsection-4"><a href="template_argument_deduction.html#Other_contexts"><span class="tocnumber">2</span> <span class="toctext">Other contexts</span></a>
<ul>
<li class="toclevel-2 tocsection-5"><a href="template_argument_deduction.html#auto_type_deduction"><span class="tocnumber">2.1</span> <span class="toctext">auto type deduction</span></a></li>
<li class="toclevel-2"><a href="template_argument_deduction.html#auto-returning_functions"><span class="tocnumber">2.2</span> <span class="toctext">auto-returning functions</span></a></li>
<li class="toclevel-2 tocsection-7"><a href="template_argument_deduction.html#overload_resolution"><span class="tocnumber">2.3</span> <span class="toctext">overload resolution</span></a></li>
<li class="toclevel-2 tocsection-8"><a href="template_argument_deduction.html#address_of_an_overload_set"><span class="tocnumber">2.4</span> <span class="toctext">address of an overload set</span></a></li>
<li class="toclevel-2 tocsection-9"><a href="template_argument_deduction.html#partial_ordering"><span class="tocnumber">2.5</span> <span class="toctext">partial ordering</span></a></li>
<li class="toclevel-2 tocsection-10"><a href="template_argument_deduction.html#conversion_function_template"><span class="tocnumber">2.6</span> <span class="toctext">conversion function template</span></a></li>
<li class="toclevel-2 tocsection-11"><a href="template_argument_deduction.html#explicit_instantiation"><span class="tocnumber">2.7</span> <span class="toctext">explicit instantiation</span></a></li>
<li class="toclevel-2 tocsection-12"><a href="template_argument_deduction.html#deallocation_function_template"><span class="tocnumber">2.8</span> <span class="toctext">deallocation function template</span></a></li>
</ul>
</li>
<li class="toclevel-1 tocsection-13"><a href="template_argument_deduction.html#Alias_templates"><span class="tocnumber">3</span> <span class="toctext">Alias templates</span></a></li>
<li class="toclevel-1 tocsection-14"><a href="template_argument_deduction.html#Implicit_conversions"><span class="tocnumber">4</span> <span class="toctext">Implicit conversions</span></a></li>
<li class="toclevel-1 tocsection-15"><a href="template_argument_deduction.html#Defect_reports"><span class="tocnumber">5</span> <span class="toctext">Defect reports</span></a></li>
</ul>
</td></tr></table>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=1" title="Edit section: Deduction from a function call">edit</a>]</span> <span class="mw-headline" id="Deduction_from_a_function_call">Deduction from a function call</span></h3>
<p>Template argument deduction attempts to determine template arguments (types for type template parameters <code><b>T</b></code>i, templates for template template parameters <code><b>TT</b></code>i, and values for non-type template parameters <code><b>I</b></code>i), which can be substituted into each parameter <code><b>P</b></code> to produce the type <i>deduced A</i>, which is the same as the type of the argument <code><b>A</b></code>, after adjustments listed below.
</p><p>If there are multiple parameters, each P/A pair is deduced separately and the deduced template arguments are then combined. If deduction fails or is ambiguous for any P/A pair or if different pairs yield different deduced template arguments, or if any template argument remains neither deduced nor explicitly specified, compilation fails.
</p><p>If removing references and cv-qualifiers from P gives <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../utility/initializer_list.html"><span class="kw838">std::<span class="me2">initializer_list</span></span></a><span class="sy1"><</span>P<span class="st0">'></span></span></span> and A is a <a href="list_initialization.html" title="cpp/language/list initialization">braced-init-list</a>, then deduction is performed for every element of the initializer list, taking P' as the parameter and the list element as the argument.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span><a href="../utility/initializer_list.html"><span class="kw838">std::<span class="me2">initializer_list</span></span></a><span class="sy1"><</span>T<span class="sy1">></span><span class="br0">)</span><span class="sy4">;</span>
f<span class="br0">(</span><span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=std::initializer_list<T>, A={1,2,3}</span>
<span class="co1">// P'1=T, A'1=1: deduces T=int</span>
<span class="co1">// P'2=T, A'2=2: deduces T=int</span>
<span class="co1">// P'3=T, A'3=3: deduces T=int</span>
<span class="co1">// deduction succeeds, T = int</span>
f<span class="br0">(</span><span class="br0">{</span><span class="nu0">1</span>,<span class="st0">"asdf"</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=std::initializer_list<T>, A={1,"asdf"}</span>
<span class="co1">// P'1=T, A'1=1: deduces T=int</span>
<span class="co1">// P'2=T, A'2="asdf", deduces T=const char*</span>
<span class="co1">// deduction fails, T ambiguous</span></pre></div></div>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<p>If removing references and cv-qualifiers from P gives <code>P'[N]</code>, and A is a non-empty braced-init-list, then deduction is performed as above, except if <code>N</code> is a non-type template parameter, it is deduced from the length of the initializer list:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> T, <span class="kw4">int</span> N<span class="sy1">></span> <span class="kw4">void</span> h<span class="br0">(</span>T <span class="kw4">const</span><span class="br0">(</span><span class="sy3">&</span><span class="br0">)</span><span class="br0">[</span>N<span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>
h<span class="br0">(</span><span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// T deduced to int, N deduced to 3</span>
 
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">void</span> j<span class="br0">(</span>T <span class="kw4">const</span><span class="br0">(</span><span class="sy3">&</span><span class="br0">)</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>
j<span class="br0">(</span><span class="br0">{</span><span class="nu0">42</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// T deduced to int, array bound is not a parameter, not considered</span>
 
<span class="kw1">struct</span> Aggr <span class="br0">{</span> <span class="kw4">int</span> i<span class="sy4">;</span> <span class="kw4">int</span> j<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">template</span><span class="sy1"><</span><span class="kw4">int</span> N<span class="sy1">></span> <span class="kw4">void</span> k<span class="br0">(</span>Aggr <span class="kw4">const</span><span class="br0">(</span><span class="sy3">&</span><span class="br0">)</span><span class="br0">[</span>N<span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>
k<span class="br0">(</span><span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// error: deduction fails, no conversion from int to Aggr</span>
k<span class="br0">(</span><span class="br0">{</span><span class="br0">{</span><span class="nu0">1</span><span class="br0">}</span>,<span class="br0">{</span><span class="nu0">2</span><span class="br0">}</span>,<span class="br0">{</span><span class="nu0">3</span><span class="br0">}</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK, N deduced to 3</span>
 
<span class="kw1">template</span><span class="sy1"><</span><span class="kw4">int</span> M, <span class="kw4">int</span> N<span class="sy1">></span> <span class="kw4">void</span> m<span class="br0">(</span><span class="kw4">int</span> <span class="kw4">const</span><span class="br0">(</span><span class="sy3">&</span><span class="br0">)</span><span class="br0">[</span>M<span class="br0">]</span><span class="br0">[</span>N<span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>
m<span class="br0">(</span><span class="br0">{</span><span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span><span class="br0">}</span>,<span class="br0">{</span><span class="nu0">3</span>,<span class="nu0">4</span><span class="br0">}</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// M and N both deduced to 2</span>
 
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> T, <span class="kw4">int</span> N<span class="sy1">></span> <span class="kw4">void</span> n<span class="br0">(</span>T <span class="kw4">const</span><span class="br0">(</span><span class="sy3">&</span><span class="br0">)</span><span class="br0">[</span>N<span class="br0">]</span>, T<span class="br0">)</span><span class="sy4">;</span>
n<span class="br0">(</span><span class="br0">{</span><span class="br0">{</span><span class="nu0">1</span><span class="br0">}</span>,<span class="br0">{</span><span class="nu0">2</span><span class="br0">}</span>,<span class="br0">{</span><span class="nu0">3</span><span class="br0">}</span><span class="br0">}</span>,Aggr<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK, T is Aggr, N is 3</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx17">(since C++17)</span></td></tr>
</table>
<p>If a <a href="parameter_pack.html" title="cpp/language/parameter pack">parameter pack</a> appears as the last P, then the type P is matched against the type A of each remaining argument of the call. Each match deduces the template arguments for the next position in the pack expansion.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> ... <span class="me1">Types</span><span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>Types<span class="sy3">&</span> ...<span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="kw4">int</span> x, <span class="kw4">float</span><span class="sy3">&</span> y<span class="br0">)</span> <span class="br0">{</span>
<span class="kw4">const</span> <span class="kw4">int</span> z <span class="sy1">=</span> x<span class="sy4">;</span>
f<span class="br0">(</span>x, y, z<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=Types&..., A1=x: deduces the first member of Types... to int</span>
<span class="co1">// P=Types&..., A2=y: deduces the second member of Types... to float</span>
<span class="co1">// P=Types&..., A3=z: deduces the third member of Types... to const int</span>
<span class="co1">// calls f<int, float, const int></span></pre></div></div>
<p>If <code><b>P</b></code> is a function type, pointer to function type, or pointer to member function type and if <code><b>A</b></code> is a <a href="overloaded_address.html" title="cpp/language/overloaded address">set of overloaded functions</a> not containing function templates, template argument deduction is attempted with each overload. If only one succeeds, that successful deduction is used. If none or more than one succeeds, the template parameter is non-deduced context (see below).
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">int</span> f<span class="br0">(</span>T <span class="br0">(</span><span class="sy2">*</span>p<span class="br0">)</span><span class="br0">(</span>T<span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">char</span><span class="br0">)</span><span class="sy4">;</span>
 
f<span class="br0">(</span>g<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=T(*)(T), A=overload set</span>
<span class="co1">// P=T(*)(T), A1=int(int): deduces T=int</span>
<span class="co1">// P=T(*)(T), A2=int(char): fails to deduce T</span>
<span class="co1">// only one overload works, deduction succeeds</span></pre></div></div>
<p><br />
Before deduction begins, the following adjustments to <code><b>P</b></code> and <code><b>A</b></code> are made:
</p>
<div class="t-li1"><span class="t-li">1)</span> If <code><b>P</b></code> is not a reference type, </div>
<div class="t-li2"><span class="t-li">a)</span> If <code><b>A</b></code> is an array type, <code><b>A</b></code> is replaced by the pointer type obtained from array-to-pointer conversion</div>
<div class="t-li2"><span class="t-li">b)</span> Otherwise, if <code><b>A</b></code> is a function type, <code><b>A</b></code> is replaced by the pointer type obtained from function-to-pointer conversion</div>
<div class="t-li2"><span class="t-li">c)</span> Otherwise, if <code><b>A</b></code> is a cv-qualified type, the top-level cv-qualifiers are ignored for deduction
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>T<span class="br0">)</span><span class="sy4">;</span>
 
<span class="kw4">int</span> a<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="sy4">;</span>
f<span class="br0">(</span>a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P = T, A = int[3], adjusted to int*. Deduces T = int*</span>
<span class="kw4">const</span> <span class="kw4">int</span> b <span class="sy1">=</span> <span class="nu0">13</span><span class="sy4">;</span>
f<span class="br0">(</span>b<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P = T, A = const int, adjusted to int. Deduces T = int</span>
<span class="kw4">void</span> g<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
f<span class="br0">(</span>g<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P = T, A = void(int), adjusted to void(*)(int). Deduces T = void(*)(int)</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">2)</span> If <code>P</code> is a cv-qualified type, the top-level cv qualifiers are ignored for deduction.</div>
<div class="t-li1"><span class="t-li">3)</span> If <code>P</code> is a reference type, the type referred to by <code>P</code> is used for deduction.</div>
<div class="t-li1"><span class="t-li">4)</span> If <code>P</code> is an rvalue reference to a cv-unqualified template parameter (so-called "forwarding reference"), and the corresponding function call argument is an lvalue, the type lvalue reference to <code>A</code> is used in place of <code>A</code> for deduction (Note: this is the basis for the action of <span class="t-lc"><a href="../utility/forward.html" title="cpp/utility/forward">std::forward</a></span>)
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span>
<span class="kw4">int</span> f<span class="br0">(</span>T<span class="sy3">&&</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P is rvalue reference to cv-unqualified T ("forwarding reference")</span>
 
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span>
<span class="kw4">int</span> g<span class="br0">(</span><span class="kw4">const</span> T<span class="sy3">&&</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P is rvalue reference to cv-qualified T (not special)</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="kw4">int</span> i<span class="sy4">;</span>
<span class="kw4">int</span> n1 <span class="sy1">=</span> f<span class="br0">(</span>i<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// argument is lvalue: calls f<int&>(int&) (special case)</span>
<span class="kw4">int</span> n2 <span class="sy1">=</span> f<span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// argument is not lvalue: calls f<int>(int&&)</span>
 
<span class="co1">// int n3 = g(i); // error: deduces to g<int>(const int&&), which</span>
<span class="co1">// cant bind an rvalue reference to an lvalue:</span>
<span class="br0">}</span></pre></div></div></div>
<p>After these transformations, the deduction process as described below (in "Deduction from type"), and attempts to find such template arguments that would make <i>deduced A</i> (that is, <code>P</code> after adjustments listed above and the substitution of the deduced template parameters) identical to the <i>transformed A</i>, that is <code><b>A</b></code> after the adjustments listed above.
</p><p>If the usual deduction from <code><b>P</b></code> and <code><b>A</b></code> fails, the following alternatives are additionally considered:
</p>
<div class="t-li1"><span class="t-li">1)</span> If the original <code><b>P</b></code> is a <i>reference</i> type, the <i>deduced A</i> (i.e., the type referred to by the reference) can be more cv-qualified than the <i>transformed A</i>.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span> T<span class="sy1">></span> <span class="kw4">void</span> f1<span class="br0">(</span><span class="kw4">const</span> T<span class="sy3">&</span> t<span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">bool</span> a <span class="sy1">=</span> <span class="kw2">false</span><span class="sy4">;</span>
f1<span class="br0">(</span>a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=const T&, adjusted to const T, A=bool, </span>
<span class="co1">// deduced T = bool, deduced A = const bool</span>
<span class="co1">// deduced A is more cv-qualified than A</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">2)</span> The <i>transformed A</i> can be another pointer or pointer to member type that can be converted to the <i>deduced A</i> via a <a href="implicit_cast.html#Qualification_conversions" title="cpp/language/implicit cast">qualification conversions</a> <span class="t-rev-inl t-since-cxx17"><span>or a function pointer conversion</span> <span><span class="t-mark-rev t-since-cxx17">(since C++17)</span></span></span>.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span> T<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">const</span> T<span class="sy2">*</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">int</span><span class="sy2">*</span> p<span class="sy4">;</span>
f<span class="br0">(</span>p<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=T, A=int*</span>
<span class="co1">// deduces T=int, deduced A = const int*</span>
<span class="co1">// qualification conversion applies (from int* to const int*)</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">3)</span> If <code>P</code> is a class and <code>P</code> has the form <i>simple-template-id</i>, then the <i>transformed A</i> can be a derived class of the <i>deduced A</i>. Likewise, if <code>P</code> is a pointer to a class of the form <i>simple-template-id</i>, the <i>transformed A</i> can be a pointer to a derived class pointed to by the <i>deduced A</i>.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw1">struct</span> B <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw1">struct</span> D <span class="sy4">:</span> <span class="kw1">public</span> B<span class="sy1"><</span>T<span class="sy1">></span> <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>B<span class="sy1"><</span>T<span class="sy1">></span><span class="sy3">&</span><span class="br0">)</span><span class="br0">{</span><span class="br0">}</span>
 
<span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
D<span class="sy1"><</span><span class="kw4">int</span><span class="sy1">></span> d<span class="sy4">;</span>
f<span class="br0">(</span>d<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P = B<T>&, adjusted P = B<T> (a simple-template-id)</span>
<span class="co1">// A = D<int></span>
<span class="co1">// deduced T = int, deduced A = B<int></span>
<span class="co1">// A is derived from deduced A</span>
<span class="br0">}</span></pre></div></div></div>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=2" title="Edit section: Non-deduced contexts">edit</a>]</span> <span class="mw-headline" id="Non-deduced_contexts">Non-deduced contexts</span></h4>
<p>In the following cases, the types, templates, and non-type values that are used to compose <code><b>P</b></code> do not participate in template argument deduction, but instead <i>use</i> the template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails
</p>
<div class="t-li1"><span class="t-li">1)</span> The <span class="t-spar">nested-name-specifier</span> (everything to the left of the scope resolution operator <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy4">::</span></span></span>) of a type that was specified using a <a href="name.html#Qualified_identifiers" title="cpp/language/identifiers">qualified-id</a>. <div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// the identity template, often used to exclude specific arguments from deduction</span>
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">typename</span> T<span class="sy1">></span> <span class="kw1">struct</span> identity <span class="br0">{</span> <span class="kw1">typedef</span> T type<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">typename</span> T<span class="sy1">></span>
<span class="kw4">void</span> bad<span class="br0">(</span><a href="../container/vector.html"><span class="kw1094">std::<span class="me2">vector</span></span></a><span class="sy1"><</span>T<span class="sy1">></span> x, T value <span class="sy1">=</span> <span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>
 
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">typename</span> T<span class="sy1">></span>
<span class="kw4">void</span> good<span class="br0">(</span><a href="../container/vector.html"><span class="kw1094">std::<span class="me2">vector</span></span></a><span class="sy1"><</span>T<span class="sy1">></span> x, <span class="kw1">typename</span> identity<span class="sy1"><</span>T<span class="sy1">></span><span class="sy4">::</span><span class="me2">type</span> value <span class="sy1">=</span> <span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>
 
<a href="../container/vector.html"><span class="kw1094">std::<span class="me2">vector</span></span></a><span class="sy1"><</span><a href="../numeric/complex.html"><span class="kw1220">std::<span class="me2">complex</span></span></a><span class="sy1"><</span><span class="kw4">double</span><span class="sy1">>></span> x<span class="sy4">;</span>
bad<span class="br0">(</span>x, <span class="nu16">1.2</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P1 = std::vector<T>, A1 = std::vector<std::complex<double>></span>
<span class="co1">// P1/A1 deduction determines T = std::complex<double></span>
<span class="co1">// P2 = T, A2 = double</span>
<span class="co1">// P2/A2 deduction determines T = double -- Error</span>
good<span class="br0">(</span>x, <span class="nu16">1.2</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P1/A1 deduces T = std::complex<double></span>
<span class="co1">// P2 = identity<T>::type, T is to the left of ::, non-deduced</span></pre></div></div></div>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<div class="t-li1"><span class="t-li">2)</span> The expression of a <a href="decltype.html" title="cpp/language/decltype">decltype</a>-specifier.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span> T<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>decltype<span class="br0">(</span><span class="sy2">*</span><a href="../utility/declval.html"><span class="kw950">std::<span class="me2">declval</span></span></a><span class="sy1"><</span>T<span class="sy1">></span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> arg<span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">int</span> n<span class="sy4">;</span> f<span class="sy1"><</span><span class="kw4">int</span><span class="sy2">*</span><span class="sy1">></span><span class="br0">(</span>n<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P = decltype(*declval<T>()), A=int. T is not deducible</span></pre></div></div>
</div></td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr></table>
<div class="t-li1"><span class="t-li">3)</span> A non-type template argument or an array bound in which a subexpression references a template
<p>parameter
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> N<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span><a href="../container/array.html"><span class="kw1092">std::<span class="me2">array</span></span></a><span class="sy1"><</span><span class="kw4">int</span>, <span class="nu0">2</span><span class="sy2">*</span>N<span class="sy1">></span> a<span class="br0">)</span><span class="sy4">;</span>
<a href="../container/array.html"><span class="kw1092">std::<span class="me2">array</span></span></a><span class="sy1"><</span><span class="kw4">int</span>, <span class="nu0">10</span><span class="sy1">></span> a<span class="sy4">;</span>
f<span class="br0">(</span>a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=std::array<int, 2*N>, "2*N" is non-deduced context, N cannot be deduced</span>
<span class="co1">// (note: "f(std::array<int, N> a)" would be able to deduce N)</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">4)</span> A template parameter used in the parameter type of a function parameter that has a default argument that is being used in the call for which argument deduction is being done
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span> T, <span class="kw1">typename</span> F<span class="sy1">></span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">const</span> <a href="../container/vector.html"><span class="kw1094">std::<span class="me2">vector</span></span></a><span class="sy1"><</span>T<span class="sy1">></span> <span class="sy3">&</span>arr, <span class="kw4">const</span> F<span class="sy3">&</span> comp <span class="sy1">=</span> <a href="../utility/functional/less.html"><span class="kw913">std::<span class="me2">less</span></span></a><span class="sy1"><</span>T<span class="sy1">></span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
<a href="../container/vector.html"><span class="kw1094">std::<span class="me2">vector</span></span></a><span class="sy1"><</span><a href="../string/basic_string.html"><span class="kw1072">std::<span class="me2">string</span></span></a><span class="sy1">></span> arr<span class="br0">(</span><span class="nu0">3</span><span class="br0">)</span><span class="sy4">;</span>
f<span class="br0">(</span>arr<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P1 = const std::vector<T> &, A1=std::vector<std::string> lvalue,</span>
<span class="co1">// P1/A1 deduces T = std::string</span>
<span class="co1">// P2 = non-deduced context for F (template parameter) used in the</span>
<span class="co1">// parameter type const F& of the function parameter comp,</span>
<span class="co1">// that has a default argument that is being used in the call f(arr)</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">5)</span> The parameter <code><b>P</b></code>, whose <code><b>A</b></code> is a function or a set of overloads such that more than one function matches <code><b>P</b></code> or no function matches <code><b>P</b></code> or the set of overloads includes one or more function templates
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span> T<span class="sy1">></span> <span class="kw4">void</span> out<span class="br0">(</span><span class="kw4">const</span> T<span class="sy3">&</span> value<span class="br0">)</span> <span class="br0">{</span> <a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> value<span class="sy4">;</span> <span class="br0">}</span>
 
out<span class="br0">(</span><span class="st0">"12345"</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=const T&, A=const char[6] lvalue, T is deduced to be "char[6]"</span>
out<span class="br0">(</span><a href="../io/manip/endl.html"><span class="kw1514">std::<span class="me2">endl</span></span></a><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=const T&, A=function template, T is non-deducible, error.</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">6)</span> The parameter <code><b>P</b></code>, whose <code><b>A</b></code> is a braced-init-list, but <code><b>P</b></code> is not std::initializer_list or a reference to one.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">void</span> g1<span class="br0">(</span><a href="../container/vector.html"><span class="kw1094">std::<span class="me2">vector</span></span></a><span class="sy1"><</span>T<span class="sy1">></span><span class="br0">)</span><span class="sy4">;</span>
g1<span class="br0">(</span><span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=std::vector<T>, A={1,2,3}, this P is non-deduced context</span>
<span class="co1">// Error: T is not explicitly specified or deduced from another P/A</span>
 
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">void</span> g2<span class="br0">(</span><a href="../container/vector.html"><span class="kw1094">std::<span class="me2">vector</span></span></a><span class="sy1"><</span>T<span class="sy1">></span>, T x<span class="br0">)</span><span class="sy4">;</span>
g2<span class="br0">(</span><span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">}</span>, <span class="nu0">10</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P1=std::vector<T,> A1={1,2,3}, this P is non-deduced context</span>
<span class="co1">// P2=T, A2=int, this P/A pair deduces T = int</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">7)</span> The parameter <code><b>P</b></code> which is a parameter pack and does not occur at the end of the parameter list.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span>... <span class="me1">Ts</span>, <span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">void</span> f1<span class="br0">(</span>T n, Ts... <span class="me1">args</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span>... <span class="me1">Ts</span>, <span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">void</span> f2<span class="br0">(</span>Ts... <span class="me1">args</span>, T n<span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
 
f1<span class="br0">(</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span>,<span class="nu0">4</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P1=T, A1=1: T is deduced to be int</span>
<span class="co1">// P2=Ts..., A2=2, A3=3, A4=4: Ts is deduced to be [int, int, int]</span>
f2<span class="br0">(</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span>,<span class="nu0">4</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P1=Ts..., non-deducible</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">8)</span> The template parameter list that appears within the parameter <code><b>P</b></code>, and which includes a pack expansion that is not at the very end of the template parameter list.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw4">int</span>...<span class="sy1">></span> <span class="kw1">struct</span> T <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw1">template</span><span class="sy1"><</span><span class="kw4">int</span>... <span class="me1">Ts1</span>, <span class="kw4">int</span> N, <span class="kw4">int</span>... <span class="me1">Ts2</span><span class="sy1">></span>
<span class="kw4">void</span> good<span class="br0">(</span><span class="kw4">const</span> T<span class="sy1"><</span>N, Ts1...<span class="sy1">></span><span class="sy3">&</span> arg1, <span class="kw4">const</span> T<span class="sy1"><</span>N, Ts2...<span class="sy1">></span><span class="sy3">&</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
 
<span class="kw1">template</span><span class="sy1"><</span><span class="kw4">int</span>... <span class="me1">Ts1</span>, <span class="kw4">int</span> N, <span class="kw4">int</span>... <span class="me1">Ts2</span><span class="sy1">></span>
<span class="kw4">void</span> bad<span class="br0">(</span><span class="kw4">const</span> T<span class="sy1"><</span>Ts1..., N<span class="sy1">></span><span class="sy3">&</span> arg1, <span class="kw4">const</span> T<span class="sy1"><</span>Ts2..., N<span class="sy1">></span><span class="sy3">&</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
 
T<span class="sy1"><</span><span class="nu0">1</span>,<span class="nu0">2</span><span class="sy1">></span> t1<span class="sy4">;</span> T<span class="sy1"><</span><span class="nu0">1</span>,<span class="sy2">-</span><span class="nu0">1</span>,<span class="nu0">0</span><span class="sy1">></span> t2<span class="sy4">;</span>
 
good<span class="br0">(</span>t1, t2<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P1=const T<N, Ts1...>&, A1=T<1,2>: deduces N=1, Ts1=[2]</span>
<span class="co1">// P2=const T<N, Ts2...>&, A2=T<1,-1,0>: deduces N=1, Ts2=[-1, 0]</span>
 
bad<span class="br0">(</span>t1, t2<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P1=const T<Ts1..., N>&, A1=T<1,2>: <Ts1..., N> is non-deduced</span>
<span class="co1">// P2=const T<Ts2..., N>&, A2=T<1,-1,0>: <Ts2..., N> is non-deduced</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">9)</span> For <code><b>P</b></code> of array type (but not reference to array or pointer to array), the major array bound.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw4">int</span> i<span class="sy1">></span> <span class="kw4">void</span> f1<span class="br0">(</span><span class="kw4">int</span> a<span class="br0">[</span><span class="nu0">10</span><span class="br0">]</span><span class="br0">[</span>i<span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw1">template</span><span class="sy1"><</span><span class="kw4">int</span> i<span class="sy1">></span> <span class="kw4">void</span> f2<span class="br0">(</span><span class="kw4">int</span> a<span class="br0">[</span>i<span class="br0">]</span><span class="br0">[</span><span class="nu0">20</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P = int[i][20], array type</span>
<span class="kw1">template</span><span class="sy1"><</span><span class="kw4">int</span> i<span class="sy1">></span> <span class="kw4">void</span> f3<span class="br0">(</span><span class="kw4">int</span> <span class="br0">(</span><span class="sy3">&</span>a<span class="br0">)</span><span class="br0">[</span>i<span class="br0">]</span><span class="br0">[</span><span class="nu0">20</span><span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P =int(&)[i][20], reference to array</span>
<span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
<span class="kw4">int</span> v<span class="br0">[</span><span class="nu0">10</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">20</span><span class="br0">]</span><span class="sy4">;</span>
f1<span class="br0">(</span>v<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK: i deduced to be 20</span>
f1<span class="sy1"><</span><span class="nu0">20</span><span class="sy1">></span><span class="br0">(</span>v<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK</span>
f2<span class="br0">(</span>v<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// error: cannot deduce template-argument i</span>
f2<span class="sy1"><</span><span class="nu0">10</span><span class="sy1">></span><span class="br0">(</span>v<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK</span>
f3<span class="br0">(</span>v<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK: i deduced to be 10</span>
<span class="br0">}</span></pre></div></div></div>
<p>In any case, if any part of a type name is non-deduced context, the entire type name is non-deduced. However, compound types can include both deduced and non-deduced type names. For example, in <span class="t-c"><span class="mw-geshi cpp source-cpp">A<span class="sy1"><</span>T<span class="sy1">></span><span class="sy4">::</span><span class="me2">B</span><span class="sy1"><</span>T2<span class="sy1">></span></span></span>, <code>T</code> is non-deduced because of rule #1 (nested name specifier), and <code>T2</code> is non-deduced because it is part of the same type name, but in <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">void</span><span class="br0">(</span><span class="sy2">*</span>f<span class="br0">)</span><span class="br0">(</span><span class="kw1">typename</span> A<span class="sy1"><</span>T<span class="sy1">></span><span class="sy4">::</span><span class="me2">B</span>, A<span class="sy1"><</span>T<span class="sy1">></span><span class="br0">)</span></span></span>, the <code>T</code> in <span class="t-c"><span class="mw-geshi cpp source-cpp">A<span class="sy1"><</span>T<span class="sy1">></span><span class="sy4">::</span><span class="me2">B</span></span></span> is non-deduced (because of the same rule), while the <code>T</code> in <span class="t-c"><span class="mw-geshi cpp source-cpp">A<span class="sy1"><</span>T<span class="sy1">></span></span></span> is deduced.
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=3" title="Edit section: Deduction from a type">edit</a>]</span> <span class="mw-headline" id="Deduction_from_a_type">Deduction from a type</span></h4>
<p>Given a function parameter <code><b>P</b></code> that depends on one or more type template parameters <code><b>T</b></code>i, template template parameters <code><b>TT</b></code>i, or non-type template parameters <code><b>I</b></code>i, and the corresponding argument <code><b>A</b></code>, deduction takes place if <code><b>P</b></code> has one of the following forms:
</p>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">This section is incomplete<br />Reason: possibly a table with micro-examples </td></tr></table>
<ul><li> <code>T</code>
</li><li> <code>cv-list T</code>
</li><li> <code>T*</code>
</li><li> <code>T&</code>
</li><li> <code>T&&</code>
</li><li> <code>T[integer-constant]</code>
</li><li> <code>class-template-name<T></code>
</li><li> <code>type(T)</code>
</li><li> <code>T()</code>
</li><li> <code>T(T)</code>
</li><li> <code>T type::*</code>
</li><li> <code>type T::*</code>
</li><li> <code>T T::*</code>
</li><li> <code>T(type::*)()</code>
</li><li> <code>type(T::*)()</code>
</li><li> <code>type(type::*)(T)</code>
</li><li> <code>type(T::*)(T)</code>
</li><li> <code>T (type::*)(T)</code>
</li><li> <code>T (T::*)()</code>
</li><li> <code>T (T::*)(T)</code>
</li><li> <code>type[i]</code>
</li><li> <code>class-template-name<I></code>
</li><li> <code>TT<T></code>
</li><li> <code>TT<I></code>
</li><li> <code>TT<></code>
</li></ul>
<p>where
</p>
<ul><li> <code>(T)</code> is a function parameter type list where at least one parameter type contains T
</li><li> <code>()</code> is a function parameter type list where no parameters contain T
</li><li> <code><T></code> is a template argument list where at least one argument contains T
</li><li> <code><I></code> is a template argument list where at least one argument contains I
</li><li> <code><></code> is a template argument list where no arguments contain T or I
</li></ul>
<p>If <code><b>P</b></code> has one of the forms that include a template parameter list <code><b><T></b></code> or <code><b><I></b></code>, then each element <code><b>P</b></code>i of that template argument list is matched against the corresponding template argument <code><b>A</b></code>i of its <code><b>A</b></code>. If the last <code><b>P</b></code>i is a pack expansion, then its pattern is compared against each remaining argument in the template argument list of <code><b>A</b></code>. A trailing parameter pack that is not otherwise deduced, is deduced to an empty parameter pack.
</p><p>If <code><b>P</b></code> has one of the forms that include a function parameter list <code><b>(T)</b></code>, then each parameter <code><b>P</b></code>i from that list is compared with the corresponding argument <code><b>A</b></code>i from <code><b>A</b></code>'s function parameter list. If the last <code><b>P</b></code>i is a pack expansion, then its declarator is compared with each remaining <code><b>A</b></code>i in the parameter type list of <code><b>A</b></code>.
</p><p>Forms can be nested and processed recursively: <code>X<int>(*)(char[6])</code> is an example of <code>type(*)(T)</code>, where <i>type</i> is <code>class-template-name<T></code> and T is <code>type[i]</code>.
</p><p>Template type argument cannot be deduced from the type of a non-type template argument.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span> T, T i<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">double</span> a<span class="br0">[</span><span class="nu0">10</span><span class="br0">]</span><span class="br0">[</span>i<span class="br0">]</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">double</span> v<span class="br0">[</span><span class="nu0">10</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">20</span><span class="br0">]</span><span class="sy4">;</span>
f<span class="br0">(</span>v<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=double[10][i] A=double[10][20]:</span>
<span class="co1">// i can be deduced to equal 20</span>
<span class="co1">// but T cannot be deduced from the type of i</span></pre></div></div>
<p>If a non-type template parameter is used in the parameter list, and the corresponding template argument is deduced, the type of the deduced template argument must match the type of the non-type template parameter exactly (except that cv-qualifiers are dropped, and except where the template argument is deduced from an array bound -- in that case any integral type is allowed, even bool though it would always become true)
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw4">int</span> i<span class="sy1">></span> <span class="kw1">class</span> A <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">template</span><span class="sy1"><</span><span class="kw4">short</span> s<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>A<span class="sy1"><</span>s<span class="sy1">></span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// the type of the non-type template param is short</span>
<span class="kw4">void</span> k1<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
A<span class="sy1"><</span><span class="nu0">1</span><span class="sy1">></span> a<span class="sy4">;</span> <span class="co1">// the type of the non-type template param of a is int</span>
f<span class="br0">(</span>a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=A<(short)s>, A=A<(int)1>, Error: deduced non-type template argument </span>
<span class="co1">// does not have the same type as its corresponding template argument</span>
f<span class="sy1"><</span><span class="nu0">1</span><span class="sy1">></span><span class="br0">(</span>a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK, the template argument is not deduced, </span>
<span class="co1">// this calls f<(short)1>(A<(short)1>)</span>
<span class="br0">}</span></pre></div></div>
<p>Template type parameter cannot be deduced from the type of a function default argument
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span> T<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>T <span class="sy1">=</span> <span class="nu0">5</span>, T <span class="sy1">=</span> <span class="nu0">7</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
f<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK: calls f<int>(1,7)</span>
f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// error: cannot deduce T</span>
f<span class="sy1"><</span><span class="kw4">int</span><span class="sy1">></span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK: calls f<int>(5,7)</span>
<span class="br0">}</span></pre></div></div>
<p>Deduction of template template parameter can use the type used in the template specialization used in the function call
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span><span class="sy1">></span> <span class="kw1">class</span> X<span class="sy1">></span> <span class="kw1">struct</span> A <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// A is a template with a TT param</span>
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span><span class="sy1">></span> <span class="kw1">class</span> TT<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>A<span class="sy1"><</span>TT<span class="sy1">></span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw1">struct</span> B <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>
A<span class="sy1"><</span>B<span class="sy1">></span> ab<span class="sy4">;</span>
f<span class="br0">(</span>ab<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// P=A<TT>, A=A<B>. This deduces TT=B and calls f(A<B>)</span></pre></div></div>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=4" title="Edit section: Other contexts">edit</a>]</span> <span class="mw-headline" id="Other_contexts">Other contexts</span></h3>
<p>Besides function call expressions and an operator expressions, template argument deduction is used in the following situations:
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=5" title="Edit section: auto type deduction">edit</a>]</span> <span class="mw-headline" id="auto_type_deduction">auto type deduction</span></h4>
<p>Template argument deduction is used in <a href="declarations.html" title="cpp/language/declarations">declarations</a> of variables, when deducing the meaning of the <b><a href="auto.html" title="cpp/language/auto">auto specifier</a></b> from the variable's initializer.
</p><p>The parameter <code><b>P</b></code> is obtained as follows: in <code>T</code>, the declared type of the variable that includes <code>auto</code>, every occurrence of <code>auto</code> is replaced with an imaginary type template parameter <code>U</code> or, if the initializer is a brace-init-list, with <code>std::initializer_list<U></code>. The argument <code><b>A</b></code> is the initializer expression. After deduction of <code>U</code> from <code><b>P</b></code> and <code><b>A</b></code> following the rules described above, the deduced <code>U</code> is substituted into <code>T</code> to get the actual variable type.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">const</span> <span class="kw4">auto</span><span class="sy3">&</span> x <span class="sy1">=</span> <span class="nu0">1</span><span class="sy2">+</span><span class="nu0">2</span><span class="sy4">;</span> <span class="co1">// P = const U&, A = 1+2</span>
<span class="co1">// same rules as for calling f(1+2) where f is</span>
<span class="co1">// template <class U> void f(const U& u)</span>
<span class="co1">// U is deduced to be int, and the type of x is const int&</span>
<span class="kw4">auto</span> l <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">13</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// P = std::initializer_list<U>, A = {13}</span>
<span class="co1">// U is deduced to be int, the type of l is std::initializer_list<int></span></pre></div></div>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<p>In direct-list-initialization (but not in copy-list-initalization), when deducing the meaning of the <a href="auto.html" title="cpp/language/auto">auto</a> from a braced-init-list, the braced-init-list must contain only one element, and the type of auto will be the type of that element:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">auto</span> x1 <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">3</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// x1 is std::initializer_list<int></span>
<span class="kw4">auto</span> x2<span class="br0">{</span><span class="nu0">1</span>, <span class="nu0">2</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// error: not a single element</span>
<span class="kw4">auto</span> x3<span class="br0">{</span><span class="nu0">3</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// x3 is int (before C++17 it was std::initializer_list<int>)</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx17">(since C++17)</span></td></tr>
</table>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<h4> <span class="mw-headline" id="auto-returning_functions">auto-returning functions</span></h4>
<p>Template argument deduction is used in declarations of <a href="functions.html" title="cpp/language/functions">functions</a>, when deducing the meaning of the <a href="auto.html" title="cpp/language/auto">auto</a> specifier in the function's return type, from the return statement.
</p><p>For auto-returning functions, the parameter <code><b>P</b></code> is obtained as follows: in <code>T</code>, the declared return type of the function that includes <code>auto</code>, every occurrence of <code>auto</code> is replaced with an imaginary type template parameter <code>U</code>. The argument <code><b>A</b></code> is the expression of the <a href="return.html" title="cpp/language/return">return</a> statement, and if the return statement has no operand, <code><b>A</b></code> is <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">void</span><span class="br0">(</span><span class="br0">)</span></span></span>. After deduction of <code>U</code> from <code><b>P</b></code> and <code><b>A</b></code> following the rules described above, the deduced <code>U</code> is substituted into <code>T</code> to get the actual return type.
</p><p>If such function has multiple return statements, the deduction is performed for each return statement. All the resulting types must be the same and become the actual return type.
</p><p>If such function has no return statement, <code><b>A</b></code> is <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">void</span><span class="br0">(</span><span class="br0">)</span></span></span> when deducing.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">auto</span> f<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">42</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// P = auto, A = 42</span>
<span class="co1">// U is deduced to be int, the return type of f is int</span></pre></div></div>
<p>Note: the meaning of <span class="t-c"><span class="mw-geshi cpp source-cpp">decltype<span class="br0">(</span><span class="kw4">auto</span><span class="br0">)</span></span></span> placeholder in variable and function declarations does not use template argument deduction
</p>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=7" title="Edit section: overload resolution">edit</a>]</span> <span class="mw-headline" id="overload_resolution">overload resolution</span></h4>
<p>Template argument deduction is used during <a href="overload_resolution.html" title="cpp/language/overload resolution">overload resolution</a>, when generating specializations from a candidate template function.
<code><b>P</b></code> and <code><b>A</b></code> are the same as in a regular function call
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><a href="../string/basic_string.html"><span class="kw1072">std::<span class="me2">string</span></span></a> s<span class="sy4">;</span>
<a href="../string/basic_string/getline.html"><span class="kw1080">std::<span class="me2">getline</span></span></a><span class="br0">(</span><a href="../io/cin.html"><span class="kw1479">std::<span class="me2">cin</span></span></a>, s<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// "std::getline" names 4 function templates, </span>
<span class="co1">// 2 of which are candidate functions (correct number of parameters)</span>
<span class="co1">// 1st candidate template:</span>
<span class="co1">// P1 = std::basic_istream<CharT,Traits>&, A1 = std::cin</span>
<span class="co1">// P2 = std::basic_string<CharT,Traits,Allocator>&, A2 = s</span>
<span class="co1">// deduction determines the type template parameters CharT, Traits, and Allocator</span>
<span class="co1">// specialization std::getline<char, std::char_traits<char>, std::allocator<char>></span>
<span class="co1">// 2nd candidate template:</span>
<span class="co1">// P1 = std::basic_istream<CharT,Traits>&&, A1 = std::cin</span>
<span class="co1">// P2 = std::basic_string<CharT,Traits,Allocator>&, A2 = s</span>
<span class="co1">// deduction determines the type template parameters CharT, Traits, and Allocator</span>
<span class="co1">// specialization std::getline<char, std::char_traits<char>, std::allocator<char>></span>
<span class="co1">// overload resolution ranks reference binding from lvalue std::cin and picks</span>
<span class="co1">// the first of the two candidate specializations</span></pre></div></div>
<p>If deduction fails<span class="t-rev-inl t-since-cxx14"><span>, or if deduction succeeds, but the specialization it produces would be invalid (for example, an overloaded operator whose parameters are neither class nor enumeration types),</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> the specialization is not included in the overload set, similar to <a href="sfinae.html" title="cpp/language/sfinae">SFINAE</a>
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=8" title="Edit section: address of an overload set">edit</a>]</span> <span class="mw-headline" id="address_of_an_overload_set">address of an overload set</span></h4>
<p>Template argument deduction is used when taking an <a href="overloaded_address.html" title="cpp/language/overloaded address">address of a overload set</a>, which includes function templates
</p><p>The function type of the function template is <code><b>P</b></code>. The <a href="overloaded_address.html" title="cpp/language/overloaded address">target type</a> is the type of <code><b>A</b></code>
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><a href="../io/cout.html"><span class="kw1481">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> <a href="../io/manip/endl.html"><span class="kw1514">std::<span class="me2">endl</span></span></a><span class="sy4">;</span> <span class="co1">// std::endl names a function template</span>
<span class="co1">// type of endl P = std::basic_ostream<CharT,Traits>&(std::basic_ostream<CharT,Traits>&)</span>
<span class="co1">// operator<< parameter A = std::basic_ostream<char,std::char_traits<char>>&(*)(std::basic_ostream<char,std::char_traits<char>>&)</span>
<span class="co1">// (other overloads of operator<< are not viable) </span>
<span class="co1">// deduction determines the type template parameters CharT and Traits</span></pre></div></div>
<p>An additional rule is applied to the deduction in this case: when comparing function parameters <code><b>P</b></code>i and <code><b>A</b></code>i, if any <code><b>P</b></code>i is an rvalue reference to cv-unqualified template parameter (a "forwarding reference") and the corresponding <code><b>A</b></code>i is an lvalue reference, then <code><b>P</b></code>i is adjusted to the template parameter type (T&& becomes T)
</p>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<p>If the return type of the function template is a placeholder (<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw4">auto</span></span></span> or <span class="t-c"><span class="mw-geshi cpp source-cpp">decltype<span class="br0">(</span><span class="kw4">auto</span><span class="br0">)</span></span></span>), that return type is a non-deduced context and is determined from the instantiation.
</p>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=9" title="Edit section: partial ordering">edit</a>]</span> <span class="mw-headline" id="partial_ordering">partial ordering</span></h4>
<p>Template argument deduction is used during <a href="function_template.html" title="cpp/language/function template">partial ordering of overloaded function templates</a>
</p>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">This section is incomplete<br />Reason: mini-example </td></tr></table>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=10" title="Edit section: conversion function template">edit</a>]</span> <span class="mw-headline" id="conversion_function_template">conversion function template</span></h4>
<p>Template argument deduction is used when selecting <a href="cast_operator.html" title="cpp/language/cast operator">user-defined conversion function</a> template arguments.
</p><p><code><b>A</b></code> is the type that is required as the result of the conversion. <code><b>P</b></code> is the return type of the conversion function template, except that
</p>
<div class="t-li1"><span class="t-li">a)</span> If the return type is a reference type then <code><b>P</b></code> is the referred type</div>
<div class="t-li1"><span class="t-li">b)</span> If the return type is an array type and <code><b>A</b></code> is not a reference type, then <code><b>P</b></code> is the pointer type obtained by array-to-pointer conversion</div>
<div class="t-li1"><span class="t-li">c)</span> If the return type is a function type and <code><b>A</b></code> is not a reference type, then <code><b>P</b></code> is the function pointer type obtained by function-to-pointer conversion</div>
<div class="t-li1"><span class="t-li">d)</span> If <code><b>P</b></code> is cv-qualified, the top-level cv qualifiers are ignored.</div>
<p>If <code><b>A</b></code> is cv-qualified, the top-level cv qualifiers are ignored. If <code><b>A</b></code> is a reference type, the referred type is used by deduction.
</p><p>If, the usual deduction from <code><b>P</b></code> and <code><b>A</b></code> (as described above) fails, the following alternatives are additionally considered:
</p>
<div class="t-li1"><span class="t-li">a)</span> If the original A is a reference type, A can be more cv-qualified than the deduced A</div>
<div class="t-li1"><span class="t-li">b)</span> If A is a pointer or pointer to member type, the deduced A is allowed to be any pointer that can be converted to A by qualification conversion.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> A <span class="br0">{</span>
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> operator T<span class="sy2">***</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
A a<span class="sy4">;</span>
<span class="kw4">const</span> <span class="kw4">int</span> <span class="sy2">*</span> <span class="kw4">const</span> <span class="sy2">*</span> <span class="kw4">const</span> <span class="sy2">*</span> p1 <span class="sy1">=</span> a<span class="sy4">;</span> <span class="co1">// A = const int * const * const *</span>
<span class="co1">// P = T***</span>
<span class="co1">// regular function-call deduction for </span>
<span class="co1">// template<class T> void f(T*** p) as if called with the argument</span>
<span class="co1">// of type const int * const * const * fails</span>
<span class="co1">// additional deduction for conversion functions determines T = int </span>
<span class="co1">// (deduced A is int***, convertible to const int * const * const *)</span></pre></div></div>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<div class="t-li1"><span class="t-li">c)</span> If the original A is a function pointer type, the deduced A is allowed to be pointer to noexcept function, convertible to the original A by function pointer conversion.</div>
<div class="t-li1"><span class="t-li">d)</span> If the original A is a pointer to member function, the deduced A is allowed to be a pointer to noexcept member function, convertible to the original A by function pointer conversion.
</div></td>
<td><span class="t-mark-rev t-since-cxx17">(since C++17)</span></td></tr>
</table></div>
<p>See <a href="member_template.html#Conversion_function_templates" title="cpp/language/member template">member template</a> for other rules regarding conversion function templates.
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=11" title="Edit section: explicit instantiation">edit</a>]</span> <span class="mw-headline" id="explicit_instantiation">explicit instantiation</span></h4>
<p>Template argument deduction is used in <a href="function_template.html#Explicit_instantiation" title="cpp/language/function template">explicit instantiations</a>, <a href="template_specialization.html" title="cpp/language/template specialization">explicit specializations</a>, and those <a href="friend.html#Template_friends" title="cpp/language/friend">friend declarations</a> where the declarator-id happens to refer to a specialization of a function template (for example, <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">friend</span> ostream<span class="sy3">&</span> operator<span class="sy1"><<</span> <span class="sy1"><></span> <span class="br0">(</span>...<span class="br0">)</span></span></span>), if not all template arguments are explicitly specified or defaulted, template argument deduction is used to determine which template's specialization is referred to.
</p><p><code><b>P</b></code> is the type of the function template that is being considered as a potential match, and <code><b>A</b></code> is the function type from the declaration. If there are no matches or more than one match (after partial ordering), the function declaration is ill-formed.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> X<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>X a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// first template f</span>
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> X<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>X<span class="sy2">*</span> a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// second template f</span>
 
<span class="kw1">template</span><span class="sy1"><></span> <span class="kw4">void</span> f<span class="sy1"><></span><span class="br0">(</span><span class="kw4">int</span> <span class="sy2">*</span>a<span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// explicit specialization of f</span>
<span class="co1">// P1 = void(X), A1=void(int*): deduces X=int*</span>
<span class="co1">// P2 = void(X*), A2=void(int*): deduces X=int</span>
<span class="co1">// foo<int*>(int*) and f<int>(int*) are then submitted to partial ordering</span>
<span class="co1">// which selects f<int>(int*) as the more specialized</span></pre></div></div>
<p>An additional rule is applied to the deduction in this case: when comparing function parameters <code><b>P</b></code>i and <code><b>A</b></code>i, if any <code><b>P</b></code>i is an rvalue reference to cv-unqualified template parameter (a "forwarding reference") and the corresponding <code><b>A</b></code>i is an lvalue reference, then <code><b>P</b></code>i is adjusted to the template parameter type (T&& becomes T)
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=12" title="Edit section: deallocation function template">edit</a>]</span> <span class="mw-headline" id="deallocation_function_template">deallocation function template</span></h4>
<p>Template argument deduction is used when determining if a <a href="../memory/new/operator_delete.html" title="cpp/memory/new/operator delete">deallocation function</a> template specialization matches a given placement form of <code>operator new</code>.
</p><p><code><b>P</b></code> is the type of the function template that is being considered as a potential match, and <code><b>A</b></code> is the function type of the deallocation function that would be the match for the placement operator new under consideration. If there is no match or more than one match (after overload resolution), the placement deallocation function is not called (memory leak may occur).
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> X <span class="br0">{</span>
X<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">throw</span> <a href="../error/runtime_error/runtime_error.html"><span class="kw650">std::<span class="me2">runtime_error</span></span></a><span class="br0">(</span><span class="st0">""</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="kw4">static</span> <span class="kw4">void</span><span class="sy2">*</span> <a href="../memory/new/operator_new.html"><span class="kw589">operator new</span></a><span class="br0">(</span><a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> sz, <span class="kw4">bool</span> b<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="sy4">::</span><a href="../memory/new/operator_new.html"><span class="kw589">operator new</span></a><span class="br0">(</span>sz<span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="kw4">static</span> <span class="kw4">void</span><span class="sy2">*</span> <a href="../memory/new/operator_new.html"><span class="kw589">operator new</span></a><span class="br0">(</span><a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> sz, <span class="kw4">double</span> f<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="sy4">::</span><a href="../memory/new/operator_new.html"><span class="kw589">operator new</span></a><span class="br0">(</span>sz<span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">typename</span> T<span class="sy1">></span> <span class="kw4">static</span> <span class="kw4">void</span> <a href="../memory/new/operator_delete.html"><span class="kw591">operator delete</span></a><span class="br0">(</span><span class="kw4">void</span><span class="sy2">*</span> ptr, T arg<span class="br0">)</span> <span class="br0">{</span>
<span class="sy4">::</span><a href="../memory/new/operator_delete.html"><span class="kw591">operator delete</span></a><span class="br0">(</span>ptr<span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
<span class="kw1">try</span> <span class="br0">{</span>
X<span class="sy2">*</span> p1 <span class="sy1">=</span> new <span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span> X<span class="sy4">;</span> <span class="co1">// when X() throws, operator delete is looked up</span>
<span class="co1">// P1=void(void*, T), A1=void(void*, bool): deduces T=bool</span>
<span class="co1">// P2=void(void*, T), A2=void(void*, double): T=double</span>
<span class="co1">// overload resolution picks operator delete<bool></span>
<span class="br0">}</span> <span class="kw1">catch</span><span class="br0">(</span><span class="kw4">const</span> <a href="../error/exception.html"><span class="kw644">std::<span class="me2">exception</span></span></a><span class="sy3">&</span><span class="br0">)</span> <span class="br0">{</span> <span class="br0">}</span>
<span class="kw1">try</span> <span class="br0">{</span>
X<span class="sy2">*</span> p1 <span class="sy1">=</span> new <span class="br0">(</span><span class="nu16">13.2</span><span class="br0">)</span> X<span class="sy4">;</span> <span class="co1">// same lookup, picks operator delete<double></span>
<span class="br0">}</span> <span class="kw1">catch</span><span class="br0">(</span><span class="kw4">const</span> <a href="../error/exception.html"><span class="kw644">std::<span class="me2">exception</span></span></a><span class="sy3">&</span><span class="br0">)</span> <span class="br0">{</span> <span class="br0">}</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=13" title="Edit section: Alias templates">edit</a>]</span> <span class="mw-headline" id="Alias_templates">Alias templates</span></h3>
<p><a href="type_alias.html" title="cpp/language/type alias">Alias templates</a> are never deduced
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw1">struct</span> Alloc <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw1">using</span> Vec <span class="sy1">=</span> vector<span class="sy1"><</span>T, Alloc<span class="sy1"><</span>T<span class="sy1">>></span><span class="sy4">;</span>
Vec<span class="sy1"><</span><span class="kw4">int</span><span class="sy1">></span> v<span class="sy4">;</span>
 
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span>,<span class="kw1">class</span><span class="sy1">></span> <span class="kw1">class</span> TT<span class="sy1">></span> <span class="kw4">void</span> g<span class="br0">(</span>TT<span class="sy1"><</span><span class="kw4">int</span>, Alloc<span class="sy1"><</span><span class="kw4">int</span><span class="sy1">>></span><span class="br0">)</span><span class="sy4">;</span>
g<span class="br0">(</span>v<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// ok: deduces TT = vector</span>
 
<span class="kw1">template</span><span class="sy1"><</span><span class="kw1">template</span><span class="sy1"><</span><span class="kw1">class</span><span class="sy1">></span> <span class="kw1">class</span> TT<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span>TT<span class="sy1"><</span><span class="kw4">int</span><span class="sy1">></span><span class="br0">)</span><span class="sy4">;</span>
f<span class="br0">(</span>v<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// error: TT cannot be deduced as "Vec" because Vec is an alias template</span></pre></div></div>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=14" title="Edit section: Implicit conversions">edit</a>]</span> <span class="mw-headline" id="Implicit_conversions">Implicit conversions</span></h3>
<p>Type deduction does not consider implicit conversions (other than type adjustments listed above): that's the job for <a href="overload_resolution.html" title="cpp/language/overload resolution">overload resolution</a>, which happens later.
</p>
<table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<p>However, if deduction succeeds for all parameters that participate in template argument deduction, and all template arguments that aren't deduced are explicitly specified or defaulted, then the remaining function parameters are compared with the corresponding function arguments. For each remaining parameter P with a type that was non-dependent before substitution of any explicitly-specified template arguments, if the corresponding argument A cannot be implicitly converted to P, deduction fails.
</p><p>Parameters with dependent types in which no template-parameters participate in template argument deduction, and parameters that became non-dependent due to substitution of explicitly-specified template arguments will be checked during overload resolution.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw1">struct</span> Z <span class="br0">{</span>
<span class="kw1">typedef</span> <span class="kw1">typename</span> T<span class="sy4">::</span><span class="me2">x</span> xx<span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw1">typename</span> Z<span class="sy1"><</span>T<span class="sy1">></span><span class="sy4">::</span><span class="me2">xx</span> f<span class="br0">(</span><span class="kw4">void</span><span class="sy2">*</span>, T<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// #1</span>
<span class="kw1">template</span> <span class="sy1"><</span><span class="kw1">class</span> T<span class="sy1">></span> <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span>, T<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// #2</span>
 
<span class="kw1">struct</span> A <span class="br0">{</span><span class="br0">}</span> a<span class="sy4">;</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
f<span class="br0">(</span><span class="nu0">1</span>, a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// for #1, deduction determines T = struct A, but the remaining argument 1</span>
<span class="co1">// cannot be implicitly converted to its parameter void*, so deduction fails</span>
<span class="co1">// Instantiation of the return type is not requested.</span>
<span class="co1">// for #2, deduction determines T = struct A, and the remaining argument 1</span>
<span class="co1">// can be implicitly converted to its parameter int, deduction succeeds for #2</span>
<span class="co1">// The function call compiles as a call to #2 (deduction failure is SFINAE)</span>
<span class="br0">}</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=edit&section=15" title="Edit section: Defect reports">edit</a>]</span> <span class="mw-headline" id="Defect_reports">Defect reports</span></h3>
<p>The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> Applied to
</th>
<th> Behavior as published
</th>
<th> Correct behavior
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1391">CWG 1391</a>
</td>
<td> C++14
</td>
<td> effect of implicit conversions of the arguments that aren't involved in deduction were not specified
</td>
<td> specified as described above
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#2052">CWG 2052</a>
</td>
<td> C++14
</td>
<td> deducing an operator with non-class arguments was a hard error (in some compilers)
</td>
<td> soft error if there are other overloads
</td></tr></table>
<!--
NewPP limit report
Preprocessor visited node count: 3627/1000000
Preprocessor generated node count: 11524/1000000
Post‐expand include size: 47898/2097152 bytes
Template argument size: 23873/2097152 bytes
Highest expansion depth: 17/40
Expensive parser function count: 0/100
-->
<!-- Saved in parser cache with key mwiki1-mwiki_en_:pcache:idhash:13515-0!*!0!!en!*!* and timestamp 20151118165324 -->
</div> <!-- /bodycontent -->
<!-- printfooter -->
<div class="printfooter">
Retrieved from "<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&oldid=81905">http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&oldid=81905</a>" </div>
<!-- /printfooter -->
<!-- catlinks -->
<div id='catlinks' class='catlinks'><div id="mw-normal-catlinks" class="mw-normal-catlinks"><a href="http://en.cppreference.com/w/Special:Categories" title="Special:Categories">Category</a>: <ul><li><a href="http://en.cppreference.com/w/Category:Todo_with_reason" title="Category:Todo with reason">Todo with reason</a></li></ul></div></div> <!-- /catlinks -->
<div class="visualClear"></div>
<!-- debughtml -->
<!-- /debughtml -->
</div>
<!-- /bodyContent -->
</div>
</div>
<!-- /content -->
<!-- footer -->
<div id="cpp-footer-base" class="noprint">
<div id="footer">
<div id="cpp-navigation">
<h5>Navigation</h5>
<ul>
<li id="n-Support-us"><a href="http://www.cppreference.com/support" rel="nofollow">Support us</a></li><li id="n-recentchanges"><a href="http://en.cppreference.com/w/Special:RecentChanges" title="A list of recent changes in the wiki [r]" accesskey="r">Recent changes</a></li><li id="n-FAQ"><a href="http://en.cppreference.com/w/Cppreference:FAQ">FAQ</a></li><li id="n-Offline-version"><a href="http://en.cppreference.com/w/Cppreference:Archives">Offline version</a></li> </ul>
</div>
<div id="cpp-toolbox">
<h5><span>Toolbox</span><a href="template_argument_deduction.html#"></a></h5>
<ul>
<li id="t-whatlinkshere"><a href="http://en.cppreference.com/w/Special:WhatLinksHere/cpp/language/template_argument_deduction" title="A list of all wiki pages that link here [j]" accesskey="j">What links here</a></li><li id="t-recentchangeslinked"><a href="http://en.cppreference.com/w/Special:RecentChangesLinked/cpp/language/template_argument_deduction" title="Recent changes in pages linked from this page [k]" accesskey="k">Related changes</a></li><li id="t-upload"><a href="http://upload.cppreference.com/w/Special:Upload" title="Upload files [u]" accesskey="u">Upload file</a></li><li id="t-specialpages"><a href="http://en.cppreference.com/w/Special:SpecialPages" title="A list of all special pages [q]" accesskey="q">Special pages</a></li><li id="t-print"><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&printable=yes" rel="alternate" title="Printable version of this page [p]" accesskey="p">Printable version</a></li><li id="t-permalink"><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&oldid=81905" title="Permanent link to this revision of the page">Permanent link</a></li><li id="t-info"><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/template_argument_deduction&action=info">Page information</a></li> </ul>
</div>
<ul id="footer-info">
<li id="footer-info-lastmod"> This page was last modified on 18 November 2015, at 08:53.</li>
<li id="footer-info-viewcount">This page has been accessed 57,315 times.</li>
</ul>
<ul id="footer-places">
<li id="footer-places-privacy"><a href="http://en.cppreference.com/w/Cppreference:Privacy_policy" title="Cppreference:Privacy policy">Privacy policy</a></li>
<li id="footer-places-about"><a href="http://en.cppreference.com/w/Cppreference:About" title="Cppreference:About">About cppreference.com</a></li>
<li id="footer-places-disclaimer"><a href="http://en.cppreference.com/w/Cppreference:General_disclaimer" title="Cppreference:General disclaimer">Disclaimers</a></li>
</ul>
<ul id="footer-icons" class="noprint">
<li id="footer-poweredbyico">
<a href="http://www.mediawiki.org/"><img src="../../../mwiki/skins/common/images/poweredby_mediawiki_88x31.png" alt="Powered by MediaWiki" width="88" height="31" /></a> <a href="http://qbnz.com/highlighter/"><img src="../../../../upload.cppreference.com/mwiki/images/2/2b/powered_by_geshi_88x31.png" alt="Powered by GeSHi" height="31" width="88" /></a> <a href="http://www.tigertech.net/referral/cppreference.com"><img src="../../../../upload.cppreference.com/mwiki/images/9/94/powered_by_tigertech_88x31.png" alt="Hosted by Tiger Technologies" height="31" width="88" /></a> </li>
</ul>
<div style="clear:both">
</div>
</div>
</div>
<!-- /footer -->
<script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=skins.cppreference2&only=scripts&skin=cppreference2&*"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../mwiki/load.php%3Fdebug=false&lang=en&modules=site&only=scripts&skin=cppreference2&*"></script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.082 secs. -->
</body>
<!-- Cached 20151118170133 -->
</html>
|