overload-resolution

How does method overload resolution work (LINQ Where extension method)?

让人想犯罪 __ 提交于 2019-12-03 13:23:14
If I have a variable of type IQueryable<T> I have four extension methods for Where in namespace Systm.Linq available: public static IQueryable<T> Where<T>(this IQueryable<T> source, Expression<Func<T, bool>> predicate); public static IQueryable<T> Where<T>(this IQueryable<T> source, Expression<Func<T, int, bool>> predicate); public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate); public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, int, bool> predicate); (The last two because IQueryable<T> inherits from IEnumerable<T> .) If I use a

Trouble with const/non-const overload resolution

不羁岁月 提交于 2019-12-03 12:18:37
问题 I have a class that looks something like this: class ClassA { public: float Get(int num) const; protected: float& Get(int num); } Outside of the class, I call the Get() function. float foo = classAInstance.Get(i); I expect this to call the public version, but instead Visual Studio errors out: error C2248: 'ClassA::Get' : cannot access protected member declared in class 'ClassA' When commenting out the protected overload and removing all references to it, the code compiles. Why does the

Why is template constructor preferred to copy constructor?

余生长醉 提交于 2019-12-03 10:34:18
问题 #include <iostream> struct uct { uct() { std::cerr << "default" << std::endl; } uct(const uct &) { std::cerr << "copy" << std::endl; } uct( uct&&) { std::cerr << "move" << std::endl; } uct(const int &) { std::cerr << "int" << std::endl; } uct( int &&) { std::cerr << "int" << std::endl; } template <typename T> uct(T &&) { std::cerr << "template" << std::endl; } }; int main() { uct u1 ; // default uct u2( 5); // int uct u3(u1); // template, why? } coliru Template overload of the constructor

Overload resolution looking into namespaces

吃可爱长大的小学妹 提交于 2019-12-03 10:32:06
问题 The following code fails as expected, because no overload of get is found. Using std::get would solve the problem. #include <array> int main() { std::array<int, 2> ar{2,3}; auto r = get<0>(ar);//fails, get was not declared in this scope } However, introducing a templated version of get , even though it's not matching the function call, somehow makes the compiler use the std::get version: #include <array> template <typename T> void get(){}; int main() { std::array<int, 2> ar{2,3}; auto r = get

Java 8 generic function should be ambiguous, but failing in runtime

久未见 提交于 2019-12-03 10:01:28
问题 I'm trying to migrate Java 7 code to Java 8, so I've code similar to: package tests; import java.util.Arrays; import java.util.Map; public class Tests { private static interface ComparableMap<K,V> extends Map<K,V>, Comparable {} public static void main(String[] args) { func(getString()); } private static void func(Comparable...input){ System.out.println(Arrays.toString(input)); } private static void func(ComparableMap <?,?> m){ System.out.println(m); } private static <T extends Comparable> T

Why do primitive and user-defined types act differently when returned as 'const' from a function?

爱⌒轻易说出口 提交于 2019-12-03 06:29:03
问题 #include <iostream> using namespace std; template<typename T> void f(T&&) { cout << "f(T&&)" << endl; } template<typename T> void f(const T&&) { cout << "f(const T&&)" << endl; } struct A {}; const A g1() { return {}; } const int g2() { return {}; } int main() { f(g1()); // outputs "f(const T&&)" as expected. f(g2()); // outputs "f(T&&)" not as expected. } The issue description is embedded in the code. My compiler is clang 5.0 . I just wonder: Why does C++ treat built-in types and custom

List-initialization and failed overload resolution of initializer_list constructor

Deadly 提交于 2019-12-03 02:42:22
The below fails to compile with clang35 -std=c++11 : #include <iostream> #include <string> #include <initializer_list> class A { public: A(int, bool) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(int, double) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(std::initializer_list<int>) { std::cout << __PRETTY_FUNCTION__ << std::endl; } }; int main() { A a1 = {1, 1.0}; return 0; } with error init.cpp:15:14: error: type 'double' cannot be narrowed to 'int' in initializer list [-Wc++11-narrowing] A a1 = {1, 1.0}; ^~~ init.cpp:15:14: note: insert an explicit cast to silence this issue A

How is ambiguity determined in the overload resolution algorithm?

走远了吗. 提交于 2019-12-03 01:47:25
问题 I'm trying to understand the overloading resolution method. Why is this ambiguous: void func(double, int, int, double) {} void func(int, double, double, double) {} void main() { func(1, 2, 3, 4); } but this isn't? void func(int, int, int, double) {} void func(int, double, double, double) {} void main() { func(1, 2, 3, 4); } In the first case there are 2 exact parameters matches and 2 conversions against 1 exact match and 3 conversions, and in the second case there are 3 exact matches and 1

Java 8 generic function should be ambiguous, but failing in runtime

断了今生、忘了曾经 提交于 2019-12-03 00:28:03
I'm trying to migrate Java 7 code to Java 8, so I've code similar to: package tests; import java.util.Arrays; import java.util.Map; public class Tests { private static interface ComparableMap<K,V> extends Map<K,V>, Comparable {} public static void main(String[] args) { func(getString()); } private static void func(Comparable...input){ System.out.println(Arrays.toString(input)); } private static void func(ComparableMap <?,?> m){ System.out.println(m); } private static <T extends Comparable> T getString(){ return (T) "aaa"; } } In java 7 it working properly, in java 8 I'm getting: java.lang

In overload resolution, does selection of a function that uses the ambiguous conversion sequence necessarily result in the call being ill-formed?

那年仲夏 提交于 2019-12-02 22:02:30
The question arose while I was researching the answer to this SO question . Consider the following code: struct A{ operator char() const{ return 'a'; } operator int() const{ return 10; } }; struct B { void operator<< (int) { } }; int main() { A a; B b; b << a; } The conversion of a to int can be either via a.operator char() followed by an integral promotion, or a.operator int() followed by an identity conversion (i.e., no conversion at all). The standard says that (§13.3.3.1 [over.best.ics]/p10, footnote omitted, bolding mine; all quotes are from N3936): If several different sequences of