overload-resolution

C# Method overload resolution not selecting concrete generic override

落爺英雄遲暮 提交于 2019-11-27 13:51:27
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 overflow .Execute(item); } } I ran into a StackOverlowException that I traced back to this call pattern where

C++ template functions overload resolution

本小妞迷上赌 提交于 2019-11-27 13:45:57
I have the following code: #include <iostream> template <typename T> void f (T) { std::cout << "f(T)" << std::endl; } template <typename T> void f (bool) { std::cout << "f(bool)" << std::endl; } int main ( ) { f(true); // #1 prints f(T) f<bool>(true); // #2 prints f(bool) } The #1 line calls f(T) , while #2 line calls f(bool) . Why does this happen? And what are the rules for selecting an overloaded template function? UPDATE I understood that in the first call compiler is just unable to deduce T while trying to call the second function, so the first is chosen. In the second call second

std::function fails to distinguish overloaded functions

左心房为你撑大大i 提交于 2019-11-27 08:49:28
I am trying to understand why std::function is not able to distinguish between overloaded functions. #include <functional> void add(int,int){} class A {}; void add (A, A){} int main(){ std::function <void(int, int)> func = add; } In the code shown above, function<void(int, int)> can match only one of these functions and yet it fails. Why is this so? I know I can work around this by using a lambda or a function pointer to the actual function and then storing the function pointer in function. But why does this fail? Isn't the context clear on which function I want to be chosen? Please help me

Single-Element-Vector Initialization in a Function Call

北慕城南 提交于 2019-11-27 07:47:21
问题 Consider the following example code: Example: void print(int n) { cout << "element print\n"; } void print(vector<int> vec) { cout << "vector print\n"; } int main() { /* call 1 */ print(2); /* call 2 */ print({2}); std::vector<int> v = {2}; /* call 3 */ print(v); /* call 4 */ print( std::vector<int>{2} ); return 0; } which generates the following output: element print element print vector print vector print Why the call to print function (call 2 in above example) is getting matched to function

Why aren't type constraints part of the method signature?

心已入冬 提交于 2019-11-27 05:51:19
问题 UPDATE: As of C# 7.3, this should no longer be an issue. From the release notes: When a method group contains some generic methods whose type arguments do not satisfy their constraints, these members are removed from the candidate set. Pre C# 7.3: So I read Eric Lippert's 'Constraints are not part of the signature', and now I understand that the spec specifies that type constraints are checked AFTER overload resolution, but I'm still not clear on why this MUST be the case. Below is Eric's

Generic extension method resolution fails

青春壹個敷衍的年華 提交于 2019-11-27 04:43:14
问题 The following program does not compile, because in the line with the error, the compiler chooses the method with a single T parameter as the resolution, which fails because the List<T> does not fit the generic constraints of a single T . The compiler does not recognize that there is another method that could be used. If I remove the single- T method, the compiler will correctly find the method for many objects. I've read two blog posts about generic method resolution, one from JonSkeet here

Does an lvalue argument prefer an lvalue reference parameter over a universal reference?

爱⌒轻易说出口 提交于 2019-11-27 03:12:08
问题 While playing with universal references, I came across this instance where clang and gcc disagree on overload resolution. #include <iostream> struct foo {}; template<typename T> void bar(T&) { std::cout << "void bar(T&)\n"; } template<typename T> void bar(T&&) { std::cout << "void bar(T&&)\n"; } int main() { foo f; bar(f); // ambiguous on gcc, ok on clang } gcc reports the call above is ambiguous. However, clang selects the T& overload and compiles successfully. Which compiler is wrong, and

Overloaded lambdas in C++ and differences between clang and gcc

女生的网名这么多〃 提交于 2019-11-27 01:34:03
I'm playing with a trick to overload lambdas in C++. Specifically: // For std::function #include <functional> // For std::string #include <string> // For std::cout #include <iostream> template <class... F> struct overload : F... { overload(F... f) : F(f)... {} }; template <class... F> auto make_overload(F... f) { return overload<F...>(f...); } int main() { std::function <int(int,int)> f = [](int x,int y) { return x+y; }; std::function <double(double,double)> g = [](double x,double y) { return x+y; }; std::function <std::string(std::string,std::string)> h = [](std::string x,std::string y) {

Why does a value of an enum with a fixed underlying type of char resolve to fct(int) instead of fct(char)?

爱⌒轻易说出口 提交于 2019-11-27 01:31:20
问题 This problem came up when answering this question about overload resolution with enums. While the case for long long was definitely a bug in MSVC2012NovCTP (according to the standard text and a test with gcc 4.7.1), I cannot figure out why the following behavior occurs: #include <iostream> enum charEnum : char { A = 'A' }; void fct(char) { std::cout << "fct(char)" << std::endl; } void fct(int) { std::cout << "fct(int)" << std::endl; } void fct(long long) { std::cout << "fct(long long)" << std

If the address of a function can not be resolved during deduction, is it SFINAE or a compiler error?

。_饼干妹妹 提交于 2019-11-27 00:44:49
问题 In C++0x SFINAE rules have been simplified such that any invalid expression or type that occurs in the "immediate context" of deduction does not result in a compiler error but rather in deduction failure (SFINAE). My question is this: If I take the address of an overloaded function and it can not be resolved, is that failure in the immediate-context of deduction? (i.e is it a hard error or SFINAE if it can not be resolved)? Here is some sample code: struct X { // template<class T> T* foo(T,T)