temp_ptr<> - preventing use as a member

I'm trying to see if I can replace all raw pointers with suitable smart pointers that clearly describe the pointer's lifetime, sharing, etc. For example, a pointer passed into a function, that is only valid for the lifetime of the function call. A temp_ptr or callstack_ptr or ... some better name. So this pointer would need a copy constructor so it can be passed along into a function (I don't think requiring a ref to the pointer would be good; I'd rather pass by value). It would also need construction from a raw pointer, but to avoid having a temp_ptr being kept beyond the function call, it would not have assignment (from another temp_ptr). To prevent constructing one via the copy constructor that will be kept around, I can define a custom new operator but keep it private. The only thing, I think, that I can't prevent, is construction of a new wrapper struct that has a temp_ptr as a member (which is copy constructed in wrapper's constructor). Can anyone think of a way to prevent that? I think anyone that goes out of their way to make a wrapper struct gets what they deserve, so I don't really *need* to prevent all bad uses. But I can aslo imagine someone just using it improperly inside their class due to misunderstanding or whatever. And thoughts on the goal of specific smart pointers for all occasions? Tony

on Sun Nov 13 2011, Gottlob Frege <gottlobfrege-AT-gmail.com> wrote:
The only thing, I think, that I can't prevent, is construction of a new wrapper struct that has a temp_ptr as a member (which is copy constructed in wrapper's constructor). Can anyone think of a way to prevent that?
I'm pretty sure there's no way to do that.
I think anyone that goes out of their way to make a wrapper struct gets what they deserve, so I don't really *need* to prevent all bad uses. But I can aslo imagine someone just using it improperly inside their class due to misunderstanding or whatever.
And thoughts on the goal of specific smart pointers for all occasions?
Sounds interesting; I'd like to know how it turns out for you in practice. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

On Sun, Nov 13, 2011 at 8:52 PM, Dave Abrahams <dave@boostpro.com> wrote:
on Sun Nov 13 2011, Gottlob Frege <gottlobfrege-AT-gmail.com> wrote:
The only thing, I think, that I can't prevent, is construction of a new wrapper struct that has a temp_ptr as a member (which is copy constructed in wrapper's constructor). Can anyone think of a way to prevent that?
I'm pretty sure there's no way to do that.
Thinking about it more, I realize that *in debug* I could probably write some strange code that detects whether the copies are going into objects on the stack or not. Not very portable to say the least.
I think anyone that goes out of their way to make a wrapper struct gets what they deserve, so I don't really *need* to prevent all bad uses. But I can aslo imagine someone just using it improperly inside their class due to misunderstanding or whatever.
And thoughts on the goal of specific smart pointers for all occasions?
Sounds interesting; I'd like to know how it turns out for you in practice.
I wish I had 6 months to try it out and now had answers. People are requesting guidance *now*. We'll see...
-- Dave Abrahams BoostPro Computing http://www.boostpro.com
Tony

on Thu Nov 17 2011, Gottlob Frege <gottlobfrege-AT-gmail.com> wrote:
On Sun, Nov 13, 2011 at 8:52 PM, Dave Abrahams <dave@boostpro.com> wrote:
on Sun Nov 13 2011, Gottlob Frege <gottlobfrege-AT-gmail.com> wrote:
The only thing, I think, that I can't prevent, is construction of a new wrapper struct that has a temp_ptr as a member (which is copy constructed in wrapper's constructor). Can anyone think of a way to prevent that?
I'm pretty sure there's no way to do that.
Thinking about it more, I realize that *in debug* I could probably write some strange code that detects whether the copies are going into objects on the stack or not. Not very portable to say the least.
Oh, sure, in non-portable code at runtime, you can do it.
I think anyone that goes out of their way to make a wrapper struct gets what they deserve, so I don't really *need* to prevent all bad uses. But I can aslo imagine someone just using it improperly inside their class due to misunderstanding or whatever.
And thoughts on the goal of specific smart pointers for all occasions?
Sounds interesting; I'd like to know how it turns out for you in practice.
I wish I had 6 months to try it out and now had answers. People are requesting guidance *now*. We'll see...
FWIW, my instinct is that if you need that many pointers (or _ptrs) and you're worried that they're too liberal, then you should probably re-think your approach. Most code doesn't need to expose reference semantics; it can often be hidden behind well-tested and tightly-encapsulated library interfaces. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

Gottlob Frege wrote:
And thoughts on the goal of specific smart pointers for all occasions?
Hi, It happens in some of my projects : I use a kind of std::auto_ptr whenever I need to transmit an aggregation (typically a factory creates an element and returns such auto_ptr). Every raw ptr means 'simple handle'. shared_ptr are used only for aggregation in multithread environment. (This obviously does not cover all your need). My feelings now are that if you put smart pointers everywhere, this could be a little cumbersome to write and read. Use at least raw pointer to represent one of the cases of lifetime duration/use. When you got some very special usage, you might also use some kind of hungarian notation just for that case. That way the whole thing might need a fewer kinds of smart_ptr. This is just my point of view. Thank you for the temp_ptr idea. Best regards, Pierre Morcello -- View this message in context: http://boost.2283326.n4.nabble.com/temp-ptr-preventing-use-as-a-member-tp403... Sent from the Boost - Dev mailing list archive at Nabble.com.

AMDG On 11/13/2011 04:41 PM, Gottlob Frege wrote:
I'm trying to see if I can replace all raw pointers with suitable smart pointers that clearly describe the pointer's lifetime, sharing, etc.
For example, a pointer passed into a function, that is only valid for the lifetime of the function call. A temp_ptr or callstack_ptr or ... some better name.
This sounds a lot like scoped_ptr.
So this pointer would need a copy constructor so it can be passed along into a function (I don't think requiring a ref to the pointer would be good; I'd rather pass by value).
I don't think this is a good idea. You probably don't want the copy constructor to copy the pointee, which means that the destructor doesn't necessarily call delete... In Christ, Steven Watanabe

On Mon, Nov 14, 2011 at 5:07 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
On 11/13/2011 04:41 PM, Gottlob Frege wrote:
I'm trying to see if I can replace all raw pointers with suitable smart pointers that clearly describe the pointer's lifetime, sharing, etc.
For example, a pointer passed into a function, that is only valid for the lifetime of the function call. A temp_ptr or callstack_ptr or ... some better name.
This sounds a lot like scoped_ptr.
Sounds more like a ptr that doesn't own it's content...

On Mon, Nov 14, 2011 at 11:07 AM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
On 11/13/2011 04:41 PM, Gottlob Frege wrote:
I'm trying to see if I can replace all raw pointers with suitable smart pointers that clearly describe the pointer's lifetime, sharing, etc.
For example, a pointer passed into a function, that is only valid for the lifetime of the function call. A temp_ptr or callstack_ptr or ... some better name.
This sounds a lot like scoped_ptr.
So this pointer would need a copy constructor so it can be passed along into a function (I don't think requiring a ref to the pointer would be good; I'd rather pass by value).
I don't think this is a good idea. You probably don't want the copy constructor to copy the pointee, which means that the destructor doesn't necessarily call delete...
I don't want to call delete in this case. It really is just a dumb pointer. But when I hand it out, I don't want you to keep it. Every pointer in an API has an associated lifetime guarantee - which varies based on the situation, but typically that guarantee is only written in comments. Can it be made part of the code? For the "here is a pointer, it will be invalid at the end of the function call", in debug I could probably track outstanding copies and maybe null out the internal pointer (ie like a weak_ptr) and then assert if they are used beyond their lifetime. Or something like that.
In Christ, Steven Watanabe
Tony

Gottlob Frege wrote:
For example, a pointer passed into a function, that is only valid for the lifetime of the function call. A temp_ptr or callstack_ptr or ... some better name.
So this pointer would need a copy constructor so it can be passed along into a function (I don't think requiring a ref to the pointer would be good; I'd rather pass by value).
If you relax this restriction,...
To prevent constructing one via the copy constructor that will be kept around, I can define a custom new operator but keep it private.
...you can avoid the need to do that...
The only thing, I think, that I can't prevent, is construction of a new wrapper struct that has a temp_ptr as a member (which is copy constructed in wrapper's constructor). Can anyone think of a way to prevent that?
...and prevent users from doing that, by making the type noncopyable. IOW, the only permissible parameter type using temp_ptr would be reference or pointer (const or not). _____ Rob Stewart robert.stewart@sig.com Software Engineer using std::disclaimer; Dev Tools & Components Susquehanna International Group, LLP http://www.sig.com ________________________________ IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

On Tue, Nov 15, 2011 at 12:58 PM, Stewart, Robert <Robert.Stewart@sig.com> wrote:
Gottlob Frege wrote:
For example, a pointer passed into a function, that is only valid for the lifetime of the function call. A temp_ptr or callstack_ptr or ... some better name.
So this pointer would need a copy constructor so it can be passed along into a function (I don't think requiring a ref to the pointer would be good; I'd rather pass by value).
If you relax this restriction,...
To prevent constructing one via the copy constructor that will be kept around, I can define a custom new operator but keep it private.
...you can avoid the need to do that...
The only thing, I think, that I can't prevent, is construction of a new wrapper struct that has a temp_ptr as a member (which is copy constructed in wrapper's constructor). Can anyone think of a way to prevent that?
...and prevent users from doing that, by making the type noncopyable.
Yep. The tradeoff is int func(temp_ptr<Foo> & foo) not a big deal maybe, but it might be if my audience already is uncomfortable with references vs pointers vs smart-ptrs (ie ex-java). Throw a const or two in there and heads explode. Maybe the whole idea is a non-starter. But I do wonder what code with all smart-ptrs would look like. Of course if I had to templatize all my, say, image processing, because the pixel could be a temp_ptr<Pixel>, some_other_ptr<Pixel>,... instead of just Pixel *, that might be annoying... Tony

On Thu, Nov 17, 2011 at 1:37 AM, Gottlob Frege <gottlobfrege@gmail.com> wrote:
Yep. The tradeoff is int func(temp_ptr<Foo> & foo)
not a big deal maybe, but it might be if my audience already is uncomfortable with references vs pointers vs smart-ptrs (ie ex-java). Throw a const or two in there and heads explode. Maybe the whole idea is a non-starter. But I do wonder what code with all smart-ptrs would look like. Of course if I had to templatize all my, say, image processing, because the pixel could be a temp_ptr<Pixel>, some_other_ptr<Pixel>,... instead of just Pixel *, that might be annoying...
Tony
To clarify, I want to see if it is possible to reasonably avoid pointers (and references), since they don't specify their lifetime contract. So I'd prefer not to have to pass the wrappers as pointers or references. :-) Tony
participants (6)
-
Dave Abrahams
-
Gottlob Frege
-
Olaf van der Spek
-
Pierre Morcello
-
Steven Watanabe
-
Stewart, Robert