This is not a redundant instruction.
The corresponding section of the first code fragment is equivalent: * P
While in the second it is equivalent: ** p
Due to the internal functions of shared_ptr, there is a second level of indirection. This is not something the optimizer can “fix”.
In any case, the difference is negligible.
EDIT:
Oops! My apologies, I am not reading your code correctly.
You pass shared_ptr BY REFERENCE to your code. This will pass it a "pointer" at the ASM level.
So, you pass a pointer to shared_ptr, and shared_ptr contains a pointer to your object.
Therefore, two levels of indirection.
Sorry for the confusion. :)
source share