Exact Match and Identity conversion

N

Neelesh Bodas

Few questions that have always confused me:

1. is exact match (for overloaded resolution) related to/involves/
needs/means/implies identity conversion?

2.

void foo(int &x);
void bar(int x);

int main()
{
int s = 10;
foo(s); //exact match or reference binding?
bar(s); //exact match or lvalue-to-rvalue-conversion?
}

3.
void foo(int (&arr)[10]);

int main()
{
char s[] = "hello";
foo(s); //exact match or reference binding? Please explain why.
}

Thanks
-Neelesh
 
N

Neelesh Bodas

Correcting the (obvious) error in 3rd.


3.
void foo(char (&arr)[6]);

int main()
{
char s[] = "hello";
foo(s); //exact match or reference binding? Please explain why.

}

-N
 
P

PicO

Few questions that have always confused me:

1. is exact match (for overloaded resolution) related to/involves/
needs/means/implies identity conversion?

2.

void foo(int &x);
void bar(int x);

int main()
{
int s = 10;
foo(s); //exact match or reference binding?
bar(s); //exact match or lvalue-to-rvalue-conversion?

}

3.
void foo(int (&arr)[10]);

int main()
{
char s[] = "hello";
foo(s); //exact match or reference binding? Please explain why.

}

Thanks
-Neelesh

yea .. i also want to know can i return a array of ( character or
integer or like that ) in C++ and if i can't , does there's any other
solutions like return pointer or address or like that ...
 
T

tony_in_da_uk

yea .. i also want to know can i return a array of ( character or
integer or like that ) in C++ and if i can't , does there's any other
solutions like return pointer or address or like that ...

You can return STL containers like std::string, std::vector<int> etc..

Tony
 
T

tony_in_da_uk

3.
void foo(char (&arr)[6]);

int main()
{
char s[] = "hello";
foo(s); //exact match or reference binding? Please explain why.
}

Sorry Neelesh - I'm no expert on these details. What I'm wondering is
why you care? Is there some subtle function selection issue /
ambiguity resolution you find this relevant to?

Thanks,

Tony
 
N

Neelesh Bodas

3.
void foo(char (&arr)[6]);
int main()
{
char s[] = "hello";
foo(s); //exact match or reference binding? Please explain why.
}

Sorry Neelesh - I'm no expert on these details. What I'm wondering is
why you care? Is there some subtle function selection issue /
ambiguity resolution you find this relevant to?

A classic question: Explain how the call to foo(r) gets resolved in
the following case:

void foo(const char* a);
void foo(const char (&)[9]);

int main()
{
char r[9] = "abcdefgh";
foo(r);
}

Note: I know "what it gets resolved to" and I also have my own
explanation of "why does it get resolved to whatever it gets resolved
to" but I am not sure my reasoning is correct. Hence the original
question(s).

-N
 
T

tony_in_da_uk

3.
void foo(char (&arr)[6]);
int main()
{
char s[] = "hello";
foo(s); //exact match or reference binding? Please explain why.
}
Sorry Neelesh - I'm no expert on these details. What I'm wondering is
why you care? Is there some subtle function selection issue /
ambiguity resolution you find this relevant to?

A classic question: Explain how the call to foo(r) gets resolved in
the following case:

void foo(const char* a);
void foo(const char (&)[9]);

int main()
{
char r[9] = "abcdefgh";
foo(r);

}

Note: I know "what it gets resolved to" and I also have my own
explanation of "why does it get resolved to whatever it gets resolved
to" but I am not sure my reasoning is correct. Hence the original
question(s).

-N

Perhaps you should have a read through the section 13 / overloading in
the standard? A draft is available at http://www.open-std.org/jtc1/sc22/wg21/docs/wp/html/oct97/
..

Cheers,

Tony
 
V

Victor Bazarov

[..]
Note: I know "what it gets resolved to" and I also have my own
explanation of "why does it get resolved to whatever it gets resolved
to" but I am not sure my reasoning is correct. Hence the original
question(s).

-N

Perhaps you should have a read through the section 13 / overloading in
the standard? A draft is available at
http://www.open-std.org/jtc1/sc22/wg21/docs/wp/html/oct97/ .

Perhaps you could point out the relevant portions of it so we just
sit in awe and say "Oh... that's what we've been missing...". How
about it?
 
V

Victor Bazarov

PicO said:
so i can't return reference :( ..

A reference to what? You need an object that survives the function
in order to use the reference to it, don't you? You _can_ return
a reference, it just has to be to the object that is still alive
when you use that reference.
 
T

tony_in_da_uk

Perhaps you could point out the relevant portions of it so we just
sit in awe and say "Oh... that's what we've been missing...". How
about it?

Perhaps I could :p, but it seems the OP wants a general understanding
of how this resolution works, so there's no point in my pointing out
specifics of particular cases. Further, I'm not keen to waste copious
quantities of my time when I don't have an issue with this aspect of C+
+ behaviour. If the OP didn't know where to look, then he does now,
and IMO it sounds like he has the technical ability to interpret the
material there.

Tony
 
T

tony_in_da_uk

A reference to what? You need an object that survives the function
in order to use the reference to it, don't you? You _can_ return
a reference, it just has to be to the object that is still alive
when you use that reference.

Your options are limited: either the calling function needs to provide
a buffer, accept ownership of some returned data, or the called
function must own the data. In the latter case, this could be through
static data or class member data, but the latter is problematic if the
class containing the called function is "destruct"ed before the caller
has finished using the data. In practice, callee-owned data is often
useful for performance reasons, but it's often hackish to implement.
Singleton patterns might help, but it's still problematic to guarantee
a singleton instance doesn't go out of scope before the caller
finishes with the data: see Modern C++ Design for a discussion and
implementation alternatives. Having the called function own the data
is also problematic in that it often obliges the caller to make
further calls to explicitly free the memory, and of course expecting
your client code to do anything reliably is a recipe for trouble.
Thankfully, a memory leak isn't _usually_ critical.
 
N

Neelesh Bodas

Perhaps I could :p, but it seems the OP wants a general understanding
of how this resolution works,

No, i don't want to know how overloaded resolution in general works, I
was/am looking for precise answers to the three questions. The example
I quoted in one of my replies was just to explain one example of *why*
I am looking for answer to these three questions.
If the OP didn't know where to look, then he does now,
and IMO it sounds like he has the technical ability to interpret the
material there.

I did know where to look, and I *did* look in the C++ standard before
posting this question. I even *did* search on this group for similar
questions, and I *did* find one-or-two places that went near to what I
asked, but couldnot find any clear explanation on the same. Hence
posted the question.

As an aside, the material in the C++ standard is more "technical" than
"explanatory". I do get confused at few places because of the wording,
and this is one of them.

anyways, thanks a lot for your precious time.

Cheers
'OP'.
 
J

James Kanze

Few questions that have always confused me:

Just to make my position clear: pragmatically, I'd avoid code
where any of this matters.
1. is exact match (for overloaded resolution) related to/involves/
needs/means/implies identity conversion?

For the most part, overload resolution involving standard
conversions only uses the rank, defined in Table 11 in
§13.3.3.1. So identity (no conversion), lvalue-to-rvalue,
array-to-pointer, function-to-pointer and qualification
conversions are all considered "exact match". (There are a few
special additional rules: if one conversion is a proper subset
of another, for example, or if the cv qualifications in one case
are a proper subset of those of another.)
void foo(int &x);
void bar(int x);
int main()
{
int s = 10;
foo(s); //exact match or reference binding?
bar(s); //exact match or lvalue-to-rvalue-conversion?
}

Reference binding is an exact match: §13.3.3.1.4/1: "When a
parameter of reference type binds directly (8.5.3) to an
argument expression, the implicit conversion sequence is the
identity conversion, unless the argument expression has a type
that is a derived class of the parameter type, in which case the
implicit conversion sequence is a derived-to-base Conversion."

And lvalue-to-rvalue conversion also has the rank "exact match".
3.
void foo(int (&arr)[10]);
int main()
{
char s[] = "hello";
foo(s); //exact match or reference binding? Please explain why.
}

I'm not sure I understand the question. s can't be bound to the
parameter of foo, the set of viable functions is empty, and
there is nothing for overload resolution to resolve.

If foo were declared:
void foo( char (&arr)[6] ) ;
then it would be an exact match. Change the 6 to any other
value, or remove it entirely, and the function isn't callable,
however.
 
T

tony_in_da_uk

A classic question: Explain how the call to foo(r) gets resolved in
the following case:

void foo(const char* a);
void foo(const char (&)[9]);

int main()
{
char r[9] = "abcdefgh";
foo(r);

}

Note: I know "what it gets resolved to" and I also have my own
explanation of "why does it get resolved to whatever it gets resolved
to" but I am not sure my reasoning is correct. Hence the original
question(s).

Hi Neelesh,

"Table 2" in that part of the Standard lists the conversions. A
reference binding like above is in the "identity" category, of "exact
match" rank. The other is an "array-to-pointer conversion", from the
"Lvalue Transformation" category, also of "exact match" rank. So,
both have exact match rank.

In describing the resolution order, the draft Standard says:

"2 [Note: As described in clause _conv_, a standard conversion
sequence
is either the Identity conversion by itself (that is, no
conversion)
or consists of one to three conversions from the other four
cate-
gories. At most one conversion from each category is allowed in
a
single standard conversion sequence. If there are two or more
conver-
sions in the sequence, the conversions are applied in the
canonical
order: Lvalue Transformation, Promotion or Conversion,
Qualification
Adjustment. --end note]"

While this isn't explicit, it at least seems implicit that the
"Identity conversion" is ordered before canonical order given.

To date, I haven't seen any clearer indication that this ordering is
required by the standard, but it does jump around a lot.

Still wondering why you're looking into this. Are you concerned
whether you can rely on all compilers to have implemented it this way?

Tony
 
N

Neelesh Bodas

3.
void foo(int (&arr)[10]);
int main()
{
char s[] = "hello";
foo(s); //exact match or reference binding? Please explain why.
}

I'm not sure I understand the question. s can't be bound to the
parameter of foo, the set of viable functions is empty, and
there is nothing for overload resolution to resolve.
yeah, typo(?) on my part, should have been -
void foo( char (&arr)[6] ) ;
then it would be an exact match.

thanks, wanted to make this sure.

-N.
 
N

Neelesh Bodas

Still wondering why you're looking into this. Are you concerned
whether you can rely on all compilers to have implemented it this way?

I was trying out certain non-trivial examples of overloaded-resolution
where I came across these questions. I read the standard but wanted to
take opinion of Gurus on these confusing aspects. Hence this question.
And yeah, this has nothing to do (yet) with any of the real-life C++
code I have been working on.

Thanks again
Neelesh
 
B

BobR

Neelesh Bodas said:
I was trying out certain non-trivial examples of overloaded-resolution
where I came across these questions.
I read the standard but wanted to
take opinion of Gurus on these confusing aspects.

Are you saying the 'Gurus' in this NG are smarter than the people worknig on
the standards? <G><G><G>
 

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

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top