Exact Match and Identity conversion

Discussion in 'C++' started by Neelesh Bodas, Aug 8, 2007.

  1. 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
     
    Neelesh Bodas, Aug 8, 2007
    #1
    1. Advertising

  2. On Aug 8, 10:21 pm, Neelesh Bodas <> wrote:
    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
     
    Neelesh Bodas, Aug 8, 2007
    #2
    1. Advertising

  3. Neelesh Bodas

    PicO Guest

    On Aug 8, 10:21 am, Neelesh Bodas <> wrote:
    > 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 ...
     
    PicO, Aug 8, 2007
    #3
  4. Neelesh Bodas

    Guest

    On Aug 9, 5:11 am, PicO <> wrote:
    > 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
     
    , Aug 9, 2007
    #4
  5. Neelesh Bodas

    Guest

    On Aug 9, 2:25 am, Neelesh Bodas <> wrote:
    > 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
     
    , Aug 9, 2007
    #5
  6. On Aug 9, 8:26 am, wrote:
    > On Aug 9, 2:25 am, Neelesh Bodas <> wrote:
    >
    > > 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
     
    Neelesh Bodas, Aug 9, 2007
    #6
  7. Neelesh Bodas

    Guest

    On Aug 9, 1:35 pm, Neelesh Bodas <> wrote:
    > On Aug 9, 8:26 am, wrote:
    >
    > > On Aug 9, 2:25 am, Neelesh Bodas <> wrote:

    >
    > > > 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
     
    , Aug 9, 2007
    #7
  8. wrote:
    > On Aug 9, 1:35 pm, Neelesh Bodas <> wrote:
    >> [..]
    >> 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?
     
    Victor Bazarov, Aug 9, 2007
    #8
  9. Neelesh Bodas

    PicO Guest

    On Aug 8, 8:24 pm, wrote:
    > On Aug 9, 5:11 am, PicO <> wrote:
    >
    > > 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


    so i can't return reference :( ..
     
    PicO, Aug 10, 2007
    #9
  10. PicO wrote:
    > On Aug 8, 8:24 pm, wrote:
    >> On Aug 9, 5:11 am, PicO <> wrote:
    >>
    >>> 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

    >
    > 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.
     
    Victor Bazarov, Aug 10, 2007
    #10
  11. Neelesh Bodas

    Guest

    On Aug 9, 9:16 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > 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?


    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
     
    , Aug 13, 2007
    #11
  12. Neelesh Bodas

    Guest

    On Aug 10, 8:50 pm, "Victor Bazarov" <> wrote:
    > PicO wrote:
    > > 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.


    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.
     
    , Aug 13, 2007
    #12
  13. On Aug 13, 8:54 am, wrote:
    > On Aug 9, 9:16 pm, "Victor Bazarov" <> wrote:
    >
    > > wrote:
    > > > 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?

    >
    > 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'.
     
    Neelesh Bodas, Aug 13, 2007
    #13
  14. Neelesh Bodas

    James Kanze Guest

    On Aug 8, 7:21 pm, Neelesh Bodas <> wrote:
    > 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.)

    > 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?
    > }


    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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Aug 13, 2007
    #14
  15. Neelesh Bodas

    Guest

    On Aug 9, 1:35 pm, Neelesh Bodas <> wrote:
    > 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
     
    , Aug 14, 2007
    #15
  16. On Aug 13, 1:38 pm, James Kanze <> wrote:
    > On Aug 8, 7:21 pm, Neelesh Bodas <> wrote:
    >
    >
    > > 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.
     
    Neelesh Bodas, Aug 14, 2007
    #16
  17. On Aug 14, 7:10 am, wrote:
    >
    >
    > 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
     
    Neelesh Bodas, Aug 14, 2007
    #17
  18. Neelesh Bodas

    BobR Guest

    Neelesh Bodas <> wrote in message...
    > On Aug 14, 7:10 am, wrote:
    > >
    > > 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.


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

    --
    Bob R
    POVrookie
     
    BobR, Aug 15, 2007
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    2
    Views:
    325
    John Harrison
    Sep 10, 2005
  2. Wayne Shu
    Replies:
    2
    Views:
    289
    James Kanze
    Jul 29, 2007
  3. Mr.SpOOn
    Replies:
    4
    Views:
    520
    Lawrence D'Oliveiro
    Nov 4, 2008
  4. jimgardener

    finding an exact match of filenames

    jimgardener, Oct 16, 2010, in forum: Python
    Replies:
    1
    Views:
    293
    Peter Otten
    Oct 16, 2010
  5. John Butler

    search reg-exp for exact match

    John Butler, Nov 20, 2008, in forum: Ruby
    Replies:
    2
    Views:
    240
    Robert Klemme
    Nov 22, 2008
Loading...

Share This Page