overload-resolution

How to resolve ambiguity of call to overloaded function with literal 0 and pointer

℡╲_俬逩灬. 提交于 2021-02-16 10:00:28
问题 I'm pretty sure this must have been here already, but I didn't find much information on how to solve this kind of problem (without casting on the call): Given two overloads, I want that a call with function with a literal 0 always calls the unsigned int version: void func( unsigned int ) { cout << "unsigned int" << endl; } void func( void * ) { cout << "void *" << endl; } func( 0 ); // error: ambiguous call I understand why this happens, but I don't want to write func( 0u ) or even func(

How to resolve ambiguity of call to overloaded function with literal 0 and pointer

纵饮孤独 提交于 2021-02-16 09:59:56
问题 I'm pretty sure this must have been here already, but I didn't find much information on how to solve this kind of problem (without casting on the call): Given two overloads, I want that a call with function with a literal 0 always calls the unsigned int version: void func( unsigned int ) { cout << "unsigned int" << endl; } void func( void * ) { cout << "void *" << endl; } func( 0 ); // error: ambiguous call I understand why this happens, but I don't want to write func( 0u ) or even func(

Can't pass std::min to function, copy of std::min works

情到浓时终转凉″ 提交于 2021-02-08 15:48:19
问题 Passing std::min to a function does not compile. I copied the libcpp declaration of std::min into my source file and it works. What's wrong with the std version? Same happens with clang and gcc. Tested on godbolt: https://godbolt.org/g/zwRqUA #include <thread> #include <algorithm> namespace mystd { // declaration copied verbatim from std::min (libcpp 4.0) template <class _Tp> inline constexpr const _Tp& mymin(const _Tp& __a, const _Tp& __b) { return std::min(__a, __b); } } int main() { std:

Ambiguous multiple inheritance of template classes

若如初见. 提交于 2021-02-08 12:32:14
问题 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 Notifier< TimeListener > , public Notifier< SpaceListener > { }; struct B : TimeListener{ }; int main() { A a; B b; a.add_listener( b ); // why is ambiguous? return 0; } Why is not obvious to the compiler that B is a TimeListener , and therefore the only

How to explain this “call is ambiguous” error?

半世苍凉 提交于 2021-02-07 13:14:51
问题 The Problem Consider these two extension methods which are just a simple map from any type T1 to T2 , plus an overload to fluently map over Task<T> : public static class Ext { public static T2 Map<T1, T2>(this T1 x, Func<T1, T2> f) => f(x); public static async Task<T2> Map<T1, T2>(this Task<T1> x, Func<T1, T2> f) => (await x).Map(f); } Now, when I use the second overload with a mapping to a reference type... var a = Task .FromResult("foo") .Map(x => $"hello {x}"); // ERROR var b = Task

How can I programmatically do method overload resolution in C#?

耗尽温柔 提交于 2021-02-07 04:49:20
问题 When the C# compiler interprets a method invocation it must use (static) argument types to determine which overload is actually being invoked. I want to be able to do this programmatically. If I have the name of a method (a string ), the type that declares it (an instance of System.Type ), and a list of argument types I want to be able to call a standard library function and get back a MethodInfo object representing the method the C# compiler would choose to invoke. For instance if I have

About operator overload resolution

自作多情 提交于 2021-02-04 19:09:26
问题 Suppose two classes with the following implicit and explicit operator pattern: class Foo { public static implicit operator decimal (Foo foo) { throw new NotImplementedException(); } public static implicit operator Foo (decimal value) { throw new NotImplementedException(); } public static Foo operator +(Foo left, Foo right) { throw new NotImplementedException(); } } class Bar { public static explicit operator decimal (Bar bar) { throw new NotImplementedException(); } public static explicit

How can I specialize an algorithm for iterators that point to complex values?

北战南征 提交于 2021-01-29 13:26:54
问题 I am trying to write an algorithm that works on iterators (similar to the STL algorithms) however I need to write a specialization of the algorithm to act differently when the iterators point to complex values vs regular double values. Here is a basic example: #include <complex> #include <iostream> #include <vector> using namespace std; template <typename InputIt> void DoSomething(InputIt first, InputIt last) { cout << "Regular Double" << endl; for (; first != last; ++first) { cout << *first

Most terse and reusable way of wrapping template or overloaded functions in function objects

≡放荡痞女 提交于 2021-01-28 21:31:47
问题 Scenario 1: a template function pred template<typename T> bool pred(T t) { /* return a bool based on t */ } Scenario 2: a set of functions overloaded on the same name pred bool pred(A t) { /* return a bool based on t */ } bool pred(B t) { /* return a bool based on t */ } bool pred(C t) { /* return a bool based on t */ } ... Whichever of the two scenarii we're in, the bottom line is that pred does not refer to a function, and so it cannot be passed around, e.g. as a unary predicate to std: