-1

How can I tell if a pointer is an RValue or I don't know what I'm talking about.. This really ridiculous idea popped into my head while drinking a beer..

What if you have stupid programmer/user..

Assume you have the following class:

template<typename T>
class Container
{
    private:
        std::vector<T> Storage;
    public:
        Container(T Anything) : Storage() {Storage.push_back(Anything);}
}

and the user does:

Container<Object*> C(new Object(Params));

Then how can I delete it? I want to be able to tell the difference between the above and the below:

Object* O = new Object(Params);
Container<Object*> C(O);

I just want to know. I know that the first example obviously should not be used but let's assume that it will be or that I want to detect leaking code like that and delete them.

How can this be done? Is that an RValue pointer? What do I call that?

Brandon
  • 22,723
  • 11
  • 93
  • 186
  • 2
    `Container C(new Object(Params));` is nonsensical – _you_ have to specify what type `T` is, so, what is it? – ildjarn Mar 13 '13 at 03:37
  • In this case T is a pointer.. Ooohh you mean like Container(new Object())? – Brandon Mar 13 '13 at 03:38
  • Oohh.. I fixed it. Sorry :( – Brandon Mar 13 '13 at 03:40
  • What do rvalues have to do with any of this? – Nicol Bolas Mar 13 '13 at 03:40
  • What what do you call a pointer to a temporary object? I thought Temporary Objects are called R Values? – Brandon Mar 13 '13 at 03:41
  • 1
    @CantChooseUsernames: [Read about expression value categories.](http://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) Also, that's *not a temporary*. – Nicol Bolas Mar 13 '13 at 03:42
  • 2
    The pointer is a temporary, the object it points to is not. That's what he seems to want to be talking about in the question, though his most recent comment seems to conflict with that interpretation. – Benjamin Lindley Mar 13 '13 at 03:46

1 Answers1

3

Easiest and correct thing would be to wrap naked pointers in some resource container like shared_ptr.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
Vishal Kumar
  • 762
  • 1
  • 7
  • 15
  • No because then you'd be deleting a managed pointer. You don't want to delete every pointer.. only the stupid leaking ones. – Brandon Mar 13 '13 at 03:38
  • 1
    @CantChooseUsernames : If it's shared, it will only be deleted when _every_ instance goes out of scope. – ildjarn Mar 13 '13 at 03:40
  • shared_poiner only deletes when resource count reaches zero. auto_ptr or unique_poiner gives single resource ownership. – Vishal Kumar Mar 13 '13 at 03:42
  • just to add, use of managed pointers is always advisable over naked pointers. It true for all resources like use scoped_lock over naked lock. Soctt Mayer explains it as RAII (http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization). But always look which managed class is needed. For pointers shared_poiner is easiest and safest but costly. Look for its varient like unique_pointer, scoped_ptr. – Vishal Kumar Mar 13 '13 at 03:48
  • I tried your suggestion. http://pastebin.com/ud65bWDY If the user deletes the pointer, it will totally mess everything up regardless that it is shared. I guess this is just not possible. I wanted to know if there was a way for the Container class to tell which type of pointer it is.. Inline or non-inline. – Brandon Mar 13 '13 at 04:27
  • 2
    @CantChooseUsernames: Yes, if the user is an idiot, and does something totally stupid and non-intuitive like calling delete on a pointer that was passed to a shared_ptr, it will break. What are you trying to do, protect users from every conceivable error that they could possibly make? Even if you could "fix" this "problem", bad programmers will find something else to mess up. – Benjamin Lindley Mar 13 '13 at 04:37
  • 1
    @CantChooseUsernames: Oh, nevermind. The problem in your example there is your class constructor. You should not take a raw pointer in and then pass it to a shared_ptr. That's a transfer of ownership, which should start with an object that ensures that the caller actually has ownership. That is, the constructor should take a shared_ptr and store a copy of it. – Benjamin Lindley Mar 13 '13 at 04:46