    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Overload Resolution - Selecting the Function</title>
        <link>https://members.accu.org/index.php/journals/268</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #66 - Apr 2005 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c146/">66</a>
                    (7)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c146-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c146+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Overload Resolution - Selecting the Function</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 12 April 2005 18:05:26 +01:00 or Tue, 12 April 2005 18:05:26 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>Overloading is a form of polymorphism, however, the rules are
quite complex in C++. This article tries to explain most of the
rules and clarify concepts like the implicit conversion sequence.
The main aim is to explain how a function is selected from the set
of possibilities. This makes it easier to understand and correct
ambiguities the compilers might complain about.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id="d0e22"></a>Overview of
Overloading Process</h2>
</div>
<p>Declaring two or more items with the same name in a scope is
called <i class="firstterm">overloading</i>. In C++ the items which
can be overloaded are free functions, member functions and
constructors, which are collectively referred to as functions. The
compiler selects which function to use at compile time according to
the argument list, including the object itself in the case of
member functions. The functions that have the same name and are
visible in a specific context are called <i class=
"firstterm">candidates</i>. First the usable functions are selected
from the set of candidates. These usable functions are called
<i class="firstterm">viable functions</i>. A function is viable if
it can be called, that is the parameter count matches the arguments
and an <i>implicit conversion sequence</i> exists
for every argument to the corresponding parameter. A function
having more parameters than there are arguments in an argument list
can also be viable if default arguments exist for all the extra
parameters. In such cases the extra parameters are not considered
for the purpose of overload resolution. Access control is applied
after overload resolution, meaning that if the function selected is
not accessible in the specified context, the program is
ill-formed.</p>
<p><span class="bold"><b>Phases of the function call
process:</b></span></p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Name lookup</p>
</li>
<li>
<p>Overload resolution</p>
</li>
<li>
<p>Access control</p>
</li>
</ol>
</div>
<p>Many different contexts of overloading exist and each has its
own set of rules for finding the set of candidate functions and
arguments. Those rules are not covered here except for a few
important cases which involve a user-defined conversion. After
defining the candidates and the arguments for each context, the
rest of the overload process is identical for all contexts.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e54" id="d0e54"></a>Ordering of
Viable Functions</h2>
</div>
<p>A viable function is better than another viable function if (and
only if) it does not have a worse implicit conversion sequence for
any of its arguments than the other function and has one of the
following properties:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>It has at least one better conversion sequence than the other
function.</p>
</li>
<li>
<p>It is a non-template and the other function is a template
specialisation.</p>
</li>
<li>
<p>Both are templates and it is more specialised than the other
function according to the partial ordering rules.</p>
</li>
</ul>
</div>
<p>The ordering of implicit conversion sequences is explained
later. If only one function is better than other functions in the
set of viable functions then it is called the <i class=
"firstterm">best viable function</i> and is selected by the
overload resolution. Otherwise the call is ill-formed and
diagnostics are reported.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e74" id="d0e74"></a>Member
Functions and Built-in Operators With Overloading</h2>
</div>
<p>For overload resolution, member functions are considered as free
functions with an extra parameter taking the object itself. This is
called the <i class="firstterm">implicit object parameter</i>. The
cv-qualification<sup>[<a name="d0e82" href="#ftn.d0e82" id=
"d0e82">1</a>]</sup> of the implicit parameter is the same as the
cv-qualification of the specified member function. The object is
matched to the implicit object parameter to make the overload
resolution possible. This is an easy way to make the overloading
rules uniform for the member functions and free functions. The
implicit object argument is just like other arguments, except for a
few special rules: the related conversions cannot introduce
temporaries, no user-defined conversions are allowed and an rvalue
can be bound to a non-constant reference. For static member
functions the implicit object parameter is not considered since
there is no object to match it. Also the built-in operators are
considered free functions for the purpose of overload
resolution.</p>
<p><span class="bold"><b>Examples:</b></span></p>
<pre class="programlisting">
struct type {
  void func(int) const;
  void other();
};
</pre>
<p>The member functions are considered as</p>
<pre class="programlisting">
void func(type const&amp;, int);
void other(type&amp;);

char* p;
p[0];
</pre>
<p>The subscript operator is considered as</p>
<pre class="programlisting">
T&amp; operator[](T*, ptrdiff_t);
</pre>
<p>where <tt class="type">T</tt> is a cv-(un)qualified type.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e109" id=
"d0e109"></a>Conversions</h2>
</div>
<p>The implicit conversion sequences are based on single
conversions. These simple implicit conversions provide a great deal
of flexibility and can be helpful if used correctly. Even though
single conversions are quite easy, the interaction between the
sequences of conversions and the overloading is far from simple.
The standard conversions are the built-in conversions, those are
categorised and ranked to form an intuitive order. This is the
basis for ranking the conversion sequences consisting of only
standard conversions. There are three ranks for these conversions
(see Table 1). In addition to those standard conversions, a
derived-to-base conversion exists but only in the description of
implicit conversion sequences. It has a conversion rank.</p>
<p><span class="bold"><b>Examples:</b></span></p>
<div class="literallayout">
<p><tt class="type">char</tt> &#10132; <tt class="type">int</tt>
(integral promotion)<br>
<tt class="type">float</tt> &#10132; <tt class="type">long</tt>
(floating-integral conversion)<br>
type &#10132; type <tt class="literal">const</tt> (qualification
conversion)<br>
type &#10132; type (identity conversion)</p>
</div>
<p>Besides standard conversions there are the user-defined
conversions, meaning conversion functions and converting
constructors. User-defined conversions are applied only if they are
unambiguous. It is good to know that at most one user-defined
conversion is implicitly applied to a single value. Three forms of
conversion sequences can be constructed from these different
conversions:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Standard conversion sequence</p>
</li>
<li>
<p>User-defined conversion sequence</p>
</li>
<li>
<p>Ellipsis conversion sequence</p>
</li>
</ul>
</div>
<div class="table"><a name="d0e145" id="d0e145"></a>
<table summary=
"Standard Conversions (smallest number is highest rank)" border="1"
cellspacing="0">
<tr>
<th>Conversion</th>
<th>Category</th>
<th>Ranking</th>
<th>Rank</th>
</tr>
<tr>
<td>
<div class="literallayout">
<p>No conversions required</p>
</div>
</td>
<td>Identity</td>
<td rowspan="3">Exact match</td>
<td rowspan="3">1</td>
</tr>
<tr>
<td>
<div class="literallayout">
<p>Lvalue-to-rvalue conversion<br>
Array-to-pointer conversion<br>
Function-to-pointer conversion</p>
</div>
</td>
<td>Lvalue transformation</td>
</tr>
<tr>
<td>
<div class="literallayout">
<p>Qualification conversion</p>
</div>
</td>
<td>Qualification adjustment</td>
</tr>
<tr>
<td>
<div class="literallayout">
<p>Integral promotions<br>
Floating point promotions</p>
</div>
</td>
<td>Promotion</td>
<td>Promotion</td>
<td>2</td>
</tr>
<tr>
<td>
<div class="literallayout">
<p>Integral conversions<br>
Floating point conversions<br>
Floating-integral conversions<br>
Pointer conversions<br>
Pointer to member conversions<br>
Boolean conversions</p>
</div>
</td>
<td>Conversion</td>
<td>Conversion</td>
<td>3</td>
</tr>
</table>
<p class="title c2">Table 1. Standard Conversions (smallest number
is highest rank)</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e202" id="d0e202"></a>Standard
Conversion Sequences</h2>
</div>
<p>The standard conversion sequence is either an identity
conversion or consists of one to three standard conversions from
the four categories when identity is not considered, at most one
conversion per category. The standard conversions are always
applied in a certain order: Lvalue transformation, Promotion or
Conversion and Qualification adjustment. The standard conversion
sequence is ranked according to the conversions it contains, the
conversion with the lowest rank dictates the rank of the whole
sequence.</p>
<p><span class="bold"><b>Examples:</b></span></p>
<div class="literallayout">
<p><tt class="type">bool</tt> &#10132; <tt class="type">short</tt>
(conversion rank)<br>
<tt class="type">char</tt> &#10132; <tt class="type">char
const</tt> (exact match rank)<br>
<tt class="type">char</tt> &#10132; <tt class="type">int</tt>
&#10132; <tt class="type">int const</tt> (promotion rank)<br>
<tt class="type">float[]</tt> &#10132; <tt class="type">float*</tt>
&#10132; <tt class="type">float const*</tt> (exact match rank)</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e242" id="d0e242"></a>User-Defined
Conversion Sequences</h2>
</div>
<p>A user-defined conversion sequence is a composition of three
pieces: first an initial standard conversion sequence followed by a
user-defined conversion and then followed by another standard
conversion sequence. In the case when the user-defined conversion
is a conversion function, the first conversion sequence converts
the source type to the implicit object parameter so that the
user-defined conversion can be applied.</p>
<p>On the other hand, if the user-defined conversion is a
constructor, the source type is converted to a type required by the
constructor. After the user-defined conversion is applied, the
second standard conversion sequence converts the result to a
destination type. If the user-defined conversion is a template
conversion function, the second standard conversion sequence is
required to have an exact match rank. A conversion from a type to
the same type is given an exact match rank even though a
user-defined conversion is used. This is natural when passing
parameters by value and hence using a copy constructor.</p>
<p><span class="bold"><b>Examples:</b></span></p>
<pre class="programlisting">
struct A { operator int(); };
long var = A();
</pre>
<p>A &#10132; <tt class="type">int</tt> &#10132; <tt class=
"type">long</tt></p>
<pre class="programlisting">
struct B { B(float); };
void func(B const&amp;);
func(0);
</pre>
<p><tt class="type">int</tt> &#10132; <tt class="type">float</tt>
&#10132; B &#10132; B <tt class="literal">const</tt></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e272" id="d0e272"></a>Ellipsis
Conversion Sequences</h2>
</div>
<p>The last and third form of conversion sequence is an ellipsis
conversion sequence, which happens when matching an argument to an
ellipsis parameter.</p>
<p><span class="bold"><b>Examples:</b></span></p>
<pre class="programlisting">
void func(...);
func(0); // an ellipsis conversion sequence,
         // int matching to an ellipsis
         // parameter.
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e282" id="d0e282"></a>Reference and
Non-Reference Parameters</h2>
</div>
<p>If a parameter type is not a reference, the implicit conversion
sequence models a copy-initialisation. In that case any difference
in top level cv-qualification is not considered as a conversion.
Also the use of a copy constructor is not ranked as a user-defined
conversion but as an exact match and hence is not a conversion.
However, if the parameter is a reference, binding to a reference
occurs. The binding is considered an identity conversion and hence
if the destination type binds directly to the source expression, it
is an exact match. An rvalue can not be bound to a non-const
reference and a candidate requiring such is not viable. If the type
of the argument does not directly bind to the parameter, the
implicit conversion sequence models a copy-initialisation of a
temporary to the underlying type of the reference, similar to the
case of a non-reference.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e287" id="d0e287"></a>Basic
Ordering of Conversion Sequences</h2>
</div>
<p>The implicit conversion sequences for the nth parameters of the
viable functions need to be ordered to select the best viable
function if one exists. The three basic forms of sequences are
ordered so that the standard conversion sequence is better than the
user-defined conversion sequence and the user-defined conversion
sequence is better than the ellipsis conversion sequence. In case
that two conversion sequences cannot be ordered, they are said to
be indistinguishable. This is rather easy and intuitive ordering
but there is a lot more to it.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e292" id="d0e292"></a>Ordering of
Standard Conversion Sequences</h2>
</div>
<p>Standard conversion sequences are ordered by their rank. The
higher the rank, the better the sequence. Another important
ordering is that a proper subsequence of another sequence is better
than the other sequence. The comparison excludes lvalue
transformations. An identity conversion is considered to be a
subsequence of any non-identity conversion sequence. Also there are
other rules that apply with the standard conversion sequences: If
two sequences have the same conversion rank, they are
indistinguishable unless one is a conversion of a pointer to bool
which is a worse conversion than other conversions. In case of
converting a type to its direct or indirect base class, the
conversion to a base class closer in the inheritance hierarchy is a
better conversion than a conversion to a base class that is further
away. The same applies with pointers and references, also with
pointers <tt class="type">void*</tt> is considered to be the
furthest in the hierarchy.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e300" id="d0e300"></a>Ordering of
User-Defined Conversion Sequences</h2>
</div>
<p>User-defined conversion sequences are somewhat more difficult to
order. Constructing a user-defined conversion sequence for a
specific parameter means first using the overload resolution to
select the best user-defined conversion for the sequence. This
works just like ordinary overloading but now the first parameter of
a converting constructor is considered as a destination type and
similarly in the case of a conversion function the implicit object
parameter. In case there is more than one best user-defined
conversion, the second standard conversion sequence is used to
decide which conversion sequence is better than the other. If there
is no best conversion sequence for that specific parameter, the
sequence is an <i class="firstterm">ambiguous conversion
sequence</i>. It is treated as any user-defined conversion sequence
because it always involves a user-defined conversion. The purpose
of an ambiguous conversion sequence is to keep a specific function
viable. Removing the function from the set of viable functions
could cause some other function to become the best viable function
even if it clearly is not. If a function using an ambiguous
conversion sequence is selected as the best viable function, the
call is ill-formed.</p>
<p><span class="bold"><b>Examples:</b></span></p>
<pre class="programlisting">
struct A;
struct B {
  B(A const&amp;);
};

struct A {
  operator B() const;
  operator int() const;
};
void func(B);
void func(int);

func(A());
</pre>
<p>The call is ambiguous, however, the parameter <i class=
"parameter"><tt>B</tt></i> has an ambiguous conversion sequence and
if the function having this parameter was eliminated the call would
not be ambiguous. This is because there would be only one function
to select.</p>
<p>For each argument the implicit conversion sequences are
constructed. After that the sequences are compared and ordered. Two
user-defined conversion sequences are indistinguishable unless they
use the same user-defined conversion in which case the second
standard conversion sequence is conclusive.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e320" id="d0e320"></a>Difficulties
With User-Defined Conversions</h2>
</div>
<p>There are a few oddities with user-defined conversions, mostly
when the destination type is a reference.</p>
<p>One such context is an initialisation by conversion function for
direct reference binding. This means that a conversion function
converting to a type which is reference-compatible with the
destination type exists. In this case the candidates for selecting
the user-defined conversion are only the conversion functions
returning a reference that is compatible with the destination
reference.</p>
<p>Another thing is that in the same context, the second standard
conversion sequence is considered to be an identity conversion if
the result binds directly to the destination, or a derived-to-base
conversion in the case of a base class. This means for example that
there is no ordering for different cv-qualifications. The rules
concerning this might change in future standards to make the rules
consistent and to meet one's expectations.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e329" id="d0e329"></a>Another Way
to Handle User-Defined Conversion Sequences</h2>
</div>
<p>Considering the overload rules for user-defined conversions, it
is easy to notice that the selection of the user-defined conversion
can be combined with the rest of the overload process. This leads
to a few rules:</p>
<p>If the destination parameter is the same for two sequences, the
first standard conversion sequences are used to order these
user-defined conversion sequences.</p>
<p>After that the second standard conversion sequence is used to
select the best conversion sequence.</p>
<p>Of course one has to be careful not to mix those with the
conversion sequences that do not have the same destination.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e340" id="d0e340"></a>Function
Templates With Overloading</h2>
</div>
<p>In most cases a function template behaves just like a normal
function when considering overload resolution. The template
argument deduction is applied, if it succeeds, the function is
added to the candidates set. Such a function is handled like any
other function, except when two viable functions are equally good,
the non-template one is selected. In case both are a specialisation
of a function template, partial ordering rules are applied. The
partial ordering rules are out of the scope of this article.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e345" id=
"d0e345"></a>Conclusion</h2>
</div>
<p>This just about covers all there is to know about conversion
sequences. However there are a lot of subjects to cover which are
related to the subject of this article, to mention a few: finding
candidate sets, overloadable declaration and partial ordering. It
can be somewhat hard to remember all the rules related to the
issue, however, only a subset is normally needed. The basic ideas
are easy enough to remember and those are the ones usually needed
and of course it is always possible to look up the exact rules.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e350" id=
"d0e350"></a>Acknowledgements</h2>
</div>
<p>Thank you to Rani Sharoni, Terje Sletteb&oslash;, Stefan de
Bruijn and Paul Grenyer for providing important comments.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e355" id="d0e355"></a>References</h2>
</div>
<div class="bibliomixed"><a name="ISO14882" id="ISO14882"></a>
<p class="bibliomixed">[ISO14882] ISO/IEC 14882-2003, Standard for
the C++ language</p>
</div>
<div class="bibliomixed"><a name="Vandevoorde-" id=
"Vandevoorde-"></a>
<p class="bibliomixed">[Vandevoorde-] David Vandevoorde and Nicolai
M. Josuttis, <span class="citetitle"><i class="citetitle">C++
templates: The Complete Guide</i></span>, Addison Wesley 2002</p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c3" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e82" href="#d0e82" id=
"ftn.d0e82">1</a>]</sup> <tt class="literal">const</tt> and
<tt class="literal">volatile</tt> are the cv-qualifiers.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
