ambiguity

Ambiguous method call with Action<T> parameter overload

我怕爱的太早我们不能终老 提交于 2019-12-01 17:27:12
I encountered some unexpected compiler behaviour when calling overloaded method with different Action<T> variations. Let's say I have this class Test and I'm creating its instance in the CallTest constructor. public class Test { public Test(Action<long> arg) { } public Test(Action<decimal> arg) { } } public class CallTest { public CallTest() { Test t = new Test(TestDecimal); } public void TestDecimal(decimal arg) { } public void TestLong(long arg) { } } When calling the Test constructor with either TestDecimal or TestLong as a parameter I'm receiving the following error: The call is ambiguous

Ambiguous method call with Action<T> parameter overload

跟風遠走 提交于 2019-12-01 16:26:52
问题 I encountered some unexpected compiler behaviour when calling overloaded method with different Action<T> variations. Let's say I have this class Test and I'm creating its instance in the CallTest constructor. public class Test { public Test(Action<long> arg) { } public Test(Action<decimal> arg) { } } public class CallTest { public CallTest() { Test t = new Test(TestDecimal); } public void TestDecimal(decimal arg) { } public void TestLong(long arg) { } } When calling the Test constructor with

Why does IList<>.Reverse() not work like List<>().Reverse

徘徊边缘 提交于 2019-12-01 15:30:08
I have problem with List<T>.Reverse() and Reverse(this IEnumerable<TSource> source) . Look to the code: // Part 1 List<int> list = new List<int> { 1, 2, 3 }; foreach (int x in list) Console.Write(x); Console.WriteLine(); list.Reverse(); foreach (int x in list) Console.Write(x); Console.WriteLine(); list.Reverse(); // Part2 IList<int> ilist = list; foreach (int x in list) Console.Write(x); Console.WriteLine(); ilist.Reverse(); foreach (int x in ilist) Console.Write(x); Console.WriteLine(); ilist.Reverse(); My result: 123 321 123 123 because Reverse() -Part1 is List<T>.Reverse() , Reverse()

How can there be ambiguity between a property getter and a method with one argument?

流过昼夜 提交于 2019-12-01 15:21:38
I can't believe I've never come across this before, but why am I getting a compiler error for this code? public class Main { public Main() { var ambiguous = new FooBar(1); var isConfused = ambiguous.IsValid; // this call is ambiguous } } public class FooBar { public int DefaultId { get; set; } public FooBar(int defaultId) { DefaultId = defaultId; } public bool IsValid { get { return DefaultId == 0; } } public bool IsValid(int id) { return (id == 0); } } Here is the error message: Ambiguity between 'FooBar.IsValid' and 'FooBar.IsValid(int)' Why is this ambiguous? I'm thinking there are two

How can there be ambiguity between a property getter and a method with one argument?

只愿长相守 提交于 2019-12-01 15:03:24
问题 I can't believe I've never come across this before, but why am I getting a compiler error for this code? public class Main { public Main() { var ambiguous = new FooBar(1); var isConfused = ambiguous.IsValid; // this call is ambiguous } } public class FooBar { public int DefaultId { get; set; } public FooBar(int defaultId) { DefaultId = defaultId; } public bool IsValid { get { return DefaultId == 0; } } public bool IsValid(int id) { return (id == 0); } } Here is the error message: Ambiguity

Disambiguating calls to functions taking std::functions

自古美人都是妖i 提交于 2019-12-01 04:34:13
问题 The code below doesn't compile on gcc 4.5 because the call to foo is ambiguous. What is the correct way to disambiguate it? #include <iostream> #include <functional> using namespace std; void foo(std::function<void(int, int)> t) { t(1, 2); } void foo(std::function<void(int)> t) { t(2); } int main() { foo([](int a, int b){ cout << "a: " << a << " b: " << b << endl;}); } 回答1: The best way is to explicitly create a std::function object of the correct type then pass that object to the function:

Why does C# allow ambiguous function calls through optional arguments?

做~自己de王妃 提交于 2019-12-01 02:20:52
I came across this today, and I am surprised that I haven't noticed it before. Given a simple C# program similar to the following: public class Program { public static void Main(string[] args) { Method(); // Called the method with no arguments. Method("a string"); // Called the method with a string. Console.ReadLine(); } public static void Method() { Console.WriteLine("Called the method with no arguments."); } public static void Method(string aString = "a string") { Console.WriteLine("Called the method with a string."); } } You get the output shown in the comments for each method call. I

How to handle a class name conflict when porting old code?

北城余情 提交于 2019-11-30 21:18:34
I'm trying to port an old library (that doesn't use namespaces as far as I can tell) to modern compilers. One of my targets can't tell the difference between System::TObject and ::TObject (without a namespace). System::TObject is native to the compiler. I've tried a using directive, i.e. using ::TObject; But that doesn't do it. The obvious solution is to wrap all the original library in a namespace and then calling it by name- that should avoid the ambiguity. But is that the wisest solution? Is there any other solution? Adding a namespace would require changing a bunch of files and I don't

Equivalent implicit operators: why are they legal?

蓝咒 提交于 2019-11-30 21:02:34
Update! See my dissection of a portion of the C# spec below; I think I must be missing something, because to me it looks like the behavior I'm describing in this question actually violates the spec. Update 2! OK, upon further reflection, and based on some comments, I think I now understand what's going on. The words "source type" in the spec refer to the type being converted from -- i.e., Type2 in my example below -- which simply means that the compiler is able to narrow the candidates down to the two operators defined (since Type2 is the source type for both). However, it cannot narrow the

Why Oracle 10g doesn't complain about column ambiguity?

时间秒杀一切 提交于 2019-11-30 08:15:15
问题 I'm using Oracle 10g (XE 10.2.0.1.0), and find a behavior that I don't understand: select * from employees manager join employees worker on MANAGER.EMPLOYEE_ID = WORKER.MANAGER_ID join departments on DEPARTMENTS.manager_id = 108 where department_id = 100 ; The problem is I think Oracle should have complain about the ambiguity of department_id in the where clause, since it's a column in both the table employees and departments . The fact is in Oracle 10g, it doesn't, and the result shows that