10

I'm new to auto pointer. I have this:

std::auto_ptr<myClass> myPointer(new MyClass(someArg));

How do I test whether I can instantiate myPointer successfully? I tried if (myPointer==NULL) and the compiler emitted an error:

no operator "==" matches these operands.

Wolf
  • 9,679
  • 7
  • 62
  • 108
user853069
  • 491
  • 2
  • 6
  • 12
  • Always try to reproduce error messages verbatim in their entirety. The compiler normally will have dropped all unimportant bits before you get to see them. – n. m. could be an AI Aug 15 '11 at 20:15
  • Seriously, though, what do you mean by "instantiate" here? – Lightness Races in Orbit Aug 15 '11 at 20:25
  • I think *whether I can instantiate myPointer successfully* means just *whether myPointer was created successfully*. The question and its answers - most of all [the one of Lightness Races in Orbit](http://stackoverflow.com/a/7070126/2932052) - could be even better without this distraction. – Wolf May 19 '14 at 11:05
  • Remember Occam's Razor guys (and gals). Code posted is often trimmed to reduce verbosity, and it ain't necessarily so that the allocation of myPointer is immediately followed by the NULL/nullptr test. – brewmanz Aug 29 '16 at 00:14
  • @brewmanz: If the OP "trimmed" the code so much that it doesn't actually demonstrate the problem any more, then that's the opposite of Occam's Razor. It's also entirely useless as a question. – Lightness Races in Orbit Sep 05 '16 at 11:06

5 Answers5

23

What do you mean by "instantiate"?

On a standard-compliant implementation, either the construction of the MyClass succeeded, or an exception was thrown and the auto_ptr will no longer be in scope. So, in the example you provided, the value of the pointer represented by your auto_ptr cannot be NULL.

(It is possible that you are using an implementation without exception support, that can return NULL on allocation failure (instead of throwing an exception), even without the use of the (nothrow) specifier, but this is not the general case.)


Speaking generally, you can check the pointer's value. You just have to get at the underlying representation because, as you've discovered, std::auto_ptr does not have an operator==.

To do this, use X* std::auto_ptr<X>::get() const throw(), like this:

if (myPointer.get()) {
   // ...
}

Also note that std::auto_ptr is deprecated in C++0x, in favour of std::unique_ptr. Prefer the latter where you have access to a conforming implementation.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 2
    The OP might be working in a context where exceptions are not used (so `new` can return NULL). This is far more common than people like to admit. – zwol Aug 15 '11 at 20:07
  • @Zack: Is that valid without specifying `(nothrow)`? @Zack: Seems [that's only relevant for non-compliant compilers](http://stackoverflow.com/questions/3389420/will-new-operator-return-null/3389428#3389428), and I'm not bothered about them. This question is about C++. – Lightness Races in Orbit Aug 15 '11 at 20:08
  • @Tomalak Geret'kal On embedded compilers that do not support exceptions there's no need to call the `std::nothrow` version of `new`. Either call will return NULL if the allocation fails. – Praetorian Aug 15 '11 at 20:10
  • @Praetorian: Hmm, OK I'll edit that in; it's non-compliant though. – Lightness Races in Orbit Aug 15 '11 at 20:11
  • Per your answer - "Either the construction of the MyClass succeeded, or an exception was thrown and the auto_ptr will no longer be in scope." Does auto_pointer automatically throws an exception eventhough I dont have throw command in MyClass's constructor? – user853069 Aug 15 '11 at 20:15
  • @user853069: No, but the allocator behind `new` will throw `std::bad_alloc` for you if the memory couldn't be allocated. – Lightness Races in Orbit Aug 15 '11 at 20:18
  • 1
    Even in non-embedded environments, exception handling is *disabled* for a lot of real, production code (for instance, I can personally attest that both Firefox and Webkit do this, and I've heard that it is also the case for OpenOffice). Yes, this is a non-compliant mode for the compiler. Yes, it means much of the standard library is unusable or does not behave as documented. It's still C++ as she is spoke, and I think it behooves us (answerers of StackOverflow questions) to be aware of it. – zwol Aug 15 '11 at 20:23
  • I guess if I still want to use auto pointer, I have to do something like this right? try{ std::auto_ptr myPointer(new MyClass(someArg)); – user853069 Aug 15 '11 at 20:29
  • 1
    @user853069: Only if you want to handle the `std::bad_alloc`. Usually you don't bother: if you've run out of memory, there's not a lot of point in continuing to execute the program. Just let it bubble up to `main` and terminate your process. (The use of `std::auto_ptr` is not related to this aspect of dynamically-allocating objects.) – Lightness Races in Orbit Aug 15 '11 at 20:31
  • N.B. it'll only bubble up to `main` if you have a `catch` in `main`! – Lightness Races in Orbit Sep 05 '16 at 11:09
5

How about this?

 if(myPointer.get()==NULL)
Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
4

I think myPointer.get() == NULL is what you're looking for.

Jon
  • 3,065
  • 1
  • 19
  • 29
1

When checking an std::auto_ptr for NULL, I think this is most idiomatic notation:

if (!myPointer.get()) {
    // do not dereference here
}
Wolf
  • 9,679
  • 7
  • 62
  • 108
1

Given the statement you wrote in your question either myPointer is set or an exception was thrown and catching the exception is the correct way to check if anything went wrong.

In any case you can get the underlying pointer by calling auto_ptr::get().

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55