overload-resolution

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

岁酱吖の 提交于 2019-11-28 06:49:40
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::endl; } int main() { fct('A'); fct(A); } Both MSVC2012NovCTP and gcc 4.7.1 agree on this output: fct

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

萝らか妹 提交于 2019-11-28 05:24:16
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); // lets not over-complicate things for now void foo(char); void foo(int); }; template<class U> struct

SFINAE: checking the existence of a function breaks when the overload is moved to other namespaces

爱⌒轻易说出口 提交于 2019-11-28 05:18:39
问题 I want to check for the existence of a function in a specific namespace using SFINAE. I have found SFINAE to test a free function from another namespace which does the job, but there are some things I don't understand. Currently I have this working code, straight from the linked question: // switch to 0 to test the other case #define ENABLE_FOO_BAR 1 namespace foo { #if ENABLE_FOO_BAR int bar(); #endif } namespace detail_overload { template<typename... Args> void bar(Args&&...); } namespace

C++0x const RValue reference as function parameter

岁酱吖の 提交于 2019-11-28 00:38:47
I am trying to understand why someone would write a function that takes a const rvalue reference . In the code example below what purpose is the const rvalue reference function (returning "3"). And why does overload resolution preference the const Rvalue above the const LValue reference function (returning "2"). #include <string> #include <vector> #include <iostream> std::vector<std::string> createVector() { return std::vector<std::string>(); } //takes movable rvalue void func(std::vector<std::string> &&p) { std::cout << "1"; } //takes const lvalue void func(const std::vector<std::string> &p)

Generic extension method resolution fails

独自空忆成欢 提交于 2019-11-28 00:19:18
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 and another from Eric Lippert here , but I could not find an explanation or a way to solve my problem.

This case of template function overloading eludes my understanding

一个人想着一个人 提交于 2019-11-27 20:03:56
#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 resolution is involved here? Barry Once we get our candidate functions set (both bar s), and then whittle

Overload resolution and arrays: which function should be called?

爷,独闯天下 提交于 2019-11-27 19:02:23
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 compiled using g++ 4.5.2 (2) is called when the program is compiled using Visual C++ 2010 SP1 (3) is

Determining which overload was selected

倖福魔咒の 提交于 2019-11-27 17:42:22
问题 Let's say I have some arbitrary complicated overloaded function: template <class T> void foo(T&& ); template <class T> void foo(T* ); void foo(int ); I want to know, for a given expression, which foo() gets called. For example, given some macro WHICH_OVERLOAD : using T = WHICH_OVERLOAD(foo, 0); // T is void(*)(int); using U = WHICH_OVERLOAD(foo, "hello"); // U is void(*)(const char*); // etc. I don't know where I would use such a thing - I'm just curious if it's possible. 回答1: Barry, sorry

Direct list initialization compiles successfully, but normal direct initialization fails, why?

你说的曾经没有我的故事 提交于 2019-11-27 15:15:50
问题 For example, code like this: struct A { A(int); }; struct B { B(A); }; int main() { B b{{0}}; // OK B c({0}); // error } The error messages are: f.cc: In function 'int main()': f.cc:7:9: error: call of overloaded 'B(<brace-enclosed initializer list>)' is ambiguous B c({0}); // error ^ f.cc:7:9: note: candidates are: f.cc:2:12: note: B::B(A) struct B { B(A); }; ^ f.cc:2:8: note: constexpr B::B(const B&) struct B { B(A); }; ^ f.cc:2:8: note: constexpr B::B(B&&) 回答1: As of the latest official

Method overload resolution with regards to generics and IEnumerable

纵饮孤独 提交于 2019-11-27 14:38:36
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 IEnumerable<T> in these cases? Thanks, UPDATE: Also, if you have another overload public void Print<T>