move object into foo(Bar&&) function - c++11

I have a class Bar which implements a move constructor.
For an instance Bar b, if I call void fooRvalRef(Bar&&) using fooRvalRef(std::move(b)) then the move constructor of Bar is not called.
On the other hand for the function void foo(Bar), calling it using foo(std::move(b)) calls the move constructor of Bar.
Why is that?

std::move tries to turn the argument into an rvalue reference, that's all it does, it doesn't move the object. Since it succeeds in your fooRvalRef call, there is no need for a move constructor, it simply passes the rvalue reference to the rvalue-reference parameter.
In the case of foo(Bar) you're passing by value so, ordinarily, the copy constructor would be called. However in this case you have a move constructor and you are passing an rvalue reference, thanks to std::move, so the move constructor is called.

Related

When are variables implicitly initialized in C++?

As of C++11, when are local variables automatically initialized
If I write
std::vector<int> v;
And then use v somewhere in the function, is automatically v initialized?
When you create an object, it is initialized through a constructor. Every class can have an explicitly defined constructor, and, if no constructor is defined for a class, compiler automatically supplies a default constructor.
In the std::vector case, the defualt constructor of the class will be called, since you are not supplying any arguments.

What is an appropriate way to think of the 'self' keyword in Ruby?

Regarding the semantics of self, is it more appropriate to say:
self is a keyword that holds a reference to whatever the current receiver is.
self is the only receiver in Ruby. When you call a method or invoke a class definition, the value bound to self becomes a copy of the value bound to that object. By value, I mean unsigned long VALUE
In other words, is it ever accurate to say that myObj is the actual receiver of a message, or is it instead the case that self is the true receiver and a copy of the unsigned long VALUE variable value that is bound to the myObj variable name gets bound to the self variable name?
In the latter case, you could only ever say that self is receiving a message, but self and myObj happen to reference the same object. (self is always the current object)
In the former case, you could actually say that the current object (message receiver) changes and Ruby just updates self accordingly.
Whats going on under the hood?
The reason I am concerned with this apparently arbitrary distinction is because I am trying to figure out how Ruby "passes a message"
When you "pass a message" to an object, the message name is used to determine which method definition to execute. As arguments, that method definition receives both the rest of the message and the value to be used for self.
So from the point of view of the method code, self is just another parameter. But in the Ruby source, instead of being declared in the method's formal parameter list, it's implicit.
In that sense, it's certainly not true that only self can receive messages, because the invocant of a message only becomes self after the message is received, and continues to be so only within the body of the method responding to that message.
Javascript, like Ruby, has a predefined name (this instead of self), but other languages deal with the invocant differently. Some let you pick your own name for it, which may differ between method definitions. Maybe the invocant is just the first formal parameter, as in Perl and Python; the answer to your question may be clearer in those languages. Or, since the syntax to specify the recipient of a message is normally different from the syntax of a passed-in argument, there might likewise be a special syntax for declaring an invocant parameter, as in Go.
Ruby has the additional wrinkle that a bareword which doesn't refer to a local variable is interpreted as a message with no explicit invocant, and automatically sent to self. Other than that, self is just another local variable (which happens to be predefined and read-only).
Execution in Ruby always occurs within the scope of an instance of an object. self is a keyword which refers to the object which is the receiver of the current stack frame. At the top level, this is an instance of Object called main. Each time you pass a message to a receiver, the Ruby VM pushes a frame onto the stack which includes the receiver of that object, which it then exposes through the self keyword.
It's probably most accurate to think of self as "the receiver in the current stack frame". To that end, it's inaccurate to say that "self is the only receiver", as otherwise you could never change receivers!

What is the difference in calling a method explicitely and implicitely?

As per my understanding, in ruby we cannot call private method on self (calling private methods on self explicitly is not possible). If you call a method without any receiver, then it gets called on self, Then why cant we call a private method with self itself?
Sorry but I didn't really get what exactly is the difference in calling explicitly and implicitly(with self and without self).
I know that I may get down votes but still want to know. Can anyone tell me please.
At least in MRI, these concepts are identical. An explicit call is a public call, an implicit call is a private call.
The parser recognizes three kinds of method calls:
methods with an explicit receiver e.g. obj.foo(1)
methods with an implicit receiver e.g. foo(1)
methods with an implicit receiver and no arguments e.g. foo
The evaluator recognizes each of these as a different "call type". These types are (respectively):
CALL_PUBLIC
CALL_FCALL
CALL_VCALL
This call type is checked before making the call:
if (((noex & NOEX_MASK) & NOEX_PRIVATE) && scope == CALL_PUBLIC) {
return NOEX_PRIVATE;
}
I.e. if the method is private and the call type is public, don't call the method (protected calls work the same way but also check the receiver's class).
So whenever there is an explicit receiver (even if it's self inside an instance method definition), that call is a "public call".

How to use #selector? to call another method

I'm trying to call a method inside my viewDidLoad method. How can I do that? I did a fast search and I found #selector. I have never used it before, can any one give me a fast example.
You don't necessarily need selector to call a method, you can invoke it directly, e.g. to call someMethod method that takes no parameters on someObject you can do the following:
[anObject someMethod];
or to call someMethod on current object you can use self:
[self someMethod];
If you really need to call a method via selector use performSelector: method (or another of few methods of 'performSelector' family) of NSObject:
[anObject performSelector:#selector(someMethod)];

Do ruby Proc/lambda have a 'this' function like javascript closure ?

In javascript closure, 'this' reference to the object which actually make the function call.
Do ruby Proc/lambda have 'this' function too?
If not, what should I do to if I want 'this' in ruby? except passing the current object to Proc/lambda by parameters.
this is not part of the concept of a function or closure in general. A function is simply a thing you can call with arguments; what does "current object" has to do with it? this existing in all functions in JavaScript comes from the peculiar way that methods work in that language.
In JavaScript, all functions have a concept of this because in JavaScript, there is no separation between methods and functions. Any function could potentially be used as a method; you can add a method to an object simply by assigning a function as an attribute of the object. Furthermore, in JavaScript, a function does not have an explicit parameter for the current object (unlike e.g. Python); so how does a method have access to its object? When you run a method call expression, it will pass the object that you called it on as an implicit this parameter to the function. However, if you get the function out using the attribute and call it manually just like any other function, this will be the global object (or in strict mode, undefined).
In other words, in JavaScript when you get a method out of an object by attribute, it is an "unbound method" -- it does not know the object it came from; and conversely when you put a function into an object as a method by attribute, that function did not need to know the object to start with -- the object will be passed to it magically by the method call syntax at the time it is called. You can also artificially supply the this argument to a function by using the .call() or .apply() methods on the function (obj.foo(x) is equivalent to obj.foo.call(obj, x)).
In Ruby, there is complete separation between methods and anonymous functions. Anonymous functions, created using lambda, proc, or Proc.new, etc. are data, and can be stored in variables. They are called with different syntax (call or []) than methods. Methods are defined using def, and you can't get it as data by simply writing its name (that will call it). It is possible to get a method out of an object using the method method on an object, giving the method name, and it returns a Method object. You can use a Method object like a Proc object, e.g. you can call call on it; you can even convert it to a Proc with to_proc. Unlike JavaScript, there is a distinction between bound methods (class Method) an unbound methods (class UnboundMethod). When you get a method out of an object, it is bound -- it knows its object; you can unbind it and bind it to another object if you want.
Also, in Ruby, you can't just take a Proc and just attach it to an object and make it a method, because methods have syntax (e.g. #some_var) that are not valid in Proc. To add a method to an object, you would use instance_exec and put the method definition (def) in the block.
So in short, the concept of this in closures deals with a unique situation in JavaScript not found in most languages. And in particular, the issue does not come up in Ruby because Ruby's objects' methods are bound methods, and also one cannot arbitrarily insert closures as methods.
P.S. others have commented on capturing the self from where a closure is defined into the closure. However, that's not what this in JavaScript is about at all.
You can use self if you initialize the lambda or proc within a Ruby object. For example:
class Example
def name
"Example"
end
def test
lambda{ puts self.name}.call
end
end
example = Example.new
example.test # "Example"
For a more detailed explanation of Ruby's self see: http://sidtalk.wordpress.com/2008/10/06/what-exactly-is-ruby-self/.

Resources