overloading

How does non idiomatic global operator overloading work?

半世苍凉 提交于 2019-12-20 02:09:21
问题 I want to understand the code from this answer type Mult = Mult with static member inline ($) (Mult, v1: 'a list) = fun (v2: 'b list) -> v1 |> List.collect (fun x -> v2 |> List.map (fun y -> (x, y))) : list<'a * 'b> static member inline ($) (Mult, v1:'a ) = fun (v2:'a) -> v1 * v2 :'a let inline (*) v1 v2 = (Mult $ v1) v2 F# can resolve overloaded members. (Because it doesn't support currying of members). So, I supposed, it should work for methods as well But it doesn't: type Mult = Mult with

Double Definition Error Despite Different Parameter Types

醉酒当歌 提交于 2019-12-20 02:08:48
问题 I'm receiving a double definition error on the following two methods: def apply[T](state: T, onRender: T => Graphic, onMouseEvent: (MouseEvent, T) => T): GraphicPanel = apply(state, onRender, onMouseEvent = Some(onMouseEvent)) and def apply[T](state: T, onRender: T => Graphic, onKeyEvent: (KeyEvent, T) => T): GraphicPanel = apply(state, onRender, onKeyEvent = Some(onKeyEvent)) which are both method overloads for the more general apply method with the signature: def apply[T](state: T, onRender

C++ Protected / Public overloads

▼魔方 西西 提交于 2019-12-20 01:35:12
问题 I have a class like this : class Foo { public: Foo() { for(int i = 0; i < 10; ++i) v.push_back(i); }; const vector<double>& V() const {return v;}; protected: vector<double>& V() {return v;}; private: vector<double> v; }; And then a piece of code like this : Foo foo; for(int i = 0; i < (int) foo.V().size(); ++i) cout << foo.V().at(i) << endl; However, the latter raises a compilation error saying the V() call is a protected method while i am just trying to read from it, not modify it. I have

Forcing std::vector overload instead of int overload on list with one element

五迷三道 提交于 2019-12-20 01:17:10
问题 Consider the code below: #include <iostream> #include <vector> void f(std::vector<int> v) {std::cout << __PRETTY_FUNCTION__ << std::endl;} void f(int n) {std::cout << __PRETTY_FUNCTION__ << std::endl;} int main() { f({42}); // the int overload is being picked up } Live on Coliru I was a bit surprised to realize that in this case the int overload is being picked up, i.e. the output of the program is: void f(int) with the warning warning: braces around scalar initializer [-Wbraced-scalar-init]

Using __add__ operator with multiple arguments in Python

喜欢而已 提交于 2019-12-20 00:44:11
问题 I am trying to add a class object with a number, but I'm confused on how to go about adding a class object with two numbers. For example, this is my hypothetical add class method: class A: def __add__(self, b): return something I know how to add this so far: object = A() print(object + 1) But, what if I want to add it like this? object = A() print(object + 1 + 2) Should I use *args for the add class method? 回答1: No, you can't use multiple arguments. Python executes each + operator separately,

In-class friend operator doesn't seem to participate in overload resolution

ⅰ亾dé卋堺 提交于 2019-12-19 19:57:23
问题 While writing a CRTP template that enables classes to provide overloads for operator+ based on template arguments, I found that an in-class friend operator doesn't seem to participate in overload resolution if none of it's arguments is of the type of the class it was defined in. Boiled down: enum class FooValueT{ zero, one, two }; class Foo{ FooValueT val_; public: Foo(FooValueT x) : val_(x){}; Foo& operator+=(Foo other){ val_ = (FooValueT)((int)val_ + (int)other.val_); return *this; } /

C++0x Error: overloading a function with std::shared_ptr to const argument is ambiguous

落花浮王杯 提交于 2019-12-19 19:54:06
问题 Suppose I have two unrelated classes A and B . I also have a class Bla that uses boost::shared_ptr like this: class Bla { public: void foo(boost::shared_ptr<const A>); void foo(boost::shared_ptr<const B>); } Notice the const . That's the important part which the original version of this question lacked. This compiles, and the following code works: Bla bla; boost::shared_ptr<A> a; bla.foo(a); However, if I switch from using boost::shared_ptr to using std::shared_ptr in the above examples, I

How to call the more specific method of overloading

此生再无相见时 提交于 2019-12-19 19:49:17
问题 Here is an example playground: protocol P { associatedtype T func getValue() -> T } class Foo: P { func getValue() -> String { return "hello" } } class Bar { func test<T: P>(_ o: T) { print("Generic", o.getValue()) } func test(_ o: Any) { print("Any") } } let foo = Foo() let bar = Bar() bar.test(foo) This outputs: Any . If I remove the Any version of test , the generic method is called. Class Foo conforms to protocol P , why does Swift not pick the generic method since it is more specific? Is

For an overloaded function, calling specialized version for parent and child instances

谁说我不能喝 提交于 2019-12-19 18:22:40
问题 I asked a question earlier but it turns out my problem was not properly modeled by my example. So here is my actual problem: I have class A , and class B inheriting from A , I have two functions foo(A&) and foo(B&) , I have a list of A* pointers, containing instances of A and B . How do I get to call foo(A&) for instances of A and foo(B&) for instances of B ? Constraints: I can modify A and B implementation, but not foo 's implementation. See below an example: #include <iostream> #include

Is the *only* purpose of a *function signature* (as opp. to type) to define duplicates in a potential overload set - or are there other purposes?

≯℡__Kan透↙ 提交于 2019-12-19 18:17:58
问题 Related to Why does casting a function to a function type that is identical except for return type fail?, I would like to understand, in a fuller way, the distinction between a function's type and a function's signature . For example, the type of a function must typically be considered when dealing with function pointers, and the type of the function includes the return type of that function. However, as noted in Mike Seymour's answer to the above-linked question, the signature of a function