35

Can any one help me understand the following code

#include <iostream>

void foo(const char * c)
{
   std::cout << "const char *" << std::endl;
}

template <size_t N>
void foo(const char (&t) [N])
{
   std::cout << "array ref" << std::endl;
   std::cout << sizeof(t) << std::endl;
}

int main()
{
    const char t[34] = {'1'};
    foo(t);

    char d[34] = {'1'};
    foo(d);
}

The output is

const char *
array ref
34

Why does the first foo calls the const char * version ? How can I make it call the reference version ?

12
  • 2
    Ah, there's nothing like ambiguous overload resolution (aka Koenig lookup). Generally speaking, template functions are chosen last when given more than one choice of function. Commented Mar 2, 2011 at 21:23
  • 3
    Congratulations, you have come to the very end of C++! Commented Mar 2, 2011 at 21:25
  • 1
    @Loadmaster: Minor nitpick, but Koenig lookup is a process of determining what functions are to be considered. Then overload resolution is the following process of determining which one of them to use. If the code said ::foo(t), it would not involve Koenig lookup but would involve overload resolution. Commented Mar 2, 2011 at 21:42
  • 1
    @junjanes No. @Marlon's compiler is wrong. Commented Mar 2, 2011 at 21:43
  • 1
    @user511274: Time to accept some answers. Commented Nov 4, 2011 at 1:31

3 Answers 3

15

Conversion of const char[N] to const char* is considered an "exact match" (to make literals easier, mainly), and between two exact matches a non-template function takes precedence.

You can use enable_if and is_array to force it to do what you want.


A messy way to force it might be:

#include <iostream>

template <typename T>
void foo(const T* c)
{
   std::cout << "const T*" << std::endl;
}

template <typename T, size_t N>
void foo(const T (&t) [N])
{
   std::cout << "array ref" << std::endl;
}

int main()
{
    const char t[34] = {'1'};
    foo(t);

    char d[34] = {'1'};
    foo(d);
}

/*
array ref
array ref
*/

I realise that the OP had char not some generic T, but nonetheless this demonstrates that the problem lay in one overload being a template and not the other.

Sign up to request clarification or add additional context in comments.

11 Comments

How do you explain that the second version of foo() is called the second time ?
@otibom: Conversion of char[N] doesn't have the same rule. char[N] to const char* is not an exact match: it's convertible, but the template function matches more easily.
@otibom The first version ain't const, but the template version takes a const array pointer.
@otibom: Conversion from char[34] to char* or to const char (&) [34] is "exact match", but conversion from char[34] to const char* is a pointer conversion, which does make a difference in overloading.
@aschepler Why is conversion from char[34] to const char (&)[34] considered an "exact match"? Can you explain in more detail?
|
5

Let's look at this modified example with no template.

void foo(const char * c)
{
    std::cout << "const char *" << std::endl;
}

void foo(const char (&t) [34])
{
    std::cout << "const char (&) [34]" << std::endl;
}

int main()
{
    const char t[34] = {'1'};
    foo(t);
}

My compiler says call of overloaded foo is ambiguous. This is because conversions from array to pointer are considered an "Exact" conversion sequence and are not better than the null conversion sequence for overload resolution (Standard section 13.3.3.1.1.)

In the original code, the template parameter N can be deduced as 34, but then both non-template foo(const char*) and foo<34>(const char (&)[34]) are considered in overload resolution. Since neither is better than the other by conversion rules, the non-template function beats the template function.

Fixing things seems tricky. It seems like the is_array template from header <type_traits> (from C++0x if possible or Boost if not) might help.

5 Comments

Indeed, you can use enable_if and is_array to get what you want.
@Tomalak: Is there a simple way using C++03 and Boost? I was going to give an example, then realized I absolutely needed rvalue-references, which are not part of the language in the question's tag.
@aschepler: I updated my answer with an approach involving levelling the playing field.
@aschepler: (Actually it's about the same as your code above, but from the opposite direction)
Except my code is for explanation purposes only, not a solution to any problem. I'm still curious about doing it for only pointer/array to cv-char types. Maybe template <typename T> void foo(const T* c, enable_if<is_same<T,char> >* = 0);?
1

This appears to be different for various compilers.

Mircosoft and Borland both use the const char* version, while GNU is giving the output you described.

Here is a snippet from the C++ standard:

14.8.2.1 Deducing template arguments from a function call [temp.deduct.call]

Template argument deduction is done by comparing each function template parameter type (call it P) with the type of the corresponding argument of the call (call it A) as described below.

If P is not a reference type:

-- If A is an array type, the pointer type produced by the array-to-pointer standard conversion (4.2) is used in place of A for type deduction; otherwise,

-- If A is a function type, the pointer type produced by the function-to-pointer standard conversion (4.3) is used in place of A for type deduction; otherwise,

-- If A is a cv-qualified type, the top level cv-qualifiers of A's type are ignored for type deduction.

If P is a cv-qualified type, the top level cv-qualifiers of P's type are ignored for type deduction. If P is a reference type, the type referred to by P is used for type deduction

The compiler will build an A list as follows:

Argument:        t                 d
A:          char const[34]      char[34]

And parameter list P:

Parameter:       c                 t
P:            char const*       char const& t[N]

By default the compiler should choose non-referenced parameters. GNU is dong it wrong the second time for some reason.

2 Comments

No, it is not. You're forgetting about non-templates being preferred. If MS and Borland give different output, then they are non-compliant. Template type deduction is completely irrelevant.
Template deduction argument is not the issue. Overload resolution is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.