overload-resolution

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 can't I use std::get<0> in std::transform?

倖福魔咒の 提交于 2019-12-01 16:11:04
问题 In trying to compile the following code which would copy a map s keys to a vector : map<string, string> mss; vector<string> vs; transform(mss.begin(), mss.end(), back_inserter(vs), get<0>); VS2013 can't distinguish which get is intended but this simpler usage works just fine: vs.push_back(get<0>(*mss.begin())); Specifying get<0, string, string> didn't help. What am I missing? 回答1: There are many overloads of std::get, where, in addition, each is a function template itself, therefore the

Why can't I use std::get<0> in std::transform?

故事扮演 提交于 2019-12-01 16:03:59
In trying to compile the following code which would copy a map s keys to a vector : map<string, string> mss; vector<string> vs; transform(mss.begin(), mss.end(), back_inserter(vs), get<0>); VS2013 can't distinguish which get is intended but this simpler usage works just fine: vs.push_back(get<0>(*mss.begin())); Specifying get<0, string, string> didn't help. What am I missing? There are many overloads of std::get , where, in addition, each is a function template itself, therefore the compiler can't tell which one you want at the call site where you request for the address of one of them. If you

Breaking change in method overload resolution in C# 6 - explanation?

ぐ巨炮叔叔 提交于 2019-12-01 15:35:49
We've recently moved from VS2013 to VS2017 in our company. After the upgrade our codebase would no longer build. We would get the following error: The call is ambiguous between the following methods or properties: 'IRepository<T>.Get(object, params Expression<Func<T, object>>[])' and 'IRepository<T>.Get(object, params string[])' Here is the call itself: this.mainRepository.Get(newEntity.Id); ...and the interface definition: public interface IRepository<T> where T : class { T Get(object id, params Expression<Func<T, object>>[] includeExprs); T Get(object id, params string[] includeExprs); } I

Lambda conversions with unclear return type and overload resolution [duplicate]

随声附和 提交于 2019-12-01 14:30:33
问题 This question already has an answer here : Peculiar overload resolution with while (true) (1 answer) Closed 5 years ago . If I have a lambda such as () => { throw new Exception(); } , it's unclear whether it has a return type or not. Because of this, it can be (implicitly) converted to both Action and Func<object> (or any other Func<T> ). This is because, according to §6.5 Anonymous function conversions of the C# 4 spec: [A] delegate type D is compatible with an anonymous function F provided:

Overload resolution, templates and inheritance

北慕城南 提交于 2019-12-01 13:46:12
问题 #include <iostream> struct A {}; struct B : public A {}; template<typename T> void foo(const T &x) { std::cout << "Called template" << std::endl; } void foo(const A &a) { std::cout << "Called A" << std::endl; } int main() { foo(A()); foo(B()); return 0; } This prints: Called A Called template I was under the impression that a suitable non-template function would always be chosen over a template function. Can someone explain to me the resolution steps that lead to this somewhat surprising

Breaking change in method overload resolution in C# 6 - explanation?

半腔热情 提交于 2019-12-01 13:41:47
问题 We've recently moved from VS2013 to VS2017 in our company. After the upgrade our codebase would no longer build. We would get the following error: The call is ambiguous between the following methods or properties: 'IRepository<T>.Get(object, params Expression<Func<T, object>>[])' and 'IRepository<T>.Get(object, params string[])' Here is the call itself: this.mainRepository.Get(newEntity.Id); ...and the interface definition: public interface IRepository<T> where T : class { T Get(object id,

Vector initialization with double curly braces: std::string vs int

爱⌒轻易说出口 提交于 2019-12-01 07:39:43
In an answer to this question: Initializing vector<string> with double curly braces it is shown that vector<string> v = {{"a", "b"}}; will call the std::vector constructor with an initializer_list with one element . So the first (and only) element in the vector will be constructed from {"a", "b"} . This leads to undefined behavior, but that is beyond the point here. What I have found is that std::vector<int> v = {{2, 3}}; Will call std::vector constructor with an initializer_list of two elements . Why is the reason for this difference in behavior? The rules for list initialization of class

how do I call an inline friend function with the same name as a member function?

喜夏-厌秋 提交于 2019-12-01 07:13:47
问题 As described here C++11 style SFINAE and function visibility on template instantiation class member functions overshadow free functions. Using a fully qualified name usually works, however I am having a hard time with friend functions of other classes which are declared in-line. Consider the following example: namespace N { struct C { friend int f(const C& c) { return 1; } friend int g(const C& c) { return 2; } }; struct D { void f() { g(C{}); // ADL finds this ::N::f(C{}); // not found

C++ templated function overloading rules

 ̄綄美尐妖づ 提交于 2019-12-01 05:51:18
When overloading a templated function, how should the compiler chose which version of the function to call if it has the option to either: Call a templated version of the function (such as func<T>(foo) ). Call an overloaded version of the function which is not itself templated but where the type of the parameter being passed to the function inherits from the type specified in the overloaded function template. Consider the following C++ code: #include <stdio.h> struct Parent {}; struct Child : public Parent {}; template <typename T> void func(T) { printf("func(T)\n"); } void func(Parent) {