Questions tagged [overload-resolution]

Overload resolution is a language mechanism to select among several viable function overloads. Its rules are intricate and often surprising, even for experienced users.

Overload resolution is a language mechanism to select among several viable function overloads.

Possible functions overloads are first determined from a candidate list resolution that itself includes inter ala. name lookups, template or generic determination and instantiation, promotions and conversions.

733 questions
34
votes
2 answers

Overload resolution looking into namespaces

The following code fails as expected, because no overload of get is found. Using std::getwould solve the problem. #include int main() { std::array ar{2,3}; auto r = get<0>(ar);//fails, get was not declared in this…
LcdDrm
  • 1,009
  • 7
  • 14
34
votes
3 answers

Is this change in overload resolution between Clang 3.5 and 3.6 correct or a bug?

The code below compiles in Visual Studio 2013, gcc 4.8, clang 3.4 and clang 3.5 (Apple LLVM 6.0) but does not compile in clang 3.6 (via Apple LLVM 6.1) The code is a simplified version of a complicated class in our codebase which is the minimum…
32
votes
1 answer

Breaking change in method overload resolution in C# 6 - explanation?

We've recently moved from VS2013 to VS2017 in our company. After the upgrade our codebase would no longer build. We would get the following error: The call is ambiguous between the following methods or properties: 'IRepository.Get(object, params…
milosz
  • 865
  • 2
  • 7
  • 24
31
votes
4 answers

Calling a const function rather than its non-const version

I tried to wrap something similar to Qt's shared data pointers for my purposes, and upon testing I found out that when the const function should be called, its non-const version was chosen instead. I'm compiling with C++0x options, and here is a…
30
votes
2 answers

Why is this Java generic method call ambiguous when only one method is valid when separate?

I am trying to understand why the compiler is unable to resolve the bar method call. I would expect bar(Xyz::new) to always select bar(Supplier) as bar(T extends Xyz) can never match due to the upper bound on Xyz. public void foo(T…
Timothy Cole
  • 304
  • 2
  • 7
30
votes
3 answers

Overload resolution and arrays: which function should be called?

Consider the following program: #include #include void f(char const*&&) { std::puts("char const*&&"); } // (1) void f(char const* const&) { std::puts("char const* const&"); } // (2) template void f(char…
James McNellis
  • 348,265
  • 75
  • 913
  • 977
30
votes
1 answer

This case of template function overloading eludes my understanding

#include template struct identity { typedef T type; }; template void bar(T) { std::cout << "a" << std::endl; } template void bar(typename identity::type) { std::cout << "b" << std::endl; } int…
gd1
  • 11,300
  • 7
  • 49
  • 88
27
votes
2 answers

C++0x const RValue reference as function parameter

I am trying to understand why someone would write a function that takes a const rvalue reference. In the code example below what purpose is the const rvalue reference function (returning "3"). And why does overload resolution preference the const…
MW_dev
  • 2,146
  • 1
  • 26
  • 40
27
votes
3 answers

How can I prevent C++ guessing a second template argument?

I'm using a C++ library (strf) which, somewhere within it, has the following code: namespace strf { template inline auto range(ForwardIt begin, ForwardIt end) { /* ... */ } template inline auto…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
27
votes
3 answers

Why is this overload of a conversion operator chosen?

Consider the following code. struct any { template operator T &&() const; template operator T &() const; }; int main() { int a = any{}; } Here the second conversion operator is chosen by the overload…
avakar
  • 32,009
  • 9
  • 68
  • 103
27
votes
2 answers

Overloaded lambdas in C++ and differences between clang and gcc

I'm playing with a trick to overload lambdas in C++. Specifically: // For std::function #include // For std::string #include // For std::cout #include template struct overload : F... { …
wyer33
  • 6,060
  • 4
  • 23
  • 53
27
votes
2 answers

On a nonconst object, why won't C++ call the const version of a method with public-const and private-nonconst overloads?

class C { public: void foo() const {} private: void foo() {} }; int main() { C c; c.foo(); } MSVC 2013 doesn't like this: > error C2248: 'C::foo' : cannot access private member declared in class 'C' If I cast to a const reference,…
26
votes
2 answers

Why doesn't narrowing affect overload resolution?

Consider the following: struct A { A(float ) { } A(int ) { } }; int main() { A{1.1}; // error: ambiguous } This fails to compile with an error about an ambiguous overload of A::A. Both candidates are considered viable, because the…
Barry
  • 286,269
  • 29
  • 621
  • 977
26
votes
1 answer

In overload resolution, does selection of a function that uses the ambiguous conversion sequence necessarily result in the call being ill-formed?

The question arose while I was researching the answer to this SO question. Consider the following code: struct A{ operator char() const{ return 'a'; } operator int() const{ return 10; } }; struct B { void operator<< (int) { } }; int…
T.C.
  • 133,968
  • 17
  • 288
  • 421
25
votes
3 answers

Overload resolution for multiply inherited operator()

First, consider this C++ code: #include struct foo_int { void print(int x) { printf("int %d\n", x); } }; struct foo_str { void print(const char* x) { printf("str %s\n", x); } }; struct foo :…
1
2
3
48 49