function overloading: direct-match vs trivial-conversion

S

subramanian100in

Suppose T is a type.

Consider the two functions:

void fn(T& first)
{
....
}

void fn(T const & second)
{
....
}

Suppose I have the declaration
T obj;

If I call fn(obj), then the function, void fn(T& first) will be
called. This is because:
When both
1) 'T' to 'T&' (this is a direct match)
2) 'T' to 'T const &' (this involves trivial conversion)
are present, direct match takes higher precedence. If direct match is
not found, only then trivial conversion is considered. That is, if the
first function void fn(T& first) is not present, only then the second
function void fn(T const & second) will be called.

Is my above understanding correct ?

Kindly clarify.

Thanks
V.Subramanian
 
L

Looney

Suppose T is a type.

Consider the two functions:

void fn(T& first)
{
...

}

void fn(T const & second)
{
...

}

Suppose I have the declaration
T obj;

If I call fn(obj), then the function, void fn(T& first) will be
called. This is because:
When both
1) 'T' to 'T&' (this is a direct match)
2) 'T' to 'T const &' (this involves trivial conversion)
are present, direct match takes higher precedence. If direct match is
not found, only then trivial conversion is considered. That is, if the
first function void fn(T& first) is not present, only then the second
function void fn(T const & second) will be called.

Is my above understanding correct ?

Kindly clarify.

Thanks
V.Subramanian

yes that is correct .
check out http://accu.org/index.php/journals/268
for a details
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top