File: overload_resolution.html

package info (click to toggle)
cppreference-doc 20151129%2Bdfsg0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 220,072 kB
  • ctags: 451
  • sloc: xml: 474,959; python: 1,770; php: 263; makefile: 162; sh: 30; cpp: 9; ansic: 9
file content (687 lines) | stat: -rw-r--r-- 94,476 bytes parent folder | download
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
<!DOCTYPE html>
<html lang="en" dir="ltr" class="client-nojs">
<head>
<title>Overload resolution - 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/overload_resolution&amp;action=edit" />
<link rel="edit" title="Edit" href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;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&amp;feed=atom" />
<link rel="stylesheet" href="../../../mwiki/load.php%3Fdebug=false&amp;lang=en&amp;modules=ext.gadget.ColiruCompiler%257Cext.rtlcite%257Cmediawiki.legacy.commonPrint%252Cshared%257Cskins.cppreference2&amp;only=styles&amp;skin=cppreference2&amp;*.css" />
<meta name="ResourceLoaderDynamicStyles" content="" />
<link rel="stylesheet" href="../../../mwiki/load.php%3Fdebug=false&amp;lang=en&amp;modules=site&amp;only=styles&amp;skin=cppreference2&amp;*.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&amp;lang=en&amp;modules=startup&amp;only=scripts&amp;skin=cppreference2&amp;*"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/overload_resolution","wgTitle":"cpp/language/overload resolution","wgCurRevisionId":81906,"wgArticleId":12784,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"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/overload_resolution","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_overload_resolution 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&amp;returnto=cpp%2Flanguage%2Foverload+resolution&amp;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&amp;returnto=cpp%2Flanguage%2Foverload+resolution" 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="overload_resolution.html"  title="View the content page [c]" accesskey="c">Page</a></span></li>
					<li  id="ca-talk"><span><a href="http://en.cppreference.com/w/Talk:cpp/language/overload_resolution"  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="overload_resolution.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="overload_resolution.html" >View</a></span></li>
					<li id="ca-edit"><span><a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;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/overload_resolution&amp;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="overload_resolution.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">Overload resolution</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">From cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>&lrm; | <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">&#160;</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&amp;action=edit">&#91;edit&#93;</a></span></div></div></div></div><div class="t-navbar-sep">&#160;</div><div class="t-navbar-head"><a href="../language.1.html" title="cpp/language"> C++ language</a></div><div class="t-navbar-sep">&#160;</div><div class="t-navbar-head"><a href="functions.html" title="cpp/language/functions"> Functions</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-h2"><td colspan="5"> Declarations </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> function declaration</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html#Parameter_list" title="cpp/language/function"> function parameter list</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html#Function_definition" title="cpp/language/function"> function definition</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="default_arguments.html" title="cpp/language/default arguments"> default arguments</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="variadic_arguments.html" title="cpp/language/variadic arguments"> variadic arguments</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="inline.html" title="cpp/language/inline"> inline specifier</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lambda.html" title="cpp/language/lambda"> lambda expression</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Function calls </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="adl.html" title="cpp/language/adl"> argument-dependent lookup (ADL)</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operator_other.html#Built-in_function_call_operator" title="cpp/language/operator other"> function-call operator</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concept/FunctionObject.html" title="cpp/concept/FunctionObject"> function objects</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> Overloading </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> overload resolution</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="operators.html" title="cpp/language/operators"> operator overloading</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="overloaded_address.html" title="cpp/language/overloaded address"> address of an overload set</a> </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/functions/navbar_content&amp;action=edit">&#91;edit&#93;</a></span></div></div></div></div><div class="t-navbar-sep">&#160;</div></div>
<p>In order to compile a function call, the compiler must first perform <a href="lookup.html" title="cpp/language/lookup">name lookup</a>, which, for functions, may involve <a href="adl.html" title="cpp/language/adl">argument-dependent lookup</a>, and for function templates may be followed by <a href="template_argument_deduction.html" title="cpp/language/template argument deduction">template argument deduction</a>. If these steps produce more than one <i>candidate function</i>, then <i>overload resolution</i> is performed to select the function that will actually be called.
</p><p>In general, the candidate function whose parameters match the arguments most closely is the one that is called.
</p>
<table id="toc" class="toc"><tr><td><div id="toctitle"><h2>Contents</h2></div>
<ul>
<li class="toclevel-1 tocsection-1"><a href="overload_resolution.html#Details"><span class="tocnumber">1</span> <span class="toctext">Details</span></a></li>
<li class="toclevel-1 tocsection-2"><a href="overload_resolution.html#Candidate_functions"><span class="tocnumber">2</span> <span class="toctext">Candidate functions</span></a>
<ul>
<li class="toclevel-2 tocsection-3"><a href="overload_resolution.html#Call_to_a_named_function"><span class="tocnumber">2.1</span> <span class="toctext">Call to a named function</span></a></li>
<li class="toclevel-2 tocsection-4"><a href="overload_resolution.html#Call_to_a_class_object"><span class="tocnumber">2.2</span> <span class="toctext">Call to a class object</span></a></li>
<li class="toclevel-2 tocsection-5"><a href="overload_resolution.html#Call_to_an_overloaded_operator"><span class="tocnumber">2.3</span> <span class="toctext">Call to an overloaded operator</span></a></li>
<li class="toclevel-2 tocsection-6"><a href="overload_resolution.html#Initialization_by_constructor"><span class="tocnumber">2.4</span> <span class="toctext">Initialization by constructor</span></a></li>
<li class="toclevel-2 tocsection-7"><a href="overload_resolution.html#Copy-initialization_by_conversion"><span class="tocnumber">2.5</span> <span class="toctext">Copy-initialization by conversion</span></a></li>
<li class="toclevel-2 tocsection-8"><a href="overload_resolution.html#Non-class_initialization_by_conversion"><span class="tocnumber">2.6</span> <span class="toctext">Non-class initialization by conversion</span></a></li>
<li class="toclevel-2 tocsection-9"><a href="overload_resolution.html#Reference_initialization_by_conversion"><span class="tocnumber">2.7</span> <span class="toctext">Reference initialization by conversion</span></a></li>
<li class="toclevel-2 tocsection-10"><a href="overload_resolution.html#List-initialization"><span class="tocnumber">2.8</span> <span class="toctext">List-initialization</span></a></li>
</ul>
</li>
<li class="toclevel-1 tocsection-11"><a href="overload_resolution.html#Viable_functions"><span class="tocnumber">3</span> <span class="toctext">Viable functions</span></a></li>
<li class="toclevel-1 tocsection-12"><a href="overload_resolution.html#Best_viable_function"><span class="tocnumber">4</span> <span class="toctext">Best viable function</span></a></li>
<li class="toclevel-1 tocsection-13"><a href="overload_resolution.html#Ranking_of_implicit_conversion_sequences"><span class="tocnumber">5</span> <span class="toctext">Ranking of implicit conversion sequences</span></a></li>
<li class="toclevel-1 tocsection-14"><a href="overload_resolution.html#Implicit_conversion_sequence_in_list-initialization"><span class="tocnumber">6</span> <span class="toctext">Implicit conversion sequence in list-initialization</span></a></li>
<li class="toclevel-1 tocsection-15"><a href="overload_resolution.html#Defect_reports"><span class="tocnumber">7</span> <span class="toctext">Defect reports</span></a></li>
<li class="toclevel-1 tocsection-16"><a href="overload_resolution.html#See_also"><span class="tocnumber">8</span> <span class="toctext">See also</span></a></li>
<li class="toclevel-1 tocsection-17"><a href="overload_resolution.html#References"><span class="tocnumber">9</span> <span class="toctext">References</span></a></li>
</ul>
</td></tr></table>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=1" title="Edit section: Details">edit</a>]</span> <span class="mw-headline" id="Details">Details</span></h3>
<p>Before overload resolution begins, the functions selected by name lookup and template argument deduction are combined to form the set of <i>candidate functions</i> (the exact criteria depend on the context in which overload resolution takes place, see below).
</p><p>If any candidate function is a <a href="member_functions.html" title="cpp/language/member functions">member function</a> (static or non-static), but not a constructor, it is treated as if it has an extra parameter (<i>implicit object parameter</i>) which represents the object for which they are called and appears before the first of the actual parameters.
</p><p>Similarly, the object on which a member function is being called is prepended to the argument list as the <i>implied object argument</i>.
</p><p>For member functions of class <code>X</code>, the type of the implicit object parameter is affected by cv-qualifications and ref-qualifications of the member function as described in <a href="member_functions.html" title="cpp/language/member functions">member functions</a>.
</p><p>The user-defined conversion functions are considered to be members of the <i>implied object argument</i> for the purpose of determining the type of the <i>implicit object parameter</i>.
</p><p>The member functions introduced by a using-declaration into a derived class are considered to be members of the derived class for the purpose of defining the type of the <i>implicit</i>
object parameter<i>.</i>
</p><p>For the static member functions, the <i>implicit object parameter</i> is considered to match any
object: its type is not examined and no conversion sequence is attempted for it.
</p><p>For the rest of overload resolution, the <i>implied object argument</i> is indistinguishable from other arguments, but the following special rules apply to the <i>implicit object parameter</i>:
</p>
<div class="t-li1"><span class="t-li">1)</span> the argument for the implicit object parameter cannot be held in a temporary object</div>
<div class="t-li1"><span class="t-li">2)</span> user-defined conversions cannot be applied to the implicit object parameter</div>
<div class="t-li1"><span class="t-li">3)</span> rvalues can be bound to non-const implicit object parameter (unless this is for a ref-qualified member function) and does not affect the ranking of the implicit conversions.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> B <span class="br0">&#123;</span> <span class="kw4">void</span> f<span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="br0">&#125;</span><span class="sy4">;</span>
<span class="kw1">struct</span> A <span class="br0">&#123;</span> operator B<span class="sy3">&amp;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="br0">&#125;</span><span class="sy4">;</span>
A a<span class="sy4">;</span>
a.<span class="me1">f</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// Error: user-defined conversions cannot be applied</span>
        <span class="co1">// to the implicit object parameter</span>
<span class="kw1">static_cast</span><span class="sy1">&lt;</span>B<span class="sy3">&amp;</span><span class="sy1">&gt;</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>.<span class="me1">f</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// OK</span></pre></div></div></div>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=2" title="Edit section: Candidate functions">edit</a>]</span> <span class="mw-headline" id="Candidate_functions">Candidate functions</span></h3>
<p>The set of candidate functions and the list of arguments is prepared in a unique way for each of the contexts where overload resolution is used:
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=3" title="Edit section: Call to a named function">edit</a>]</span> <span class="mw-headline" id="Call_to_a_named_function">Call to a named function</span></h4>
<p>If <code>E</code> in a <a href="operator_other.html#Built-in_function_call_operator" title="cpp/language/operator other">function call expression</a> <code>E(args)</code> names a set of overloaded functions and/or function templates (but not callable objects), the following rules are followed:
</p>
<ul><li> If the expression <code>E</code> has the form <code>PA-&gt;B</code> or <code>A.B</code> (where A has class type cv T), then <code>B</code> is <a href="lookup.html" title="cpp/language/lookup">looked up</a> as a member function of <code>T</code>. The function declarations found by that lookup are the candidate functions. The argument list for the purpose of overload resolution has the implied object argument of type cv T.
</li><li> If the expression <code>E</code> is a <a href="expressions.html#Primary_expressions" title="cpp/language/expressions">primary expression</a>, the name is <a href="lookup.html" title="cpp/language/lookup">looked up</a> following normal rules for function calls (which may involve <a href="adl.html" title="cpp/language/adl">ADL</a>). The function declarations found by this lookup are (due to the way lookup works) either:
</li></ul>
<dl><dd>a) all non-member functions (in which case the argument list for the purpose of overload resolution is exactly the argument list used in the function call expression)
</dd><dd>b) all member functions of some class <code>T</code>, in which case, if <a href="this.html" title="cpp/language/this">this</a> is in scope and refers to <code>T</code>, <code>*this</code> is used as the implied object argument. Otherwise (if <code>this</code> is not in scope or does not point to <code>T</code>, a fake object of type <code>T</code> is used as the implied object argument, and if overload resolution subsequently selects a non-static member function, the program is ill-formed.
</dd></dl>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=4" title="Edit section: Call to a class object">edit</a>]</span> <span class="mw-headline" id="Call_to_a_class_object">Call to a class object</span></h4>
<p>If <code>E</code> in a <a href="operator_other.html#Built-in_function_call_operator" title="cpp/language/operator other">function call expression</a> <code>E(args)</code> has class type cv <code>T</code>, then
</p>
<ul><li> The function-call operators of T are obtained by ordinary <a href="lookup.html" title="cpp/language/lookup">lookup</a> of the name <span class="t-c"><span class="mw-geshi cpp source-cpp">operator<span class="br0">&#40;</span><span class="br0">&#41;</span></span></span> in the context of the expression <code>(E).operator()</code>, and every declaration found is added to the set of candidate functions. 
</li><li> For each non-explicit <a href="cast_operator.html" title="cpp/language/cast operator">user-defined conversion function</a> in <code>T</code> or in a base of <code>T</code> (unless hidden), whose cv-qualifiers is same or greater than <code>T</code>'s cv-qualifiers, and where the conversion function converts to:
</li></ul>
<dl><dd><ul><li> to pointer-to-function
</li><li> to reference-to-pointer-to-function
</li><li> to reference-to-function
</li></ul>
</dd><dd>then a surrogate function with a unique name whose first parameter is the result of the conversion, the remaining parameters are the parameter-list accepted by the result of the conversion, and the return type is the return type of the result of the conversion, is added to the set of candidate functions. If this surrogate function is selected by the subsequent overload resolution, then the user-defined conversion function will be called and then the result of the conversion will be called.
</dd></dl>
<p>In any case, the argument list for the purpose of overload resolution is the argument list of the function call expression preceded by the implied object argument <code>E</code> (when matching against the surrogate function, the user-defined conversion will automatically convert the implied object argument to the first argument of the surrogate function).
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> f1<span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span><span class="sy4">;</span>
<span class="kw4">int</span> f2<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="sy4">;</span>
<span class="kw1">struct</span> A <span class="br0">&#123;</span>
    <span class="kw1">using</span> fp1 <span class="sy1">=</span> <span class="kw4">int</span><span class="br0">&#40;</span><span class="sy2">*</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span><span class="sy4">;</span>
    operator fp1<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> f1<span class="sy4">;</span> <span class="br0">&#125;</span> <span class="co1">// conversion function to pointer to function</span>
    <span class="kw1">using</span> fp2 <span class="sy1">=</span> <span class="kw4">int</span><span class="br0">&#40;</span><span class="sy2">*</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="sy4">;</span>
    operator fp2<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> f2<span class="sy4">;</span> <span class="br0">&#125;</span> <span class="co1">// conversion function to pointer to function</span>
<span class="br0">&#125;</span> a<span class="sy4">;</span>
<span class="kw4">int</span> i <span class="sy1">=</span> a<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// calls f1 via pointer returned from conversion function</span></pre></div></div>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=5" title="Edit section: Call to an overloaded operator">edit</a>]</span> <span class="mw-headline" id="Call_to_an_overloaded_operator">Call to an overloaded operator</span></h4>
<p>If at least one of the arguments to an operator in an expression has a class type or an enumeration type, both <a href="expressions.html#Operators" title="cpp/language/expressions">builtin operators</a> and <a href="operators.html" title="cpp/language/operators">user-defined operator overloads</a> participate in overload resolution, with the set of candidate functions selected as follows:
</p><p>For a unary operator <code><b>@</b></code> whose argument has type <code>T1</code> (after removing cv-qualifications), or binary operator <code><b>@</b></code> whose left operand has type <code>T1</code> and right operand of type <code>T2</code> (after removing cv-qualifications), three sets of candidate functions are prepared:
</p>
<div class="t-li1"><span class="t-li">1)</span> <i>member candidates</i>: if <code>T1</code> is a complete class or a class currently being defined, the set of member candidates is the result of <a href="lookup.html" title="cpp/language/lookup">qualified name lookup</a> of <code>T1::operator@</code>. In all other cases, the set of member candidates is empty. </div>
<div class="t-li1"><span class="t-li">2)</span> <i>non-member candidates</i>: For the operators where <a href="operators.html" title="cpp/language/operators">operator overloading</a> permits non-member forms, all declarations found by <a href="lookup.html" title="cpp/language/lookup">unqualified name lookup</a> of <code>operator@</code> in the context of the expression (which may involve <a href="adl.html" title="cpp/language/adl">ADL</a>), except that member function declarations are ignored and do not prevent the lookup from continuing into the next enclosing scope. If both operands of a binary operator or the only operand of a unary operator has enumeration type, the only functions from the lookup set that become non-member candidates are the ones whose parameter has that enumeration type (or reference to that enumeration type)</div>
<div class="t-li1"><span class="t-li">3)</span> <i>built-in candidates</i>: For <span class="t-c"><span class="mw-geshi cpp source-cpp">operator,</span></span>, the unary <span class="t-c"><span class="mw-geshi cpp source-cpp">operator<span class="sy3">&amp;</span></span></span>, and the <span class="t-c"><span class="mw-geshi cpp source-cpp">operator<span class="sy2">-</span><span class="sy1">&gt;</span></span></span>, the set of built-in candidates is empty. For other operators built-in candidates are the ones listed in <a href="expressions.html#Operators" title="cpp/language/expressions">built-in operator pages</a> as long as all operands can be implicitly converted to their parameters. If any built-in candidate has the same parameter list as a non-member candidate that isn't a function template specialization, it is not added to the list of built-in candidates. When the built-in assignment operators are considered, the conversions from their left-hand arguments are restricted: user-defined conversions and conversions that involve a temporary are not considered.</div>
<p>The set of candidate functions to be submitted for overload resolution is a union of the three sets above. The argument list for the purpose of overload resolution consists of the operands of the operator except for <code>operator-&gt;</code>, where the second operand is not an argument for the function call (see <a href="operator_member_access.html#Built-in_member_access_operators" title="cpp/language/operator member access">member access operator</a>). 
</p>
<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">&#123;</span>
    operator <span class="kw4">int</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// user-defined conversion</span>
<span class="br0">&#125;</span><span class="sy4">;</span>
A operator<span class="sy2">+</span><span class="br0">&#40;</span><span class="kw4">const</span> A<span class="sy3">&amp;</span>, <span class="kw4">const</span> A<span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// non-member user-defined operator</span>
<span class="kw4">void</span> m<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
    A a, b<span class="sy4">;</span>
    a <span class="sy2">+</span> b<span class="sy4">;</span> <span class="co1">// member-candidates: none</span>
           <span class="co1">// non-member candidates: operator+(a,b)</span>
           <span class="co1">// built-in candidates: int(a) + int(b)</span>
           <span class="co1">// overload resolution chooses operator+(a,b)</span>
<span class="br0">&#125;</span></pre></div></div>
<p>If the overload resolution selects a built-in candidate, the <a href="implicit_cast.html" title="cpp/language/implicit cast">user-defined conversion sequence</a> from an operand of class type is not allowed to have a second standard conversion sequence: the user-defined conversion function must give the expected operand type directly:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> Y <span class="br0">&#123;</span> operator <span class="kw4">int</span><span class="sy2">*</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="br0">&#125;</span><span class="sy4">;</span>  <span class="co1">// Y is convertible to int*</span>
<span class="kw4">int</span> <span class="sy2">*</span>a <span class="sy1">=</span> Y<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy2">+</span> <span class="nu16">100.0</span><span class="sy4">;</span> <span class="co1">// error: no operator+ between pointer and double</span></pre></div></div>
<p>For <span class="t-c"><span class="mw-geshi cpp source-cpp">operator,</span></span>, the unary <span class="t-c"><span class="mw-geshi cpp source-cpp">operator<span class="sy3">&amp;</span></span></span>, and <span class="t-c"><span class="mw-geshi cpp source-cpp">operator<span class="sy2">-</span><span class="sy1">&gt;</span></span></span>, if there are no viable functions (see below) in the set of candidate functions, then the operator is reinterpreted as a built-in.
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=6" title="Edit section: Initialization by constructor">edit</a>]</span> <span class="mw-headline" id="Initialization_by_constructor">Initialization by constructor</span></h4>
<p>When an object of class type is <a href="direct_initialization.html" title="cpp/language/direct initialization">direct-initialized</a> or <a href="default_initialization.html" title="cpp/language/default initialization">default-initialized</a>, the candidate functions are all constructors of the class being initialized. The argument list is the expression list of the initializer.
</p><p>When an object of class type is <a href="copy_initialization.html" title="cpp/language/copy initialization">copy-initialized</a> from an object of the same or derived class type, the candidate functions are all <a href="converting_constructor.html" title="cpp/language/converting constructor">converting constructors</a> of the class being initialized. The argument list is the expression of the initializer.
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=7" title="Edit section: Copy-initialization by conversion">edit</a>]</span> <span class="mw-headline" id="Copy-initialization_by_conversion">Copy-initialization by conversion</span></h4>
<p>If <a href="copy_initialization.html" title="cpp/language/copy initialization">copy-initialization</a> of an object of class type requires that a <a href="cast_operator.html" title="cpp/language/cast operator">user-defined conversion function</a> is called to convert the initializer expression of type cv <code>S</code> to the type cv <code>T</code> of the object being initialized, the following functions are candidate functions:
</p>
<ul><li> all <a href="converting_constructor.html" title="cpp/language/converting constructor">converting constructors</a> of <code>T</code>
</li><li> the non-explicit conversion functions from <code>S</code> and its base classes (unless hidden) to <code>T</code> or class derived from <code>T</code> or a reference to such. If this copy-initialization is part of the direct-initialization sequence of <i>cv</i> <code>T</code> (initializing a reference to be bound to the first parameter of a constructor that takes a reference to cv <code>T</code>), then explicit conversion functions are also considered.
</li></ul>
<p>Either way, the argument list for the purpose of overload resolution consists of a single argument which is the initializer expression, which will be compared against the first argument of the constructor or against the implicit object argument of the conversion function.
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=8" title="Edit section: Non-class initialization by conversion">edit</a>]</span> <span class="mw-headline" id="Non-class_initialization_by_conversion">Non-class initialization by conversion</span></h4>
<p>When initialization of an object of non-class type cv1 <code>T</code> requires a <a href="cast_operator.html" title="cpp/language/cast operator">user-defined conversion function</a> to convert from an initializer expression of class type cv <code>S</code>, the following functions are candidates:
</p>
<ul><li> the non-explicit user-defined conversion functions of <code>S</code> and its base classes (unless hidden) that produce type <code>T</code> or a type convertible to <code>T</code> by a <a href="implicit_cast.html" title="cpp/language/implicit cast">standard conversion sequence</a>, or a reference to such type. cv qualifiers on the returned type are ignored for the purpose of selecting candidate functions.
</li><li> if this is <a href="direct_initialization.html" title="cpp/language/direct initialization">direct-initialization</a>, the explicit user-defined conversion functions of <code>S</code> and its base classes (unless hidden) that produce type <code>T</code> or a type convertible to <code>T</code> by a <a href="implicit_cast.html" title="cpp/language/implicit cast">qualification conversion</a>, , or a reference to such type, are also considered.
</li></ul>
<p>Either way, the argument list for the purpose of overload resolution consists of a single argument which is the initializer expression, which will be compared against the implicit object argument of the conversion function.
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=9" title="Edit section: Reference initialization by conversion">edit</a>]</span> <span class="mw-headline" id="Reference_initialization_by_conversion">Reference initialization by conversion</span></h4>
<p>During <a href="reference_initialization.html" title="cpp/language/reference initialization">reference initialization</a>, where the reference to cv1 <code>T</code> is bound to the glvalue or class prvalue result of a conversion from the initializer expression from the class type cv2 <code>S</code>, the following functions are selected for the candidate set:
</p>
<ul><li> the non-explicit user-defined conversion functions of <code>S</code> and its base classes (unless hidden) to the type
</li></ul>
<dl><dd><ul><li> (when initializing lvalue reference or rvalue reference to function) lvalue reference to cv2 <code>T2</code>
</li><li> (when initializing rvalue reference or lvalue reference to function) cv2 <code>T2</code> or rvalue reference to cv2 <code>T2</code>
</li></ul>
</dd><dd>where cv2 T2 is reference-compatible with cv1 T
</dd></dl>
<ul><li> for direct initializaton, the explicit user-defined conversion functions are also considered if T2 is the same type as T or can be converted to type T with a qualification conversion.
</li></ul>
<p>Either way, the argument list for the purpose of overload resolution consists of a single argument which is the initializer expression, which will be compared against the implicit object argument of the conversion function.
</p>
<h4><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=10" title="Edit section: List-initialization">edit</a>]</span> <span class="mw-headline" id="List-initialization">List-initialization</span></h4>
<p>When an object of non-aggregate class type <code>T</code> is <a href="list_initialization.html" title="cpp/language/list initialization">list-initialized</a>, two-phase overload resolution takes place.
</p>
<ul><li> at phase 1, the candidate functions are all initializer-list constructors of <code>T</code> and the argument list for the purpose of overload resolution consists of a single initializer list argument
</li><li> if overload resolution fails at phase 1, phase 2 is entered, where the candidate functions are all constructors of <code>T</code> and the argument list for the purpose of overload resolution consists of the individual elements of the initializer list. 
</li></ul>
<p>If the initializer list is empty and <code>T</code> has a default constructor, phase 1 is skipped.
</p><p>In copy-list-initialization, if phase 2 selects an explicit constructor, the initialization is ill-formed (as opposed to all over copy-initializations where explicit constructors are not even considered).
</p>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=11" title="Edit section: Viable functions">edit</a>]</span> <span class="mw-headline" id="Viable_functions">Viable functions</span></h3>
<p>Given the set of candidate functions, constructed as described above, the next step of overload resolution is examining arguments and parameters to reduce the set to the set of <i>viable functions</i>
</p><p>To be included in the set of viable functions, the candidate function must satisfy the following:
</p>
<div class="t-li1"><span class="t-li">1)</span> If there are <code>M</code> arguments, the candidate function that has exactly <code>M</code> parameters is viable</div>
<div class="t-li1"><span class="t-li">2)</span> If the candidate function has less than <code>M</code> parameters, but has an <a href="variadic_arguments.html" title="cpp/language/variadic arguments">ellipsis parameter</a>, it is viable.</div>
<div class="t-li1"><span class="t-li">3)</span> If the candidate function has more than <code>M</code> parameters and the <code>M+1</code>'st parameter and all parameters that follow must have default arguments, it is viable. For the rest of overload resolution, the parameter list is truncated at M.</div>
<div class="t-li1"><span class="t-li">4)</span> For every argument there must be at least one implicit conversion sequence that converts it to the corresponding parameter.</div>
<div class="t-li1"><span class="t-li">5)</span> If any parameter has reference type, reference binding is accounted for at this step: if an rvalue argument corresponds to non-const lvalue reference parameter or an lvalue argument corresponds to rvalue reference parameter, the function is not viable.</div>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=12" title="Edit section: Best viable function">edit</a>]</span> <span class="mw-headline" id="Best_viable_function">Best viable function</span></h3>
<p>For each pair of viable function <code>F1</code> and <code>F2</code>, the implicit conversion sequences from the <code>i</code>-th parameter to <code>i</code>-th argument are ranked to determine which one is better (except the first argument, the <i>implicit object argument</i> for static member functions has no effect on the ranking)
</p><p><code>F1</code> is determined to be a better function than <code>F2</code> if implicit conversions for all arguments of F1 are <i>not worse</i> than the implicit conversions for all arguments of F2, and
</p>
<div class="t-li1"><span class="t-li">1)</span> there is at least one argument of F1 whose implicit conversion is <i>better</i> than the corresponding implicit conversion for that argument of F2</div>
<div class="t-li1"><span class="t-li">2)</span> or. if not that, (only in context of non-class initialization by conversion), the standard conversion sequence from the return type of F1 to the type being initialized is <i>better</i> than the standard conversion sequence from the return type of F2</div>
<div class="t-li1"><span class="t-li">3)</span> or, if not that, F1 is a non-template function while F2 is a template specialization</div>
<div class="t-li1"><span class="t-li">4)</span> or, if not that, F1 and F2 are both template specializations and F1 is <i>more specialized</i> according to the <a href="function_template.html#Function_template_overloading" title="cpp/language/function template">partial ordering rules for template specializations</a></div>
<p>These pair-wise comparisons are applied to all viable functions. If exactly one viable function is better than all others, overload resolution succeeds and this function is called. Otherwise, compilation fails.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> Fcn<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">int</span><span class="sy2">*</span>, <span class="kw4">short</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// overload #1</span>
<span class="kw4">void</span> Fcn<span class="br0">&#40;</span><span class="kw4">int</span><span class="sy2">*</span>, <span class="kw4">int</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// overload #2</span>
<span class="kw4">int</span> i<span class="sy4">;</span>
<span class="kw4">short</span> s <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
<span class="kw4">void</span> f<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    Fcn<span class="br0">&#40;</span><span class="sy3">&amp;</span>i, <span class="nu0">1L</span><span class="br0">&#41;</span><span class="sy4">;</span>  <span class="co1">// 1st argument: &amp;i -&gt; int* is better than &amp;i -&gt; const int*</span>
                  <span class="co1">// 2nd argument: 1L -&gt; short and 1L -&gt; int are equivalent</span>
                  <span class="co1">// calls Fcn(int*, int)</span>
&#160;
    Fcn<span class="br0">&#40;</span><span class="sy3">&amp;</span>i,<span class="st0">'c'</span><span class="br0">&#41;</span><span class="sy4">;</span>  <span class="co1">// 1st argument: &amp;i -&gt; int* is better than &amp;i -&gt; const int*</span>
                  <span class="co1">// 2nd argument: 'c' -&gt; int is better than 'c' -&gt; short</span>
                  <span class="co1">// calls Fcn(int*, int)</span>
&#160;
    Fcn<span class="br0">&#40;</span><span class="sy3">&amp;</span>i, s<span class="br0">&#41;</span><span class="sy4">;</span>   <span class="co1">// 1st argument: &amp;i -&gt; int* is better than &amp;i -&gt; const int*</span>
                  <span class="co1">// 2nd argument: s -&gt; short is better than s -&gt; int</span>
                  <span class="co1">// no winner, compilation error</span>
<span class="br0">&#125;</span></pre></div></div>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=13" title="Edit section: Ranking of implicit conversion sequences">edit</a>]</span> <span class="mw-headline" id="Ranking_of_implicit_conversion_sequences">Ranking of implicit conversion sequences</span></h3>
<p>The argument-parameter implicit conversion sequences considered by overload resolution correspond to <a href="implicit_cast.html" title="cpp/language/implicit cast">implicit conversions</a> used in <a href="copy_initialization.html" title="cpp/language/copy initialization">copy initialization</a> (for non-reference parameters), except that when considering conversion to the implicit object parameter or to the left-hand side of assignment operator, conversions that create temporary objects are not considered.
</p><p>Each <a href="implicit_cast.html" title="cpp/language/implicit cast">type of standard conversion sequence</a> is assigned one of three ranks:
</p>
<div class="t-li1"><span class="t-li">1)</span> <b>Exact match</b>: no conversion required, lvalue-to-rvalue conversion, qualification conversion, <span class="t-rev-inl t-since-cxx17"><span>function pointer conversion,</span> <span><span class="t-mark-rev t-since-cxx17">(since C++17)</span></span></span> user-defined conversion of class type to the same class</div>
<div class="t-li1"><span class="t-li">2)</span> <b>Promotion</b>: integral promotion, floating-point promotion</div>
<div class="t-li1"><span class="t-li">3)</span> <b>Conversion</b>: integral conversion, floating-point conversion, floating-integral conversion, pointer conversion, pointer-to-member conversion, boolean conversion, user-defined conversion of a derived class to its base</div>
<p>The rank of the standard conversion sequence is the worst of the ranks of the standard conversions it holds (there may be up to <a href="implicit_cast.html" title="cpp/language/implicit cast">three conversions</a>)
</p><p>Binding of a reference parameter directly to the argument expression is either Identity or a derived-to-base Conversion:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> Base <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy4">;</span>
<span class="kw1">struct</span> Derived <span class="sy4">:</span> Base <span class="br0">&#123;</span><span class="br0">&#125;</span> d<span class="sy4">;</span>
<span class="kw4">int</span> f<span class="br0">&#40;</span>Base<span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span>    <span class="co1">// overload #1</span>
<span class="kw4">int</span> f<span class="br0">&#40;</span>Derived<span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// overload #2</span>
<span class="kw4">int</span> i <span class="sy1">=</span> f<span class="br0">&#40;</span>d<span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// d -&gt; Derived&amp; has rank Exact Match</span>
              <span class="co1">// d -&gt; Base&amp; has rank Conversion</span>
              <span class="co1">// calls f(Derived&amp;)</span></pre></div></div>
<p>Since ranking of conversion sequences operates with types and value categories only, a <a href="bit_field.html" title="cpp/language/bit field">bit field</a> can bind to a reference argument for the purpose of ranking, but if that function gets selected, it will be ill-formed.
</p>
<div class="t-li1"><span class="t-li">1)</span> A standard conversion sequence is always <i>better</i> than a user-defined conversion sequence or an ellipsis conversion sequence.</div>
<div class="t-li1"><span class="t-li">2)</span> A user-defined conversion sequence is always <i>better</i> than an <a href="variadic_arguments.html" title="cpp/language/variadic arguments">ellipsis conversion</a> sequence</div>
<div class="t-li1"><span class="t-li">3)</span> A standard conversion sequence <code>S1</code> is <i>better</i> than a standard conversion sequence <code>S2</code> if</div>
<div class="t-li2"><span class="t-li">a)</span> <code>S1</code> is a subsequence of <code>S2</code>, excluding lvalue transformations. The identity conversion sequence is considered a subsequence of any other conversion</div>
<div class="t-li2"><span class="t-li">b)</span> Or, if not that, the rank of <code>S1</code> is better thank the rank of <code>S2</code></div>
<div class="t-li2"><span class="t-li">c)</span> or, if not that, both <code>S1</code> and <code>S2</code> are binding to a reference parameter to something other than the implicit object parameter of a ref-qualified member function, and <code>S1</code> binds an rvalue reference to an rvalue while <code>S2</code> binds an rvalue reference to an rvalue
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> i<span class="sy4">;</span>
<span class="kw4">int</span> f1<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
<span class="kw4">int</span> g<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">int</span><span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span>  <span class="co1">// overload #1</span>
<span class="kw4">int</span> g<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">int</span><span class="sy3">&amp;&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// overload #2</span>
<span class="kw4">int</span> j <span class="sy1">=</span> g<span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy4">;</span>    <span class="co1">// lvalue int -&gt; const int&amp; is the only valid conversion</span>
<span class="kw4">int</span> k <span class="sy1">=</span> g<span class="br0">&#40;</span>f1<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// rvalue int -&gt; const int&amp;&amp; better than rvalue int -&gt; const int&amp;</span></pre></div></div></div>
<div class="t-li2"><span class="t-li">d)</span> or, if not that, both <code>S1</code> and <code>S2</code> are binding to a reference parameter and <code>S1</code> binds an lvalue reference to function while <code>S2</code> binds an rvalue reference to function.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> f<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#40;</span><span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy4">;</span>  <span class="co1">// overload #1</span>
<span class="kw4">int</span> f<span class="br0">&#40;</span><span class="kw4">void</span><span class="br0">&#40;</span><span class="sy3">&amp;&amp;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// overload #2</span>
<span class="kw4">void</span> g<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
<span class="kw4">int</span> i1 <span class="sy1">=</span> f<span class="br0">&#40;</span>g<span class="br0">&#41;</span><span class="sy4">;</span>     <span class="co1">// calls #1</span></pre></div></div></div>
<div class="t-li2"><span class="t-li">e)</span> or, if not that, both <code>S1</code> and <code>S2</code> are binding to a reference parameters only different in top-level cv-qualification, and <code>S1</code>'s type is <i>less</i> cv-qualified than <code>S2</code>'s.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> f<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">int</span> <span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// overload #1</span>
<span class="kw4">int</span> f<span class="br0">&#40;</span><span class="kw4">int</span> <span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span>       <span class="co1">// overload #2 (both references)</span>
<span class="kw4">int</span> g<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">int</span> <span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// overload #1</span>
<span class="kw4">int</span> g<span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span><span class="sy4">;</span>         <span class="co1">// overload #2</span>
<span class="kw4">int</span> i<span class="sy4">;</span>
<span class="kw4">int</span> j <span class="sy1">=</span> f<span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// lvalue i -&gt; int&amp; is better than lvalue int -&gt; const int&amp;</span>
              <span class="co1">// calls f(int&amp;)</span>
<span class="kw4">int</span> k <span class="sy1">=</span> g<span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// lvalue i -&gt; const int&amp; ranks Exact Match</span>
              <span class="co1">// lvalue i -&gt; rvalue int ranks Exact Match</span>
              <span class="co1">// ambiguous overload: compilation error</span></pre></div></div></div>
<div class="t-li2"><span class="t-li">f)</span> Or, if not that, S1 and S2 only differ in qualification conversion, and the cv-qualification of the result of <code>S1</code> is a subset of the cv-qualification of the result of <code>S2</code>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> f<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">int</span><span class="sy2">*</span><span class="br0">&#41;</span><span class="sy4">;</span>
<span class="kw4">int</span> f<span class="br0">&#40;</span><span class="kw4">int</span><span class="sy2">*</span><span class="br0">&#41;</span><span class="sy4">;</span>
<span class="kw4">int</span> i<span class="sy4">;</span>
<span class="kw4">int</span> j <span class="sy1">=</span> f<span class="br0">&#40;</span><span class="sy3">&amp;</span>i<span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// &amp;i -&gt; int* is better than &amp;i -&gt; const int*, calls f(int*)</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">4)</span> A user-defined conversion sequence <code>U1</code> is <i>better</i> than a user-defined conversion sequence <code>U2</code> if they call the same constructor/user-defined conversion function or initialize the same class with aggregate-initialization, and in either case the second standard conversion sequence in <code>U1</code> is better than the second standard conversion sequence in <code>U2</code>
<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">&#123;</span>
    operator <span class="kw4">short</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// user-defined conversion function</span>
<span class="br0">&#125;</span> a<span class="sy4">;</span>
<span class="kw4">int</span> f<span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span><span class="sy4">;</span>   <span class="co1">// overload #1</span>
<span class="kw4">int</span> f<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// overload #2</span>
<span class="kw4">int</span> i <span class="sy1">=</span> f<span class="br0">&#40;</span>a<span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// A -&gt; short, followed by short -&gt; int (rank Promotion)</span>
              <span class="co1">// A -&gt; short, followed by short -&gt; float (rank Conversion)</span>
              <span class="co1">// calls f(int)</span></pre></div></div></div>
<div class="t-li1"><span class="t-li">5)</span> A list-initialization sequence <code>L1</code> is <i>better</i> than list-initialization sequence <code>L2</code> if <code>L1</code> initializes an <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></span> parameter, while <code>L2</code> does not.
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f1<span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span><span class="sy4">;</span>                                 <span class="co1">// #1</span>
<span class="kw4">void</span> f1<span class="br0">&#40;</span><a href="../utility/initializer_list.html"><span class="kw838">std::<span class="me2">initializer_list</span></span></a><span class="sy1">&lt;</span><span class="kw4">long</span><span class="sy1">&gt;</span><span class="br0">&#41;</span><span class="sy4">;</span>         <span class="co1">// #2</span>
<span class="kw4">void</span> g1<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> f1<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="nu0">42</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="br0">&#125;</span>                       <span class="co1">// chooses #2</span>
&#160;
<span class="kw4">void</span> f2<span class="br0">&#40;</span><a href="../utility/pair.html"><span class="kw936">std::<span class="me2">pair</span></span></a><span class="sy1">&lt;</span><span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span>, <span class="kw4">const</span> <span class="kw4">char</span><span class="sy2">*</span><span class="sy1">&gt;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// #3</span>
<span class="kw4">void</span> f2<span class="br0">&#40;</span><a href="../utility/initializer_list.html"><span class="kw838">std::<span class="me2">initializer_list</span></span></a><span class="sy1">&lt;</span><a href="../string/basic_string.html"><span class="kw1072">std::<span class="me2">string</span></span></a><span class="sy1">&gt;</span><span class="br0">&#41;</span><span class="sy4">;</span>  <span class="co1">// #4</span>
<span class="kw4">void</span> g2<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> f2<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="st0">&quot;foo&quot;</span>,<span class="st0">&quot;bar&quot;</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="br0">&#125;</span>              <span class="co1">// chooses #4</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">6)</span> A list-initialization sequence <code>L1</code> is <i>better</i> than list-initialization sequence <code>L2</code> if the corresponding parameters are references to arrays, and L1 converts to type "array of N1 T," L2 converts to type "array of N2 T", and N1 is smaller than N2.
</div></td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<p>If two conversion sequences are indistinguishable because they have the same rank, the following additional rules apply:
</p>
<div class="t-li1"><span class="t-li">1)</span> Conversion that involves pointer to bool, pointer-to-member to bool, or <span class="t-lc"><a href="../types/nullptr_t.html" title="cpp/types/nullptr t">std::nullptr_t</a></span> to bool conversion is worse than the one that doesn't</div>
<div class="t-li1"><span class="t-li">2)</span> Conversion that converts pointer-to-derived to pointer-to-base is better than the conversion of pointer-to-derived to pointer-to-void, and conversion of pointer-to-base to void is better than pointer-to-derived to void.</div>
<div class="t-li1"><span class="t-li">3)</span> If <code>Mid</code> is derived (directly or indirectly) from <code>Base</code>, and <code>Derived</code> is derived (directly or indirectly) from <code>Mid</code></div>
<div class="t-li2"><span class="t-li">a)</span> <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived<span class="sy2">*</span></span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Mid<span class="sy2">*</span></span></span> is better than <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived<span class="sy2">*</span></span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy2">*</span></span></span></div>
<div class="t-li2"><span class="t-li">b)</span> <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived</span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Mid<span class="sy3">&amp;</span></span></span> or <span class="t-c"><span class="mw-geshi cpp source-cpp">Mid<span class="sy3">&amp;&amp;</span></span></span> is better than <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived</span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy3">&amp;</span></span></span> or <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy3">&amp;&amp;</span></span></span></div>
<div class="t-li2"><span class="t-li">c)</span> <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy4">::</span><span class="sy2">*</span></span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Mid<span class="sy4">::</span><span class="sy2">*</span></span></span> is better than <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy4">::</span><span class="sy2">*</span></span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived<span class="sy4">::</span><span class="sy2">*</span></span></span></div>
<div class="t-li2"><span class="t-li">d)</span> <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived</span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Mid</span></span> is better than <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived</span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Base</span></span></div>
<div class="t-li2"><span class="t-li">e)</span> <span class="t-c"><span class="mw-geshi cpp source-cpp">Mid<span class="sy2">*</span></span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy2">*</span></span></span> is better than <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived<span class="sy2">*</span></span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy2">*</span></span></span></div>
<div class="t-li2"><span class="t-li">f)</span> <span class="t-c"><span class="mw-geshi cpp source-cpp">Mid</span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy3">&amp;</span></span></span> or <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy3">&amp;&amp;</span></span></span> is better than <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived</span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy3">&amp;</span></span></span> or <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy3">&amp;&amp;</span></span></span></div>
<div class="t-li2"><span class="t-li">g)</span> <span class="t-c"><span class="mw-geshi cpp source-cpp">Mid<span class="sy4">::</span><span class="sy2">*</span></span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived<span class="sy4">::</span><span class="sy2">*</span></span></span> is better than <span class="t-c"><span class="mw-geshi cpp source-cpp">Base<span class="sy4">::</span><span class="sy2">*</span></span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived<span class="sy4">::</span><span class="sy2">*</span></span></span></div>
<div class="t-li2"><span class="t-li">h)</span> <span class="t-c"><span class="mw-geshi cpp source-cpp">Mid</span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Base</span></span> is better than <span class="t-c"><span class="mw-geshi cpp source-cpp">Derived</span></span> to <span class="t-c"><span class="mw-geshi cpp source-cpp">Base</span></span></div>
<p>Ambiguous conversion sequences are ranked as user-defined conversion sequences because multiple conversion sequences for an argument can exist only if they involve different user-defined conversions:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> B<span class="sy4">;</span>
<span class="kw1">class</span> A <span class="br0">&#123;</span> A <span class="br0">&#40;</span>B<span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span><span class="br0">&#125;</span><span class="sy4">;</span> <span class="co1">// converting constructor</span>
<span class="kw1">class</span> B <span class="br0">&#123;</span> operator A <span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="br0">&#125;</span><span class="sy4">;</span> <span class="co1">// user-defined conversion function</span>
<span class="kw1">class</span> C <span class="br0">&#123;</span> C <span class="br0">&#40;</span>B<span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="br0">&#125;</span><span class="sy4">;</span> <span class="co1">// converting constructor</span>
<span class="kw4">void</span> f<span class="br0">&#40;</span>A<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span> <span class="co1">// overload #1</span>
<span class="kw4">void</span> f<span class="br0">&#40;</span>C<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span> <span class="co1">// overload #2</span>
B b<span class="sy4">;</span>
f<span class="br0">&#40;</span>b<span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// B -&gt; A via ctor or B -&gt; A via function (ambiguous conversion)</span>
      <span class="co1">// b -&gt; C via ctor (user-defined conversion)</span>
      <span class="co1">// the conversions for overload #1 and for overload #2</span>
      <span class="co1">// are indistinguishable; compilation fails</span></pre></div></div>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=14" title="Edit section: Implicit conversion sequence in list-initialization">edit</a>]</span> <span class="mw-headline" id="Implicit_conversion_sequence_in_list-initialization">Implicit conversion sequence in list-initialization</span></h3>
<p>In <a href="list_initialization.html" title="cpp/language/list initialization">list initialization</a>, the argument is a <span class="t-spar">braced-init-list</span>, which isn't an expression, so the implicit conversion sequence into the parameter type for the purpose of overload resolution is decided by the following special rules:
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td> 
<ul><li> If the parameter type is some class <code>X</code> and the initializer list consists of exactly one element of same or derived class (possibly cv-qualified), the implicit conversion sequence is the one required to convert the element to the parameter type.
</li><li> Otherwise, if the parameter type is a reference to character array and the initializer list has a single element that is an appropriately-typed string literal, the implicit conversion sequence is the identity conversion.
</li></ul>
</td>
<td><span class="t-mark-rev t-since-cxx14">(since C++14)</span></td></tr>
</table>
<ul><li> If the parameter type is <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">&lt;</span>X<span class="sy1">&gt;</span></span></span>, and there is an non-narrowing implicit conversion from every element of the initializer list to <code>X</code>, the implicit conversion sequence for the purpose of overload resolution is the worst conversion necessary. If the braced-init-list is empty, the conversion sequence is the identity conversion.
</li></ul>
<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">&#123;</span>
    A<span class="br0">&#40;</span><a href="../utility/initializer_list.html"><span class="kw838">std::<span class="me2">initializer_list</span></span></a><span class="sy1">&lt;</span><span class="kw4">double</span><span class="sy1">&gt;</span><span class="br0">&#41;</span><span class="sy4">;</span>                    <span class="co1">// #1</span>
    A<span class="br0">&#40;</span><a href="../utility/initializer_list.html"><span class="kw838">std::<span class="me2">initializer_list</span></span></a><span class="sy1">&lt;</span>complex<span class="sy1">&lt;</span><span class="kw4">double</span><span class="sy1">&gt;&gt;</span><span class="br0">&#41;</span><span class="sy4">;</span>           <span class="co1">// #2</span>
    A<span class="br0">&#40;</span><a href="../utility/initializer_list.html"><span class="kw838">std::<span class="me2">initializer_list</span></span></a><span class="sy1">&lt;</span><a href="../string/basic_string.html"><span class="kw1072">std::<span class="me2">string</span></span></a><span class="sy1">&gt;</span><span class="br0">&#41;</span><span class="sy4">;</span>               <span class="co1">// #3</span>
<span class="br0">&#125;</span><span class="sy4">;</span>
A a<span class="br0">&#123;</span><span class="nu16">1.0</span>,<span class="nu16">2.0</span><span class="br0">&#125;</span><span class="sy4">;</span> <span class="co1">// selects #1 (rvalue double -&gt; double: identity conv)</span>
<span class="kw4">void</span> g<span class="br0">&#40;</span>A<span class="br0">&#41;</span><span class="sy4">;</span>
g<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="st0">&quot;foo&quot;</span>,<span class="st0">&quot;bar&quot;</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="co1">// selects #3 (lvalue const char[4] -&gt; std::string: user-def conv)</span></pre></div></div>
<ul><li> Otherwise, if the parameter type is "array of N T" (this only happens for references to arrays), <span class="t-rev-inl t-since-cxx14"><span>the initializer list must have N or less elements, and</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> the worst implicit conversion necessary to convert every element of the list <span class="t-rev-inl t-since-cxx14"><span>(or the empty pair of braces <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="br0">&#123;</span><span class="br0">&#125;</span></span></span> if the list is shorter than N)</span> <span><span class="t-mark-rev t-since-cxx14">(since C++14)</span></span></span> to <code>T</code> is the one used.
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">typedef</span> <span class="kw4">int</span> IA<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span><span class="sy4">;</span>
<span class="kw4">void</span> h<span class="br0">&#40;</span><span class="kw4">const</span> IA<span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span>
h<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy4">;</span>        <span class="co1">// int-&gt;int identity conversion</span></pre></div></div>
<ul><li> Otherwise, if the parameter type is a non-aggregate class type <code>X</code>, overload resolution picks the constructor of X to initialize from the argument initializer list, and then the implicit conversion sequence is a user-defined conversion sequence with the second standard conversion sequence an identity conversion. If multiple constructors are viable but none is better than the others, the implicit conversion sequence is the ambiguous conversion sequence.
</li></ul>
<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">&#123;</span> A<span class="br0">&#40;</span><a href="../utility/initializer_list.html"><span class="kw838">std::<span class="me2">initializer_list</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="br0">&#125;</span><span class="sy4">;</span>
<span class="kw4">void</span> f<span class="br0">&#40;</span>A<span class="br0">&#41;</span><span class="sy4">;</span>
<span class="kw1">struct</span> B <span class="br0">&#123;</span> B<span class="br0">&#40;</span><span class="kw4">int</span>, <span class="kw4">double</span><span class="br0">&#41;</span><span class="sy4">;</span> <span class="br0">&#125;</span><span class="sy4">;</span>
<span class="kw4">void</span> g<span class="br0">&#40;</span>B<span class="br0">&#41;</span><span class="sy4">;</span>
g<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="st0">'a'</span>,<span class="st0">'b'</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy4">;</span>  <span class="co1">// calls g(B(int,double)), user-defined conversion</span>
<span class="co1">// g({1.0, 1,0}); // error: double-&gt;int is narrowing, not allowed in list-init</span>
<span class="kw4">void</span> f<span class="br0">&#40;</span>B<span class="br0">&#41;</span><span class="sy4">;</span>
<span class="co1">// f({'a','b'}); // f(A) and f(B) both user-defined conversions</span></pre></div></div>
<ul><li> Otherwise, if the parameter type is an aggregate which can be initialized from the initializer list according by <a href="aggregate_initialization.html" title="cpp/language/aggregate initialization">aggregate initialization</a>, the implicit conversion sequence is a user-defined conversion sequence with the second standard conversion sequence an identity conversion.
</li></ul>
<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">&#123;</span> <span class="kw4">int</span> m1<span class="sy4">;</span> <span class="kw4">double</span> m2<span class="sy4">;</span><span class="br0">&#125;</span><span class="sy4">;</span>
<span class="kw4">void</span> f<span class="br0">&#40;</span>A<span class="br0">&#41;</span><span class="sy4">;</span>
f<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="st0">'a'</span>,<span class="st0">'b'</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy4">;</span>  <span class="co1">// calls f(A(int,double)), user-defined conversion</span></pre></div></div>
<ul><li> Otherwise, if the parameter is a reference, reference initialization rules apply
</li></ul>
<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">&#123;</span> <span class="kw4">int</span> m1<span class="sy4">;</span> <span class="kw4">double</span> m2<span class="sy4">;</span> <span class="br0">&#125;</span><span class="sy4">;</span>
<span class="kw4">void</span> f<span class="br0">&#40;</span><span class="kw4">const</span> A<span class="sy3">&amp;</span><span class="br0">&#41;</span><span class="sy4">;</span>
f<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="st0">'a'</span>,<span class="st0">'b'</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy4">;</span>  <span class="co1">// temporary created, f(A(int,double)) called. User-defined conversion</span></pre></div></div>
<ul><li> Otherwise, if the parameter type is not a class and the initializer list has one element, the implicit conversion sequence is the one required to convert the element to the parameter type
</li><li> Otherwise, if the parameter type is not a class type and if the initializer list has no elements, the implicit conversion sequence is the identity conversion.
</li></ul>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;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#1467">CWG 1467</a>
</td>
<td> C++14
</td>
<td> same-type list-initialization of aggregates and arrays was omitted
</td>
<td> initializaton defined
</td></tr></table>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=16" title="Edit section: See also">edit</a>]</span> <span class="mw-headline" id="See_also">See also</span></h3>
<ul><li> <a href="lookup.html" title="cpp/language/lookup">Name lookup</a> 
</li><li> <a href="adl.html" title="cpp/language/adl">Argument-dependent lookup</a> 
</li><li> <a href="template_argument_deduction.html" title="cpp/language/template argument deduction">Template argument deduction</a>
</li><li> <a href="sfinae.html" title="cpp/language/sfinae">SFINAE</a>
</li></ul>
<h3><span class="editsection">[<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;action=edit&amp;section=17" title="Edit section: References">edit</a>]</span> <span class="mw-headline" id="References">References</span></h3>
<div class="t-ref-std-c++14">
<ul><li> C++14 standard (ISO/IEC 14882:2014): 
</li></ul>
<dl><dd><ul><li> 13.3 Overload resolution &#91;over.match&#93;
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-c++11">
<ul><li> C++11 standard (ISO/IEC 14882:2011): 
</li></ul>
<dl><dd><ul><li> 13.3 Overload resolution &#91;over.match&#93;
</li></ul>
</dd></dl>
</div>

<!-- 
NewPP limit report
Preprocessor visited node count: 3581/1000000
Preprocessor generated node count: 7364/1000000
Post‐expand include size: 42301/2097152 bytes
Template argument size: 12904/2097152 bytes
Highest expansion depth: 13/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_en_:pcache:idhash:12784-0!*!0!!en!*!* and timestamp 20151118165942 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    Retrieved from "<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;oldid=81906">http://en.cppreference.com/mwiki/index.php?title=cpp/language/overload_resolution&amp;oldid=81906</a>"                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <div id='catlinks' class='catlinks catlinks-allhidden'></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="overload_resolution.html#"></a></h5>
            <ul>
<li id="t-whatlinkshere"><a href="http://en.cppreference.com/w/Special:WhatLinksHere/cpp/language/overload_resolution" 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/overload_resolution" 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/overload_resolution&amp;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/overload_resolution&amp;oldid=81906" 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/overload_resolution&amp;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:59.</li>
                                    <li id="footer-info-viewcount">This page has been accessed 44,346 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&amp;lang=en&amp;modules=skins.cppreference2&amp;only=scripts&amp;skin=cppreference2&amp;*"></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&amp;lang=en&amp;modules=site&amp;only=scripts&amp;skin=cppreference2&amp;*"></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.103 secs. -->
	</body>
<!-- Cached 20151118170843 -->
</html>