overload-resolution

Determining which overload was selected

二次信任 提交于 2019-11-29 03:45:17
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. Barry, sorry for the misunderstanding in my first answer. In the beginning I understood your question in a wrong way. 'T.C.

Overload resolution behaviour difference between GCC and clang (SFINAE)

人盡茶涼 提交于 2019-11-29 03:11:55
GCC accepts the following code: template <typename T> struct meta { typedef typename T::type type; }; struct S {}; template <typename T> typename meta<T>::type foo(T, S); int foo(int, int); int main() { foo(0, 0); } But clang rejects it with the following error: test.cpp:4:22: error: type 'int' cannot be used prior to '::' because it has no members typedef typename T::type type; ^ test.cpp:10:10: note: in instantiation of template class 'meta<int>' requested here typename meta<T>::type foo(T, S); ^ test.cpp:10:24: note: while substituting deduced template arguments into function template 'foo'

How does the operator overload resolution work within namespaces?

时光怂恿深爱的人放手 提交于 2019-11-29 02:56:51
问题 I found a strange behaviour of C++ resolution of operator-overloading, I can't explain myself. A pointer to some resource describing it would be just as nice as an answer. I have 2 translation units. In one (called util.cpp/h) I declare and define two operators (I omit the real implementations for readabilty, the problam occurs anyway): // util.h #ifndef GUARD_UTIL #define GUARD_UTIL #include <iostream> std::istream& operator>>(std::istream& is, const char* str); std::istream& operator>>(std:

Should this compile? Overload resolution and implicit conversions

拥有回忆 提交于 2019-11-29 01:20:48
This example seems to compile with VC10 and gcc (though my version of gcc is very old). EDIT: R. Martinho Fernandez tried this on gcc 4.7 and the behaviour is still the same. struct Base { operator double() const { return 0.0; } }; struct foo { foo(const char* c) {} }; struct Something : public Base { void operator[](const foo& f) {} }; int main() { Something d; d["32"]; return 0; } But clang complains: test4.cpp:19:6: error: use of overloaded operator '[]' is ambiguous (with operand types 'Something' and 'const char [3]') d["32"] ~^~~~~ test4.cpp:13:10: note: candidate function void operator[

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

旧城冷巷雨未停 提交于 2019-11-29 00:16:20
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&&) As of the latest official standard, C++14, your first initialization is not ambiguous. [over.match.list]: As no initializer-list

“Manual” signature overload resolution

左心房为你撑大大i 提交于 2019-11-28 21:56:56
I want to make a std::function like object that can handle storing more than one overload. Syntax sort of like this: my_function< int(double, int), double(double, double), char(int, int) > . Or, more explicitly: template<typename... Ts> struct type_list {}; template<typename... Signatures > struct my_function { std::tuple< std::function<Signatures>... > m_functions; typedef type_list< Signatures... > sig_list; template<typename... Args> typename pick_overload_signature< sig_list, type_list<Args...> >::return_value operator()( Args&&... args ) { return get<pick_overload_signature< sig_list,

Why can't the compiler tell the better conversion target in this overload resolution case? (covariance)

喜你入骨 提交于 2019-11-28 21:23:47
Understanding the C# Language Specification on overload resolution is clearly hard, and now I am wondering why this simple case fails: void Method(Func<string> f) { } void Method(Func<object> f) { } void Call() { Method(() => { throw new NotSupportedException(); }); } This gives compile-time error CS0121, The call is ambiguous between the following methods or properties: followed by my two Method function members (overloads). What I would have expected was that Func<string> was a better conversion target than Func<object> , and then the first overload should be used. Since .NET 4 and C# 4

List-initialization priority from object of same type

我怕爱的太早我们不能终老 提交于 2019-11-28 10:59:31
问题 #include <iostream> #include <initializer_list> using namespace std; struct CL { CL(){} CL (std::initializer_list<CL>){cout<<1;} CL (const CL&){cout<<2;} }; int main() { CL cl1; CL cl2 {cl1}; //prints 21 } Here is CL struct with copy constructor and initializer-list constructor. I think only copy constructor must be called here, because according to C++ 14 Standard, 8.5.4/3 List-initialization of an object or reference of type T is defined as follows: — If T is a class type and the

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

自闭症网瘾萝莉.ら 提交于 2019-11-28 09:47:39
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 why? Edit: Tested the same code on VS2013 Preview, and it agrees with clang; except Intellisense, which

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

半城伤御伤魂 提交于 2019-11-28 09:18:36
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 example: static void Foo<T>(T t) where T : Reptile { } static void Foo(Animal animal) { } static void