1

With this code:

namespace core {
template <typename T>
auto max(T&&, T&&) { }

struct type_t final { };
} // namespace core

template <typename T>
auto max(T&&, T&&) { }

int main() {
  core::type_t _1;
  core::type_t _2;
  max(_1, _2);
}

Compiler complains about an overloading issue:

/tmp/untitled1/main.cpp: In function ‘int main()’:
/tmp/untitled1/main.cpp:14:6: error: call of overloaded ‘max(core::type_t&, core::type_t&)’ is ambiguous
   14 |   max(_1, _2);
      |   ~~~^~~~~~~~
/tmp/untitled1/main.cpp:9:6: note: candidate: ‘auto max(T&&, T&&) [with T = core::type_t&]’
    9 | auto max(T&&, T&&) { }
      |      ^~~
/tmp/untitled1/main.cpp:3:6: note: candidate: ‘auto core::max(T&&, T&&) [with T = core::type_t&]’
    3 | auto max(T&&, T&&) { }
      |      ^~~

And the problem could be fixed by calling the global scope max() function using ::. Why it's related to overloading? the core::max is defined under different namespace.

Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32

0 Answers0