12

I'm trying to understand how the overload selection rules result in the following (unintuitive) behavior. When I have the following functions:

#include <iostream>

// Overload 1
template<class T>
void write(T data)
{
  std::cout << "Called write(T data)" << std::endl;
}

// Overload 2
template<class T, class ...U>
void write(T&& obj, U&&... objs)
{
  std::cout << "Called write(T&& obj, U&&... objs)" << std::endl;
}

int main(int, char**)
{
  int j = 0;
  write(j);
  return 0;
}

The void write(T data) overload (Overload 1) is selected. I think that makes sense to me: the candidates for overload selection are void write<T>(T) T = int and void write<T,U>(T&) T = int, U = <>. Both write(T) and write(T&) would be equally specialized, but Overload 2 has an empty parameter pack so Overload 1 is selected. However, if I add a third overload:

#include <iostream>

// Overload 0
void write(const int& data)
{
  std::cout << "Called write(const int& data)" << std::endl;
}

// Overload 1
template<class T>
void write(T data)
{
  std::cout << "Called write(T data)" << std::endl;
}

// Overload 2
template<class T, class ...U>
void write(T&& obj, U&&... objs)
{
  std::cout << "Called write(T&& obj, U&&... objs)" << std::endl;
}

int main(int, char**)
{
  int j = 0;
  write(j);
  return 0;
}

Then all of a sudden void write(T&& obj, U&&... objs) (Overload 2) is what gets called. Why does adding an overload that doesn't get selected change which overload actually does get selected?

If the only candidates were void write<T,U>(T&) T = int, U = <> and void write(const int&) I understand why void write<T,U>(T&) would be selected, so perhaps something about adding the extra overload prevents void write(T data) from participating on overload selection? If so why?

Since this seems to be compiler specific behavior this was observed on gcc 7.3.0.

Some more interesting behavior: If the functions are reordered such that the new overload is placed between the original two (i.e. Overload 1, then Overload 0, then Overload 2) then gcc rejects it with call of overloaded ‘write(int&)’ is ambiguous. If the functions are reordered such that the new overload is last (i.e. Overload 1, then Overload 2, then Overload 0) then write(const int& data) is selected.

2
  • 2
    FWIW, clang rejects the second code block for an ambiguity while MSVS compiles but displays Called write(T data) Commented Sep 20, 2018 at 15:55
  • 2
    gcc uses #2 overload whereas clang reject as ambiguous: Demo.
    – Jarod42
    Commented Sep 20, 2018 at 16:31

1 Answer 1

2

I think this is a GCC bug:

Overloads are:

  • Overload 0: write(const int&)
  • Overload 1: write(T) [T=int] -> write(int)
  • Overload 2: write(T&&,U&&...) [T=int&,U=[]] -> write(int&)

Overload 0 is a better match than overload 1 because overload 0 is not a template functions specialization.

Overload 1 is a better match than overload 2 because overload 1 is a function template more specialized than overload 2.

Overload 2 is a better match than overload 0 because the cv qualifier of the parameter type of overload 2 int& is a subset of the one of overload 0: const int&.

So the call is ambiguous as reported by Clang.


To simplify, the best viable function is evaluate here in 4 steps here, when comparing two functions:

  1. Check which is the best conversion sequence (all conversions are the identity conversion here), then if the conversion ranks are the same:
  2. Check if between two reference bindings one is better than the other, then if one is not a reference binding or the two bindings are not differentiable,
  3. Check if one of the function is a template specialization and the other is not, then if the two are template specializations
  4. Check if one of the specializations is more specialized than the other.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.