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
11
votes
1 answer

Why is std::initializer_list conversion not preferred?

Consider this snippet: #include #include void f(std::vector){std::cout << __PRETTY_FUNCTION__ << '\n';} void f(int x){std::cout << __PRETTY_FUNCTION__ << '\n';} int main() { f({42}); } Live on Coliru If you run it,…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
11
votes
2 answers

Implicit conversion operator priority

In the following piece of code (live on coliru): #include #include int main() { struct S { operator bool () const { return false; } operator std::string () const { return "false"; } } s; …
YSC
  • 38,212
  • 9
  • 96
  • 149
11
votes
2 answers

How to deal with an overload resolution ambiguity of functions with generics?

Consider this class with two functions, one with Int argument, the other with a generic one: class C { // ... operator fun f(index: Int): Pair = ... operator fun f(key: K): V = ... } When it is parameterized as C
hotkey
  • 140,743
  • 39
  • 371
  • 326
11
votes
2 answers

Ambiguous multiple inheritance of template classes

I've got a real situation which can be summarized in the following example: template< typename ListenerType > struct Notifier { void add_listener( ListenerType& ){} }; struct TimeListener{ }; struct SpaceListener{ }; struct A : public…
11
votes
1 answer

Why does Scala type inference fail here?

I have this class in Scala: object Util { class Tapper[A](tapMe: A) { def tap(f: A => Unit): A = { f(tapMe) tapMe } def tap(fs: (A => Unit)*): A = { fs.foreach(_(tapMe)) tapMe } } implicit def…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
11
votes
2 answers

Disambiguating between overloaded methods passed as delegates in an overloaded call

Suppose I had this in C#: class OverloadTest { void Main() { CallWithDelegate(SomeOverloadedMethod); } delegate void SomeDelegateWithoutParameters(); delegate void SomeDelegateWithParameter(int n); void…
HLorenzi
  • 480
  • 7
  • 17
11
votes
4 answers

Why variadic template constructor matches better than copy constructor?

The following code does not compile: #include #include struct Foo { Foo() { std::cout << "Foo()" << std::endl; } Foo(int) { std::cout << "Foo(int)" << std::endl; } }; template struct Bar { Foo foo; …
11
votes
2 answers

When are two function templates considered as partially ordered and when are ambiguous?

I'm completely confused after reading the question How to make these std::function parameters unambiguous?, so far I'd thought I understood what partial ordering of function templates is, but after reading that question I wrote down three examples…
11
votes
1 answer

Is this overload resolution correct?

From: Is it safe to overload char* and std::string? #include #include void foo(std::string str) { std::cout << "std::string\n"; } void foo(char* str) { std::cout << "char*\n"; } int main(int argc, char *argv[]) { …
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
11
votes
6 answers

Order of operator overload resolution involving temporaries

Consider the following minimal example: #include using namespace std; class myostream : public ostream { public: myostream(ostream const &other) : ostream(other.rdbuf()) { } }; int main() { cout <<…
Thomas
  • 174,939
  • 50
  • 355
  • 478
11
votes
8 answers

Method Overloading with Types C#

I was wondering if the following is possible. Create a class that accepts an anonymous type (string, int, decimal, customObject, etc), then have overloaded methods that do different operations based on the Type. Example class TestClass { …
Matt
  • 6,264
  • 10
  • 54
  • 82
11
votes
3 answers

How does the operator overload resolution work within namespaces?

I found a strange behaviour of C++ resolution of operator-overloading, I can't explain myself. A pointer to some resource describing it would be just as nice as an answer. I have 2 translation units. In one (called util.cpp/h) I declare and define…
hildensia
  • 1,650
  • 2
  • 12
  • 24
10
votes
6 answers

member function hiding free function

void foo(int) { } class X { void foo() { } void bar() { foo(42); // error: no matching function for call to 'X::foo(int)' // note: candidate is: // note: void X::foo() // note: …
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
10
votes
3 answers

How to pass a function template as a template argument?

#include template void print(Args const&... args) { (std::cout << ... << args); } int main() { std::cout << 1 << 2 << 3 << std::endl; // ok print(1, 2, 3); // ok print(1, 2, 3,…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
10
votes
0 answers

why the explicit conversion function of derived class return type is not a candidate in the context of direct-initialization

Given the following example: #include struct A{ A() = default; A(A const&){} }; struct B:A{}; struct C{ explicit operator B(){ return B{}; } }; int main(){ C c; A a(c); // #1 } GCC and Clang both report…
xmh0511
  • 7,010
  • 1
  • 9
  • 36