why no ambiguity error in code?

This is a discussion on why no ambiguity error in code? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Any ideas why there is no ambiguity issues in the code? Which myfunc is called? The code can ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    why no ambiguity error in code?

    Hello everyone,


    Any ideas why there is no ambiguity issues in the code? Which myfunc is called?

    The code can pass compile and link in Visual Studio 2008 without any warning messages. The output is 100.

    Code:
    int myfunc (int& a) {return 100;}
    
    int myfunc (const int& a) {return 200;}
    
    int main()
    {
    	int a = 1;
    	int& ra = a;
    	int rtn;
    	rtn = myfunc (ra); // call which myfunc? output 100
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My deduction is that it uses the non-const version, because ra is not a const. Try using a const int &cra and call myfunc(cra) and see if that doesn't return 200.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    int& is an exact match. const int& would have to add a const. Clearly, int& is the better match and is thus used.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    But if we remove & and change code to the following segment,

    Code:
    int myfunc (int a) {return 100;}
    
    int myfunc (const int a) {return 200;}
    There will be duplicated defined function error. So I think the rules are,

    1. (as you said) compiler allow more than one matched function exist, but has a priority to match them;
    2. (as I showed above) for const reference and non-const reference, compiler will treat them of two different functions, but for non-const value and const value, compiler will treat them as same functions.

    My conclusions (1) and (2) are correct?

    Quote Originally Posted by CornedBee View Post
    int& is an exact match. const int& would have to add a const. Clearly, int& is the better match and is thus used.

    regards,
    George

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    1) Correct.
    2) Also correct. And if you'll refer back to your recent overriding thread, I said there quite explicitly that top-level cv qualifiers don't matter in function resolution. int and const int are treated the same. int& and const int& are not, because the const is not top-level.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Question answered.

    Quote Originally Posted by CornedBee View Post
    1) Correct.
    2) Also correct. And if you'll refer back to your recent overriding thread, I said there quite explicitly that top-level cv qualifiers don't matter in function resolution. int and const int are treated the same. int& and const int& are not, because the const is not top-level.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21