Hello, I have two questions regarding the proper use of boost::shared_ptr; both actually arose after reading http://www.boost.org/libs/smart_ptr/example/shared_ptr_example.cpp 1. In order for some class to work with std::set, you have to supply an operator with "less than" semantics. Why are you instead declaring a "greater than" operator? 2. In your examples, sometimes you're passing a shared_ptr by value, and sometimes by reference-to-const. Which is the better practice? Usually, common pointers are passed by value, but since in shared_ptr a reference counting mechanism keeps track of the copies, maybe it's better to pass it by reference whenever possible? Thanks in advance, Matthias Kaeppler -- Matthias Kaeppler | Tel: +49 631 3405805 Gerhart-Hauptmann-Str. 16a | Mob: +49 176 20108693 D-67663 Kaiserslautern | E-Mail: matthias at finitestate dot org
Matthias Kaeppler wrote:
Hello,
I have two questions regarding the proper use of boost::shared_ptr; both actually arose after reading http://www.boost.org/libs/smart_ptr/example/shared_ptr_example.cpp
2. In your examples, sometimes you're passing a shared_ptr by value, and sometimes by reference-to-const. Which is the better practice? Usually, common pointers are passed by value, but since in shared_ptr a reference counting mechanism keeps track of the copies, maybe it's better to pass it by reference whenever possible?
To sum up the costs (assuming raw pointer + count pointer impl): by ref: address-of push dword /de-ref per usage *on top of shared_ptr access*/ by val: de-ref incr push 2 dwords /normal usage/ de-ref decr As all of these are trivial CPU operations, both ways are very cheap, but since trivial usages will often be inlined, and complex usages in inner loops can be amortized (I've always wanted to say that :-) (the result of the reference de-ref or shared_ptr de-ref can be kept in a register), there can be /no/ difference in cost. So you're left with taste. Personally, I think a reference to a pointer is _always_ pretty ugly (but occasionally useful).
On 10/13/05 4:16 AM, "Matthias Kaeppler"
I have two questions regarding the proper use of boost::shared_ptr; both actually arose after reading http://www.boost.org/libs/smart_ptr/example/shared_ptr_example.cpp
1. In order for some class to work with std::set, you have to supply an operator with "less than" semantics. Why are you instead declaring a "greater than" operator? [TRUNCATE question 2]
The comparison object for an associative container can be any function (object) that can provide an ordering for two objects/values. It doesn't have to be "less than". If it was like you said, then how could you store stuff in reverse order (without using reverse iterators), use types without relational operators (like complex numbers), use a specific field of a composite object, use a complex mixture of a composite object's fields, or use one criterion for one set and a different criterion for a different set when both sets use the same element type? -- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com
participants (3)
-
Daryle Walker
-
Matthias Kaeppler
-
Simon Buchan