overload-resolution

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

≡放荡痞女 提交于 2019-12-10 12:45:19
问题 Consider this class with two functions, one with Int argument, the other with a generic one: class C<K, V> { // ... operator fun f(index: Int): Pair<K, V> = ... operator fun f(key: K): V = ... } When it is parameterized as C<Int, SomeType> , K is Int , and both functions match the calls, resulting into an error: val m = C<Int, SomeType>() m.f(1) Overload resolution ambiguity. All these functions match: public final fun f(index: Int): SomeType defined in C public final fun f(key: Int): Pair

Wrong overload selected for stream manipulator

吃可爱长大的小学妹 提交于 2019-12-10 11:46:42
问题 Here's the code: #include <iostream> #include <iomanip> #include <typeinfo> #if 0 std::ostream &foo(std::ostream &os, std::ios_base &(*x)(std::ios_base &), bool show_id = false) { if ( show_id ) os << "(" << typeid(x).name() << ") "; return os << x; } #endif template<typename T> std::ostream &foo(std::ostream &os, T const &t, bool show_id = false) { if ( show_id ) os << "(" << typeid(t).name() << ") "; return os << t; } int main() { foo(std::cout, std::hex) << 255 << std::endl; foo(std::cout,

C# Method overload resolution not selecting concrete generic override

守給你的承諾、 提交于 2019-12-10 10:09:06
问题 This complete C# program illustrates the issue: public abstract class Executor<T> { public abstract void Execute(T item); } class StringExecutor : Executor<string> { public void Execute(object item) { // why does this method call back into itself instead of binding // to the more specific "string" overload. this.Execute((string)item); } public override void Execute(string item) { } } class Program { static void Main(string[] args) { object item = "value"; new StringExecutor() // stack

Implicit conversion operator priority

限于喜欢 提交于 2019-12-10 01:50:21
问题 In the following piece of code (live on coliru): #include <iostream> #include <string> int main() { struct S { operator bool () const { return false; } operator std::string () const { return "false"; } } s; std::cout << s << "\n"; // outputs 0 } How does the compiler choose to pick the implicit conversion to bool over std::string ? My hypothesis is that in this case, it might be purely the order of declaration of the different flavours of std::basic_ostream::operator<<, but is it all? Does

Confusion around function call resolution

旧巷老猫 提交于 2019-12-09 17:08:55
问题 This question is inspired by this one. Consider the code: namespace ns { template <typename T> void swap(T& a, T& b) { using namespace std; swap(a, b); } } After some test with GCC, I found that swap(a, b); resolves to 1) std::swap if T has overloaded std::swap (e.g., standard container types) 2) ns::swap otherwise, leading to infinite recursion. So, it seems that the compiler will first try to find a match in the namespace ns . If a match is found, the search ends. But this is not the case

Method overload resolution using dynamic argument

为君一笑 提交于 2019-12-09 17:03:08
问题 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 compile ). The error is: the call is ambiguous between [the first two overloads of M ] . static void M(Func<int> f) { } static void M(Func<string> f) { } static void M(Func<dynamic> f) { } static dynamic DynamicObject() { return new object(); } static

Overloaded function template disambiguation with `std::enable_if` and non-deduced context

大兔子大兔子 提交于 2019-12-09 14:17:51
问题 Consider the following code: template <typename T> struct dependent_type { using type = T; }; template <typename T> auto foo(T) -> std::enable_if_t<std::is_same<T, int>{}> { std::cout << "a\n"; } template<typename T> void foo(typename dependent_type<T>::type) { std::cout << "b\n"; } The first overload of foo can deduce T from its invocation. The second overload of foo is a non-deduced context. int main() { foo<int>( 1 ); // prints "b" foo<double>( 1.0 ); // prints "b" foo( 1 ); // prints "a"

Overload resolution oddity

北慕城南 提交于 2019-12-08 19:38:02
问题 Not sure if this is C# 4+ specific, but just noticed this. Consider the following classes: class Base { protected void Foo(object bar, DayOfWeek day) { } } class Program : Base { protected void Foo(object bar, object baz) { } void Bar(DayOfWeek day) { Foo(new { day }, day); } } The call to Foo in Bar , resolves to Foo(object, object) . While changing it to: class Base { } class Program : Base { protected void Foo(object bar, object baz) { } protected void Foo(object bar, DayOfWeek day) { }

Disambiguating between overloaded methods passed as delegates in an overloaded call

本小妞迷上赌 提交于 2019-12-08 16:01:17
问题 Suppose I had this in C#: class OverloadTest { void Main() { CallWithDelegate(SomeOverloadedMethod); } delegate void SomeDelegateWithoutParameters(); delegate void SomeDelegateWithParameter(int n); void CallWithDelegate(SomeDelegateWithoutParameters del) { } void CallWithDelegate(SomeDelegateWithParameter del) { } void SomeOverloadedMethod() { } void SomeOverloadedMethod(int n) { } } Of course, this does not compile, because the line CallWithDelegate(SomeOverloadedMethod); is ambiguous. Now,

How does overload resolution work in the context of private modifier?

自作多情 提交于 2019-12-08 05:50:37
问题 I am unable to understand the output of the following C++ snippet. Should not objc.fn() call A 's fn() since B's fn() is private and should not be visible in C . However, the answer is: the call to fn() is ambiguous. How? #include<iostream> using namespace std; class A{ public: void fn() { cout << "1"; } }; class B{ void fn() { cout << "2"; } }; class C: public A, public B {}; int main(){ C objc; objc.fn(); return 0; } 回答1: According to the book C++ Templates: The Complete Guide Appendix B.1,