Похожие чаты

Hello 🙌🏻 i have a question why such thing is allowed

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

15 ответов

16 просмотров

Right. A temp object is called temporary since it doesn't have an address. Referencing is possible, however.

Mehrad- Автор вопроса
Azadi
Right. A temp object is called temporary since it ...

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 }

Mehrad- Автор вопроса
Mehrad
yeah referencing is allowed but with an const lavl...

so why is not like this and can accept (its temp object)?

Mehrad- Автор вопроса
Azadi
10 is a value not an address!

but "this" is a pointer so how can it take a value ? (myStruct{})

Mehrad
but "this" is a pointer so how can it take a value...

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.

Azadi
Right. A temp object is called temporary since 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)

Pavel
I don't think temporary doesn't have an address (c...

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{})

Azadi
Not sure where temp objects are temporarily stored...

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

Pavel
Memory-wise it doesn't really matter if the 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.

Pavel
Memory-wise it doesn't really matter if the object...

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.

Mehrad- Автор вопроса
Manav | avoid unnecessary messaging me
Value categories are expressions, when you talk ab...

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

Mehrad
based on your speech, why can't golang handle this...

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.

Mehrad
based on your speech, why can't golang handle this...

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.

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта