overload-resolution

What's the rank of implicitly conversion for copy-list-initialization

偶尔善良 提交于 2020-06-29 03:39:56
问题 #include <iostream> struct A{ A(int){ } }; struct B{ B() = default; B(A){ } B(B const&){} B(B&&){} }; int main(){ B b({0}); } For the given codes, the candidate functions are: #1 B::B(A) #2 B::B(const B&) #3 B::B(B&&) According to the standard, for #1, the object of type A is copy-list-initialized by {0} as A a = {0} , A::A(int) is considered for the initialization, so only the standard conversion within #1. For #2, it's an initialization of a reference form braced-init-list which is the

Overload Resolution differs between compilers

不问归期 提交于 2020-06-27 07:35:51
问题 I have constructed the following minimal example of my problem: #include <iostream> struct Foo { Foo() { std::cout << "default" << std::endl; } Foo(Foo& f2) { std::cout << "non-const" << std::endl; } Foo(const Foo& f2) { std::cout << "const" << std::endl; } }; int main() { std::pair<Foo, int> foop0(Foo(), 1); std::cout << std::endl; std::pair<const Foo, int>foop1(foop0); } On my Ubuntu machine g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 will print out the following: $ g++ -std=c++14 test.cpp -o

Overload Resolution differs between compilers

大兔子大兔子 提交于 2020-06-27 07:35:48
问题 I have constructed the following minimal example of my problem: #include <iostream> struct Foo { Foo() { std::cout << "default" << std::endl; } Foo(Foo& f2) { std::cout << "non-const" << std::endl; } Foo(const Foo& f2) { std::cout << "const" << std::endl; } }; int main() { std::pair<Foo, int> foop0(Foo(), 1); std::cout << std::endl; std::pair<const Foo, int>foop1(foop0); } On my Ubuntu machine g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 will print out the following: $ g++ -std=c++14 test.cpp -o

Swift: generic overloads, definition of “more specialized”

只谈情不闲聊 提交于 2020-06-26 14:16:32
问题 In the below example, why is the foo(f) call ambiguous? I understand that the second overload could also apply with P == () , but why isn't the first one considered more specialized, and therefore a better match? func foo<R>(_ f: () -> R) { print("r") } func foo<P, R>(_ f: (P) -> R) { print("pr") } let f: () -> Int = { 42 } foo(f) // "Ambiguous use of 'foo'" 回答1: I'd say your problem is that you don't explicitely tell the compiler that P == () try the following code in a playground : Void

Swift: Specialize method of generic class for function types

≯℡__Kan透↙ 提交于 2020-06-25 18:18:34
问题 For generic free functions I can use overloading, to essentially specialize the function for function types, like this: func foo<T>(_ t: T.Type) { print("T is unknown") } func foo<P>(_ t: ((P) -> Void).Type) { print("T is a function with one parameter") } let f: (String) -> Void = { print($0) } foo(type(of: f)) // prints "T is a function with one parameter" Note the second version of foo() is not protocol-constrained, mainly because as far as I know, we can't make function types conform to

Overload Resolution/Ambiguity in name lookup(which one)

谁都会走 提交于 2020-02-15 07:56:08
问题 $7.3.3/14 (C++03) struct A { int x(); }; struct B : A { }; struct C : A { using A::x; int x(int); }; struct D : B, C { using C::x; int x(double); }; int f(D* d) { return d->x(); // ambiguous: B::x or C::x } The comment in the code in 'f' indicates that one can expect ambiguity between 'B::x' or 'C::x'. However, on compiling with g++(ideone) or Comeau the errors are slightly different. These errors instead of indicating ambiguity in B::x or C::x indicate the fact that A is an ambiguous base of

Overload Resolution/Ambiguity in name lookup(which one)

倖福魔咒の 提交于 2020-02-15 07:55:55
问题 $7.3.3/14 (C++03) struct A { int x(); }; struct B : A { }; struct C : A { using A::x; int x(int); }; struct D : B, C { using C::x; int x(double); }; int f(D* d) { return d->x(); // ambiguous: B::x or C::x } The comment in the code in 'f' indicates that one can expect ambiguity between 'B::x' or 'C::x'. However, on compiling with g++(ideone) or Comeau the errors are slightly different. These errors instead of indicating ambiguity in B::x or C::x indicate the fact that A is an ambiguous base of

Call a functor with a specific function from an overload set

喜欢而已 提交于 2020-01-23 09:10:40
问题 Context In mathematics-related context, I'd like to define functors working on <cmath> functions. For the purpose of this question, we will be using std::invoke as our functor. This is ill-formed (live demo): std::invoke(std::sin, 0.0); (g++-8.1) error: no matching function for call to 'invoke(<unresolved overloaded function type>, double)' Indeed, std::sin is an overload set and the compiler lacks the type information to choose one of those functions. Question How could I name a specific

Call a functor with a specific function from an overload set

﹥>﹥吖頭↗ 提交于 2020-01-23 09:09:07
问题 Context In mathematics-related context, I'd like to define functors working on <cmath> functions. For the purpose of this question, we will be using std::invoke as our functor. This is ill-formed (live demo): std::invoke(std::sin, 0.0); (g++-8.1) error: no matching function for call to 'invoke(<unresolved overloaded function type>, double)' Indeed, std::sin is an overload set and the compiler lacks the type information to choose one of those functions. Question How could I name a specific