Why can a T* be passed in register, but a unique_ptr cannot?

Digital Marketing Facebook Marketing API 3 years ago

855 1 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago
_x000D_ In C++, non-trivial destructor -> must have addressable storage (An illustration of a point in @MaximEgorushkin's answer using @harold's example in a comment.) If you compile: struct Foo { int bar; }; Foo test(Foo byval) { return byval; } you get: test(Foo): mov eax, edi ret i.e. the Foo object is passed to test in a register (edi) and also returned in a register (eax). but this is actually an exception to the rule. Generally, the C++ standard requires an object's storage to be addressable. Only if there's a trivial destructor [CITATION NEEDED] are we allowed the "as-if shortcut" of passing data through the registers. When the destructor is not trivial (like the std::unique_ptr example of OP's) - addressable storage means we have to put the object on the stack. This is true even if the destructor does not at all use the object's address! Thus if you compile: struct Foo2 { int bar; ~Foo2() { } }; Foo2 test(Foo2 byval) { return byval; } you get: test(Foo2): mov edx, DWORD PTR [rsi] mov rax, rdi mov DWORD PTR [rdi], edx ret with useless loading and storing.
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum


Q

What are the best SEO tools for a personal website? What website or tools should I use to build and...

What are the best SEO tools for a personal website? What website or tools should I use to build and...
Q

Why is keyword research important?

Why is keyword research important?
Q

What is SEO ?

What is SEO ?