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
9
votes
2 answers

Multiple inheritence leads to spurious ambiguous virtual function overload

In this example, classes Foo and Bar are provided from a library. My class Baz inherits from both. struct Foo { void do_stuff (int, int); }; struct Bar { virtual void do_stuff (float) = 0; }; struct Baz : public Foo, public Bar { void…
9
votes
1 answer

compiler cannot deduce overload of std::max

With my compiler typedef const double&(*fT)(const double&, const double&); typedef std::function std_func; fT f1 = std::max; //(1) std_func f2 =…
jimifiki
  • 5,377
  • 2
  • 34
  • 60
9
votes
2 answers

Wouldn't it make sense to overload with respect to noexcept?

I am trying to understand the noexcept feature. I know it could be confusing, but besides that could noexcept be deduced from the calling function when possible. This is a non working example of this situation, void f(){} void f() noexcept{} // not…
alfC
  • 14,261
  • 4
  • 67
  • 118
9
votes
1 answer

How does overload resolution work when an argument is an overloaded function?

Preamble Overload resolution in C++ can be an overly complex process. It takes quite a lot of mental effort to understand all of the C++ rules that govern overload resolution. Recently it occurred to me that the presence of the name of an overloaded…
Leon
  • 31,443
  • 4
  • 72
  • 97
9
votes
1 answer

Overload resolution with template parameters

I am having trouble understanding why the following leads to an ambiguous call: #include // generic version f(X, Y) template void f(X x, Y y) { std::cout << "generic" << std::endl; } // overload version template…
linuxfever
  • 3,763
  • 2
  • 19
  • 43
9
votes
2 answers

Overload resolution and user defined conversion

Consider the simple code : struct A; struct B { B(){} B(A const&){ } }; struct A { operator int() const {return 0;}; }; void func(B){} void func(char){} int main() { func(A()); //ambiguous call oO } First of all I'm not sure if I understand…
Angelus Mortis
  • 1,534
  • 11
  • 25
9
votes
2 answers

std::set insert with initialiser lists

I have this simple code: struct Base { Base(int xx, int yy) : x(xx), y(yy){} bool operator<(const Base& b) const {return (x < b.x) || (x==b.x && y < b.y);} int x; int y; }; struct D1 : Base { D1(int x, int y) : Base(x,…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
9
votes
1 answer

Taking the address of an overloaded function template is possible, sometimes

On gcc 4.9.0: #include #include struct A { typedef int type; }; template void foo(T*) { std::cout << "a" << std::endl; } template void foo(typename T::type*) { std::cout << "b" << std::endl;…
gd1
  • 11,300
  • 7
  • 49
  • 88
9
votes
1 answer

ambiguous overload for ‘operator=’ with c++11 std::move and copy and swap idiom

I am getting the following error: [matt ~] g++ -std=c++11 main.cpp -DCOPY_AND_SWAP && ./a.out main.cpp: In function ‘int main(int, const char* const*)’: main.cpp:101:24: error: ambiguous overload for ‘operator=’ in ‘move = std::move((* &…
9
votes
3 answers

Incorrect overload resolution for 2-argument functions

Let's take the following example program: #include namespace half_float { template struct half_expr {}; struct half : half_expr { operator float() const; }; template half sin(const…
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
9
votes
4 answers

Ambiguous string::operator= call for type with implicit conversion to int and string

Given the following program: #include #include using namespace std; struct GenericType{ operator string(){ return "Hello World"; } operator int(){ return 111; } operator double(){ return 123.4; …
8
votes
1 answer

How conversion of pointer-to-base to void is better than pointer-to-derived to void conversion

[over.ics.rank]/4: [..] (4.3) If class B is derived directly or indirectly from class A, conversion of B* to A* is better than conversion of B* to void*, and conversion of A* to void* is better than conversion of B* to void*. So if I…
mada
  • 1,646
  • 1
  • 15
8
votes
2 answers

Method overload resolution using dynamic argument

This may have been answered before. I see many "dynamic method overload resolution" questions, but none that deal specifically with passing a dynamic argument. In the following code, in Test, the last call to M cannot be resolved (it doesn't…
Daniel
  • 47,404
  • 11
  • 101
  • 179
8
votes
1 answer

Array-to-pointer conversion + rvalue-ref: Overload resolution difference GCC vs clang

#include #define FUNC() { std::cout << __PRETTY_FUNCTION__ << "\n"; } void foo(char const*&& ) FUNC() // A void foo(char const(&)[4]) FUNC() // B int main() { foo("bar"); } Demo When using an rvalue reference in the parameter…
dyp
  • 38,334
  • 13
  • 112
  • 177
8
votes
1 answer

Overload Resolution differs between compilers

I have constructed the following minimal example of my problem: #include struct Foo { Foo() { std::cout << "default" << std::endl; } Foo(Foo& f2) { std::cout << "non-const" << std::endl; } Foo(const Foo& f2) { …
JMC
  • 1,723
  • 1
  • 11
  • 20