in c++ ? (calling method on a temporary object)
struct myStruct {
void method(){}
};
int main() {
myStruct{}.method();
return 0;
}
my analyze:
myStruct is temp obj and an rvalue so you can't take address of it to pass through methods that have this pointer which just refers to an lvalue kind
Right. A temp object is called temporary since it doesn't have an address. Referencing is possible, however.
yeah referencing is allowed but with an const lavlue reference my question is impilcit this is a pointer not a reference so you bind an rvalue it your example would like this void foo(const int* r) { std::cout << r; } int main() { foo(10); // not allowed }
so why is not like this and can accept (its temp object)?
10 is a value not an address!
but "this" is a pointer so how can it take a value ? (myStruct{})
Your temp object myStruct{} has the implicit this pointer but the object is not stored in memory to get an actual address to be able to take and use it.
I don't think temporary doesn't have an address (can't find such a rule), it's more like explicitly taking this address doesn't make sense in most cases (so the compilers issue a warning for doing that)
Not sure where temp objects are temporarily stored. Probably standard hasn't mandated using memory (parts developers are dealing with them mostly, such as heap, stack or static) for them. In what code does a compiler issue a warning when you take the address of a temp object?
Value categories are expressions, when you talk about those you're actually talking about the process of code being generated by compiler. They're not as concrete as the underlying machine instructions, that's why you have entities on which addressof operator fails, like in your case if you do &(mystruct{})
Memory-wise it doesn't really matter if the object is temporary or if it's a normal named variable. Both can be stored on the stack or go directly to registers, depending on what compiler decides the best in the specific case, so there shouldn't be differences. What code? Here was an example https://stackoverflow.com/questions/2280688/taking-the-address-of-a-temporary-object
This exactly, for instance literals are usually stored in the data section of binary generated. But addressof operator can't be used on them.
I agree to some extent that's why I talked about standard not implementations and about parts of memory and not registers. What is clear is taking the address of a temp object is not allowed. Both MSVC and GCC issue an error for that line of code in that link.
based on your speech, why can't golang handle this but c++ can ? package main type myStruct struct {} func (ms *myStruct) function() {} func main () { myStruct{}.function() } ./main.go:11:13: cannot call pointer method function on myStruct
Well C++ and go have different grammar and implementation. Also I don't know much about go to even answer properly why it can't do that.
I guess can explain why I said that value categories are expressions and they're not as concrete as underlying machine instructions. I quickly wrote this, https://godbolt.org/z/3WsfvxKE5 You can see here that on x86-64 there's no difference between the different functions their calls and how values are passed to them when. It doesn't matter in this case as the notion of temporaries, rvalue, lvalue references dilute at this state. For it, simply they are all on stack, and their pointers can be passed for successful member function call.
Обсуждают сегодня