Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8315769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:12:26+00:00 2026-06-08T21:12:26+00:00

The code is: #include <iostream> using namespace std; // compares two objects template <typename

  • 0

The code is:

#include <iostream>
using namespace std;

// compares two objects
template <typename T> void compare(const T&, const T&){
    cout<<"T"<<endl;
};
// compares elements in two sequences
template <class U, class V> void compare(U, U, V){
    cout<<"UV"<<endl;
};
// plain functions to handle C-style character strings
void compare(const char*, const char*){
    cout<<"ordinary"<<endl;
};

int main() {

    cout<<"-------------------------char* --------------------------"<< endl;

    char* c="a";
    char* d="b";
    compare(c,d);

cout<<"------------------------- char [2]---------------------------"<< endl;

    char e[]= "a";
    char f[]="b";
    compare(e,f);

    system("pause");
}

The result is:

————————-char* ————————–

T

————————- char [2]———————–

ordinary

And my question is:
Why does compare(c,d) call compare(const T&, const T&) and compare(e,f) call the ordinary function even though the arguments of the two functions are char*s?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:12:27+00:00Added an answer on June 8, 2026 at 9:12 pm

    It appears that VS2005 may be erroneously treating the e and f variables as const char * types.

    Consider the following code:

    #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;
    }
    

    which outputs:

    T:        <- char *
    T:        <- char []
    ordinary: <- const char []
    

    Section 13.3 Overload resolution of C++03 (section numbers appear to be unchanged in C++11 so the same comments apply there) specifies how to select which function is used and I’ll try to explain it in (relatively) simple terms, given that the standard is rather a dry read.

    Basically, a list of candidate functions is built based on how the function is actually being called (as a member function of an class/object, regular (unadorned) function calls, calls via a pointer and so on).

    Then, out of those, a list of viable functions is extracted based on argument counts.

    Then, from the viable functions, the best fit function is selected based on the idea of a minimal implicit conversion sequence (see 13.3.3 Best viable function of C++03).

    In essence, there is a “cost” for selecting a function from the viable list that is set based on the implicit conversions required for each argument. The cost of selecting the function is the sum of the costs for each individual argument to that function, and the compiler will chose the function with the minimal cost.

    If two functions are found with the same cost, the standard states the the compiler should treat it as an error.

    So, if you have a function where an implicit conversion happens to one argument, it will be preferred over one where two arguments have to be converted in that same way.

    The “cost” can be see in the table below in the Rank column. An exact match has less cost than promotion, which has less cost than conversion.

    Rank                 Conversion
    ----                 ----------
    Exact match          No conversions required
                         Lvalue-to-rvalue conversion
                         Array-to-pointer conversion
                         Function-to-pointer conversion
                         Qualification conversion
    Promotion            Integral promotions
                         Floating point promotions
    Conversion           Integral conversion
                         Floating point conversions
                         Floating-integral conversions
                         Pointer conversions
                         Pointer-to-member conversions
                         Boolean conversions
    

    In places where the conversion cost is identical for functions F1 and F2 (such as in your case), F1 is considered better if:

    F1 is a non-template function and F2 is a function template specialization.


    However, that’s not the whole story since the template code and non-template code are all exact matches hence you would expect to see the non-template function called in all cases rather than just the third.

    That’s covered further on in the standard: The answer lies in section 13.3.3.2 Ranking implicit conversion sequences. That section states that an identical rank would result in ambiguity except under certain conditions, one of which is:

    Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if (1) S1 is a proper subsequence of S2 (comparing the conversion sequences in the canonical form defined by 13.3.3.1.1, excluding any Lvalue Transformation; the identity conversion sequence is considered to be a subsequence of any non-identity conversion sequence) …

    The conversions for the template version are actually a proper subset (qualification conversion) of the non-template version (qualification AND array-to-pointer conversions), and proper subsets are deemed to have a lower cost.

    Hence it prefers the template version in the first two cases. In the third case, the only conversions are array-to-pointer for the non-template version and qualification for the template version, hence there’s no subset in either direction, and it prefers the non-template version based on the rule I mentioned above, under the ranking table).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code: #include <iostream> using namespace std; template<class T> void printbinary3(T num){
The following code #include <iostream> using namespace std; int main() { const char* const
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
I have the following code: #include<iostream> using namespace std; typedef void (*HandlerFunc)(int, int); HandlerFunc
This is the C++ code: #include<iostream> using namespace std; int a=8; int fun(int &a)
My Code: #include <iostream> using namespace std; class A { public: virtual void print(void)
#include <string> #include <algorithm> #include <iostream> using namespace std; bool compare( const string::size_type i,
code: #include<iostream> using namespace std; template<class T, int N> class point { T coordinate[N];
Please Explain the following code #include <iostream> using namespace std; int main() { const

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.