overload-resolution

Template partial ordering - why does partial deduction succeed here

£可爱£侵袭症+ 提交于 2019-11-26 22:33:33
Consider the following simple (to the extent that template questions ever are) example: #include <iostream> template <typename T> struct identity; template <> struct identity<int> { using type = int; }; template<typename T> void bar(T, T ) { std::cout << "a\n"; } template<typename T> void bar(T, typename identity<T>::type) { std::cout << "b\n"; } int main () { bar(0, 0); } Both clang and gcc print "a" there. According to the rules in [temp.deduct.partial] and [temp.func.order], to determine partial ordering, we need to synthesize some unique types. So we have two attempts at deduction: +---+--

Overload resolution and virtual methods

好久不见. 提交于 2019-11-26 22:33:12
Consider the following code (it's a little long, but hopefully you can follow): class A { } class B : A { } class C { public virtual void Foo(B b) { Console.WriteLine("base.Foo(B)"); } } class D: C { public override void Foo(B b) { Console.WriteLine("Foo(B)"); } public void Foo(A a) { Console.WriteLine("Foo(A)"); } } class Program { public static void Main() { B b = new B(); D d = new D (); d.Foo(b); } } If you think the output of this program is "Foo(B)" then you'd be in the same boat as me: completely wrong! In fact, it outputs "Foo(A)" If I remove the virtual method from the C class, then

Overload resolution issue for generic method with constraints

风格不统一 提交于 2019-11-26 22:04:04
问题 A code sample: interface IFoo { } class FooImpl : IFoo { } static void Bar<T>(IEnumerable<T> value) where T : IFoo { } static void Bar<T>(T source) where T : IFoo { } Can anybody explain, why this method call: var value = new FooImpl[0]; Bar(value); targets Bar<T>(T source) (and, hence, doesn't compile)? Does compiler take into account type parameter constraints at all, when resolving overloads? UPD . To avoid confusion with arrays. This happens with any implementation of IEnumerable<T> , e.g

Overloaded method-group argument confuses overload resolution?

核能气质少年 提交于 2019-11-26 20:44:05
The following call to the overloaded Enumerable.Select method: var itemOnlyOneTuples = "test".Select<char, Tuple<char>>(Tuple.Create); fails with an ambiguity error (namespaces removed for clarity): The call is ambiguous between the following methods or properties: 'Enumerable.Select<char,Tuple<char>> (IEnumerable<char>,Func<char,Tuple<char>>)' and 'Enumerable.Select<char,Tuple<char>> (IEnumerable<char>, Func<char,int,Tuple<char>>)' I can certainly understand why not specifying the type-arguments explicitly would result in an ambiguity (both the overloads would apply), but I don't see one

This case of template function overloading eludes my understanding

筅森魡賤 提交于 2019-11-26 20:08:42
问题 #include <iostream> template<typename T> struct identity { typedef T type; }; template<typename T> void bar(T) { std::cout << "a" << std::endl; } template<typename T> void bar(typename identity<T>::type) { std::cout << "b" << std::endl; } int main () { bar(5); // prints "a" because of template deduction rules bar<int>(5); // prints "b" because of ...? return EXIT_SUCCESS; } I expected bar<int>(5) to result in an ambiguity, at the very least. What crazy rule about template function overload

Overload resolution and arrays: which function should be called?

南楼画角 提交于 2019-11-26 19:43:18
问题 Consider the following program: #include <cstddef> #include <cstdio> void f(char const*&&) { std::puts("char const*&&"); } // (1) void f(char const* const&) { std::puts("char const* const&"); } // (2) template <std::size_t N> void f(char const (&)[N]) { std::puts("char const(&)[N]"); } // (3) int main() { const char data[] = "a"; f(data); } Which f should be called? Why? The latest released versions of three compilers disagree on the answer to this question: (1) is called when the program is

C++ template functions overload resolution

十年热恋 提交于 2019-11-26 18:16:37
问题 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

std::function fails to distinguish overloaded functions

天大地大妈咪最大 提交于 2019-11-26 17:46:16
问题 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.

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

▼魔方 西西 提交于 2019-11-26 17:28:21
问题 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

Method overload resolution with regards to generics and IEnumerable

回眸只為那壹抹淺笑 提交于 2019-11-26 16:49:42
问题 I noticed this the other day, say you have two overloaded methods: public void Print<T>(IEnumerable<T> items) { Console.WriteLine("IEnumerable T"); } public void Print<T>(T item) { Console.WriteLine("Single T"); } This code: public void TestMethod() { var persons = new[] { new Person { Name = "Yan", Age = 28 }, new Person { Name = "Yinan", Age = 28 } }; Print(persons); Print(persons.ToList()); } prints: Single T Single T Why are Person[] and List<Person> better matched to T than they are to