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

Digital Marketing Facebook Marketing API 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Facebook Marketing API related to Digital Marketing. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 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.

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.