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

C++ automatically choose correct overloaded function based on argument type

I'm creating a interface OutputItem for writing to a json object. I want there to be different modes of writing to the json object, thus I have provided a overloaded method json_serialize which takes a SetConstructType* or AppendConstructType*…
Tom
  • 1,235
  • 9
  • 22
0
votes
1 answer

How to pick correct vector insert() function when overloading?

I'm trying to implement my own vector dml::vector whose API is same as std::vector. What make me confuse is the insert() function's overloading resolution. I would like to call: template void insert(iterator pos, InputIt first,…
ChrisZZ
  • 1,521
  • 2
  • 17
  • 24
0
votes
1 answer

Is this overloading?

If I have two functions like this: int f ( int a ) {} void f ( signed a ) {} are they overloaded? When in main() I call f(5), I get an error: old declaration ‘void f(int)’
0
votes
0 answers

C++ : Inheriting from lambdas, overload resolution "quirk"

Why is the using-declaration inside struct S necessary? struct S inherits from both l1's and l2's types, which means their respective operator()s are considered when calling s(123). This is corroborated by the fact that, once I remove using, gcc…
0
votes
2 answers

Overload resolution works for normal method but not for constructor

My goal is to have a series of overloads, where the correct version of a method gets called depending on the type of the parameter (known only at runtime). However, I've run into an interesting problem in a case where the method I want to overload…
0
votes
3 answers

function overloading and parameter inheritance

In the following code #include using namespace std; class A { }; class B : public A { }; class C { public: void foo(const A& a) { cout << "A";} void foo(const B& b) { cout << "B";} }; int main() { C c; const A& o = B(); …
perencia
  • 1,498
  • 3
  • 11
  • 19
0
votes
1 answer

What does boost::hana::always do more than just "always return its first argument"?

At the doc page of boost::hana::always I read that always(x) is a function such that always(x)(y...) == x for any y.... This makes me think that it shouldn't behave any differently than this lambda: [](auto const&...){ return false; }. However it…
Enlico
  • 23,259
  • 6
  • 48
  • 102
0
votes
1 answer

Overloading functions with optional arguments

I would like to write a procedure which takes an optional argument, which may be of type FooType or of type BarType, such that this program is valid: module m implicit none type :: FooType end type type :: BarType end type end…
veryreverie
  • 2,871
  • 2
  • 13
  • 26
0
votes
1 answer

Weird constructor SFINAE error with std::initializer_list

I can't understand why the following code doesn't compile. The error message given by the compiler isn't that helpful either. Working example: #include #include template struct TIteratorValue { using Type =…
0
votes
1 answer

ADL not working outside (even of structure)

If I have a structure the overloads begin, end, etc. like the following: #include template struct Array10 { private: std::array m_array; public: constexpr auto begin() { return std::begin(m_array); //…
0
votes
4 answers

What is the cause of this overload resolution headache?

I've got a program where I've got a lot of nested if/switch statements which were repeated in several places. I tried to extract that out and put the switches in a template method class, and then allow clients to overload which switch branches they…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
0
votes
1 answer

How can one invoke the non-extension `run` function (the one without scope / "object reference") in environments where there is an object scope?

Example: data class T(val flag: Boolean) { constructor(n: Int) : this(run { // Some computation here... }) } In this example, the custom constructor needs to run some computation in order to determine which…
Tom
  • 4,910
  • 5
  • 33
  • 48
0
votes
1 answer

Standard ML Foldl/Foldr function with multiplication operator?

For Standard ML (SMLNJ), for the foldr and foldl functions, what is the correct way to use the multiplication operator? Using foldr (op *) 1 [1,2,3]; gives an error Standard ML of New Jersey v110.78 [built: Thu Aug 31 03:45:42 2017] stdIn:1.12…
Eugene
  • 10,957
  • 20
  • 69
  • 97
0
votes
4 answers

How to avoid an invalid overload from the std library?

In my C++14 project I'm using #include using std::max; // for max(a,b) but I also want to provide a max() function taking any number of arguments (of equal type). To this end, I added template
Walter
  • 44,150
  • 20
  • 113
  • 196
0
votes
2 answers

Invoking the correct instance of an overloaded function template when passing derived classes

Let's say I have a class template template struct Base {}; And I've written some overloaded function templates around it: template void f(Base&, T2){ std::cout << "1" << std::endl; } template
16807
  • 1,418
  • 2
  • 18
  • 32