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
Filter by
Sorted by
Tagged with
0 votes
0 answers
92 views

C++ Inheritance and Overloaded Methods

I would like some clarification as to the behavior of the following: #include <iostream> class A {}; class B : public A {}; class Q : public A {}; class C : public B {}; void doOverload(...
Patrick Wright's user avatar
0 votes
0 answers
79 views

Why are members from multiple inheritances of same class template ambiguous?

When inheriting from a class template multiple times, there is the problem that multiple inheritance of these template classes makes member functions ambiguous (also here), because apparently it is ...
vohir's user avatar
  • 21
9 votes
1 answer
240 views

Overload resolution for function templates with auto non-type template parameter

What are the rules for the selection of overloaded function templates in case of non-type template parameters, if one of the parameters is a placeholder type. I am confused with the current behavior ...
Fedor's user avatar
  • 18.5k
1 vote
1 answer
83 views

Does const "this" pointer implies constness of all its members?

I know that if we add a "const" qualifier after a function declaration, it implies the implicit "this" pointer become const so we cannot directly modify any member However, in a ...
torez233's user avatar
  • 373
3 votes
1 answer
127 views

is it possible to resolve template T to array reference instead of pointer to an array?

Disclaimer: This is an XY problem kind question. Is it possible to resolve template <typename T> to (const T (&)[N]) overload instead of (const T*)? For example: template <typename T> ...
Sergey Kolesnik's user avatar
1 vote
0 answers
131 views

When should I put a `requires` clause in a template function's signature instead of before it?

In C++20 you can add a requires clause (1) after a template parameter list as well as (2) after a function declaration (like a specifier). In the following snippet foo is an example of (1) and bar is ...
nebuch's user avatar
  • 6,765
3 votes
2 answers
200 views

Overload resolution of user-defined type conversion

I considered a type conversion from one type to the other, which are defined by two way, i.e., type conversion constructor and type conversion function. struct to_type; struct from_type{ operator ...
hmIto's user avatar
  • 63
0 votes
1 answer
43 views

Why is my function override for STL class function used even for wrong argument type? [duplicate]

Consider this program: #include <iostream> #include <stack> class MyStack : public std::stack<double> { public: void push(char) { std::cout << "Using char ...
Lukáš J.'s user avatar
1 vote
2 answers
221 views

In C#, how can I select the correct overload based on a generic argument?

// overloads void f(int x){} void f(float x){} // generic void s<T>(T t){ f(t); // <<< cannot convert from 'T' to 'int' } // use s(10); The C# compiler responds that, in the body ...
xtofl's user avatar
  • 41.1k
-3 votes
1 answer
138 views

C# overload resolution and generics

I have the following methods public static EnumerableAssertions<T> AssertThat<T>(IEnumerable<T> collection) { Debug.WriteLine("Enumerable!"); return new ...
Oliver Saggau's user avatar
1 vote
3 answers
59 views

Avoiding template parameter substitution completely

I have a class that can accept arithmetic types and std::complex. A simplified code of the class is #include <complex> template<typename T> struct is_complex : std::false_type {}; ...
SomeoneWithPassion's user avatar
3 votes
1 answer
326 views

how does overloading with C linkage work in C++?

Consider this example: int foo(void); extern "C" int foo(void); int main() { return foo(); } It errors out with: $ g++ -c main.cpp main.cpp:2:16: error: conflicting declaration of ‘int ...
stsp's user avatar
  • 388
1 vote
1 answer
110 views

How to detect overload resolution failure?

Consider the following code snippet: #include <tuple> #include <type_traits> #include <utility> #include <cstddef> template <typename ...> struct always_false : std::...
El Mismo Sol's user avatar
4 votes
1 answer
117 views

Compilers work differently for overload resolution with user-defined conversion to rvalue reference

I encountered a weird behavior in that gcc and clang select different overloaded constructors when their arguments are implicitly converted from user-defined conversion operators. The concerning code ...
s417-lama's user avatar
3 votes
1 answer
86 views

Different results of std::isinf for different types and different compilers

Compiling with MSVC(v19.33), this does not compile (C2668 ambiguous call to overloaded function): std::cout << std::isinf(0) << std::endl; But this compiles: std::cout << std::isinf(...
starriet 주녕차's user avatar
1 vote
0 answers
53 views

Why is const char * overload chosen instead of std::string? [duplicate]

In this code: #include <string> #include <iostream> void my_function(const char* value) { std::cout << "char * " << (value ? value : "nullptr"); } ...
Rob L's user avatar
  • 2,400
3 votes
2 answers
79 views

User defined operator conversion resolution order

Is it possible to configure the resolution order of user defined operators? Consider the following code: #include <memory> #include <utility> using P = std::unique_ptr<int>; struct ...
Deev's user avatar
  • 481
0 votes
0 answers
32 views

cannot convert ‘<unresolved overloaded function type>’ to [duplicate]

This question relates to Call a class method inside a function which was passed as a string parameter I have two class hierarchies. The most base classes for each are obj_t and obj2_t. I have a ...
sancho.s ReinstateMonicaCellio's user avatar
0 votes
1 answer
95 views

What's the relevance of the Note in [over.load]/1?

(You might see this question as a duplicate of this, but, to be honest, I've not really understood the matter, so I'm asking separately with my own wording.) [over.load]/1 reads: Not all function ...
Enlico's user avatar
  • 25.5k
5 votes
1 answer
95 views

Check the viability of a conversion function [duplicate]

I have the following code: struct S { operator int(); // F1 operator double(); // F2 }; int main() { int res = S(); } Since neither F1 nor F2 is cv-qualified, the type of the ...
mada's user avatar
  • 1,852
0 votes
1 answer
96 views

Initialization by conversion function for direct reference binding

I found out the rules for determining the candidate conversion functions, for direct reference binding, are not described clearly by the standard at least for me. The rules are found in [over.match....
mada's user avatar
  • 1,852
3 votes
1 answer
163 views

Overload resolution of int vs std::vector<int> with an initializer list of a single int

Why does c++ choose a primitive type overload match over a "better" matching initializer list? #include <vector> void foo([[maybe_unused]] int i) {} void foo([[maybe_unused]] const ...
Oliver Schönrock's user avatar
1 vote
1 answer
31 views

Overloaded method resolution for variadic tuples displays strange results

I'm getting strange and unexpected results for this code: https://godbolt.org/z/8vs87vcKK #include <string> #include <iostream> #include <tuple> using namespace std; struct Foo { ...
David Bien's user avatar
4 votes
0 answers
76 views

The type 'T' cannot be used as type parameter 'T' in the generic type or method but it should not use generic method [duplicate]

In my code (.NET6.0) I have a class with two very similar methods. One is not generic, but has a second optional parameter. Second method is generic with only one (generic) parameter. var c = new C(); ...
Beyboy's user avatar
  • 63
3 votes
3 answers
135 views

Raw int pointer vs vector::iterator<int>

I was trying to understand difference between a raw pointer and an vector iterator. However, the following program trips me out. Does template function have priority over non-template function? ...
Hee Hwang's user avatar
  • 121
2 votes
2 answers
253 views

Overload resolution and array-to-pointer decay - why is int (&a)[2] and int* a considered equally exact regarding overload resolution

tl;dr; why is - regarding overload resolution / function declaration - the pointer type treated as a more equally exact match than the array type, even though a passed variable is really of type array ...
Chilippso's user avatar
  • 461
5 votes
2 answers
244 views

Specialization of variadic template function over the non-variadic arguments

I have a template for a function that accepts at least one parameter and performs some formatting on the rest: template <typename T, typename... ARGS> void foo(T first, ARGS&&... args) { ...
sorush-r's user avatar
  • 10.7k
3 votes
1 answer
75 views

What part of overload resolution (or more generally of the function call processing) does the value category of the argument play a role in?

C++ Templates - The Complete Guide, in §C.1, reads Overload resolution is performed to find the best candidate. If there is one, it is selected; otherwise, the call is ambiguous. Then, in §C.2, ...
Enlico's user avatar
  • 25.5k
3 votes
2 answers
255 views

Deduction of template arguments for friend function declared in class template

Consider the following example: #include <iostream> template <class T, int V> struct S { friend int Func(T) // decl-1 { return V; } }; struct U { friend int Func(...
Shinuzi Kyura's user avatar
8 votes
1 answer
174 views

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 ...
mada's user avatar
  • 1,852
3 votes
0 answers
149 views

Understand the whole process of function template overload resolution

I found out that the overload resolution process is the more complicated I have ever learned in C++, Therefore, bear in mind that this topic is relatively difficult for me to understand easily, so be ...
mada's user avatar
  • 1,852
3 votes
4 answers
125 views

foo(int, int) is picked over foo(int...)

In this piece of code, why the compiler is not able to reference the method that has varargs argument from static context. private static void doSomething(int... nums) { System.out.println("...
anupamD's user avatar
  • 912
5 votes
1 answer
55 views

C++ function resolution matches different function when I adjust their sequence

I've got a test program to see how compiler(g++) match template function: #include<stdio.h> template<class T>void f(T){printf("T\n");} template<class T>void f(T*){printf(&...
Immanuel Kant's user avatar
0 votes
1 answer
86 views

How does function template ordering de-prioritize empty function parameter packs in C++? [duplicate]

In the C++20 standard, [temp.func.order] contains the following non-normative example: template<class T, class... U> void f(T, U...); // #1 template<class T > void f(T);...
user3188445's user avatar
  • 4,278
3 votes
2 answers
588 views

How can I specify an overload when passing an overloaded function to another? [duplicate]

Simple example: std::vector<std::string> strings{"1","2","3"); std::vector<double> doubles(3); transform(begin(strings), end(strings), begin(doubles), std::...
bitmask's user avatar
  • 33.5k
2 votes
2 answers
90 views

Why is this function call didn't reject the unsuitable overload?

Consider the following code: #include<vector> #include<ranges> #include<algorithm> //using namespace std; using namespace std::ranges; int main() { std::vector<int> a = {}; ...
zclll's user avatar
  • 77
0 votes
0 answers
67 views

How to ensure correct overload resolution with variadic template constructor in C++

I have browsed similar threads, but none have answered my question. I am looking for a way to ensure correct overload resolution with two competing constructors, where one takes two parameters and the ...
Mikesz's user avatar
  • 1
16 votes
3 answers
1k views

Why are static member functions considered to have an implicit object parameter during overload resolution?

In this link : Implicit object parameter In this quote : If any candidate function is a member function (static or non-static) that does not have an explicit object parameter (since C++23), but not ...
f877576's user avatar
  • 459
1 vote
2 answers
284 views

ADL conflicts with a function in the current namespace

I have a function in my namespace. Its name is destroy. std namespace also contains a function with this name. When I call my function within my namespace, the call is ambiguous, because std::destroy ...
anton_rh's user avatar
  • 8,710
7 votes
2 answers
431 views

SFINAE doesn't work in recursive function

Let's create currying function. template <typename TFunc, typename TArg> class CurryT { public: CurryT(const TFunc &func, const TArg &arg) : func(func), arg(arg ) {} ...
anton_rh's user avatar
  • 8,710
1 vote
0 answers
40 views

Can constrained templated conversion operator kick in overload resolution of builtin operator? [duplicate]

For example: #include <concepts> struct A { int v; template<std::same_as<int> T> operator T() {return v;} }; static_assert(std::same_as<int, decltype(A{4} + 2)>); ...
VainMan's user avatar
  • 2,122
5 votes
1 answer
115 views

Question on [over.match.funcs.general]/9 and inherited copy/move constructors

Per § 12.2.2.1 [over.match.funcs.general]/9-sentence-2: A constructor inherited from class type C ([class.inhctor.init]) that has a first parameter of type “reference to cv1 P” (including such a ...
mada's user avatar
  • 1,852
0 votes
0 answers
59 views

Confusion about "perfect match" and "matchb with minor adjustments" in the context of perfect forwarding ctor vs other constructors

I was reading Chapter 25 from C++ Templates - The Complete Guide - 2nd ed., where, given code more or less like this #include <utility> template<typename...> struct Tuple; template<...
Enlico's user avatar
  • 25.5k
1 vote
2 answers
94 views

C# Method resolution does not work as expected at runtime

I looked at https://stackoverflow.com/a/5174773/787958 and the method resolution is not working as expected in my instance. I have JSON coming into a method that I want to create a typed object from ...
ScubaSteve's user avatar
  • 7,984
1 vote
0 answers
53 views

R: Overloading primitive, non-generic functions

I'd like to find the minimal hack to be able to say module::obj when module is not a package but a list or environment. After some digging, I see the following works for the new use case, but breaks ...
Abel Borges's user avatar
11 votes
1 answer
303 views

Passing a concept-constrained function overload

The following code fails to compile (Godbolt link): #include <concepts> template <class Fn> decltype(auto) g(Fn&& fn) { return fn(); } template <typename T> requires(std::...
jcai's user avatar
  • 3,543
11 votes
1 answer
781 views

Array reference binding vs. array-to-pointer conversion with templates

This code sample fails to compile due to ambiguous overload resolution void g(char (&t)[4]) {} void g(char *t) {} int main() { char a[] = "123"; g(a); } and careful reading of ...
AnT stands with Russia's user avatar
3 votes
0 answers
150 views

C++ user-defined conversions, ref-qualifiers and overload resolution

Please, help me understand what's wrong with this piece of code: #include <string> #include <utility> class Sample { public: explicit Sample(std::string data): _data(std::move(...
Yurii A's user avatar
  • 369
2 votes
1 answer
81 views

Compiler variance for overloading over array reference parameters

The following program is, as expected, accepted by both GCC, Clang and MSVC (for various compiler and language versions): // #g.1: rvalue reference function parameter constexpr bool g(int&&) ...
dfrib's user avatar
  • 72k
-1 votes
1 answer
58 views

C++ name lookup inconsistency for template/overloaded functions

I'm building a minimal binary parser/serializer for one of my projects and I got this error that seems inconsistent at first. I have two top functions I want to expose T parse<T>(bytes) and ...
petoknm's user avatar
  • 11

1
2
3 4 5
16