Incorrect overloaded function chosen in C++ - by Nobody_12345

Status : 

  Deferred<br /><br />
		The product team has reviewed this issue and has deferred it for consideration at a later time.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


2
0
Sign in
to vote
ID 756412 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 8/1/2012 1:09:06 AM
Access Restriction Public

Description

Found in VS2005 but apparently in 2012 as well. The following code will choose the wrong overloaded function:

#include <iostream>
using namespace std;

template <typename T> void compare (const T&, const T&) {
    cout << "T:        ";
};

template <class U, class V> void compare (U, U, V) {
    cout << "UV:       ";
};

void compare (const char*, const char*) {
    cout << "ordinary: ";
};

int main (void) {
    char* c = "a";
    char* d = "b";
    compare (c,d);
    cout << "<- char *\n";

    char e[] = "a";
    char f[] = "b";
    compare (e,f);
    cout << "<- char []\n";

    const char g[] = "a";
    const char h[] = "b";
    compare (g,h);
    cout << "<- const char []\n";

    return 0;
}

More details at http://stackoverflow.com/questions/11752307/why-does-this-code-call-different-template-function-in-vs2005/11752401#11752401 but basically, ISO C++03 sections 13.3.3 state that the ranking should decide which function gets called and, if the ranking is identical, a preference should be given to an ICS which is a proper subset - for cases 1 and 2 in the code, that's the template function (only qualification) rather than the non-template one (qualification AND array-to-pointer conversion). If it's not a proper subset (like case 3 in the code), the non-template version is selected.

VC++ erroneously selects the non-template version for case 2.
Sign in to post a comment.
Posted by Helen [MSFT] on 8/1/2012 at 2:22 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Posted by Macy [MSFT] on 8/1/2012 at 1:49 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)