30

I want to pass a unique_ptr to a helper function, and I want to make sure that the helper function neither modifies the pointer, nor the pointed object. Without the unique_ptr, the solution is to have

void takePtr(AClass const * const aPtr) {
  // Do something with *aPtr. 
  // We cannot change aPtr, not *aPtr. 
}

(Well, technically, AClass const * aPtr is enough.) And I can call this with

AClass * aPtr2 = new AClass(3);
takePtr(aPtr2);

I want to instead use unique_ptr, but cannot figure out how to write this. I tried

void takeUniquePtr(unique_ptr<AClass const> const & aPtr) {
  // Access *aPtr, but should not be able to change aPtr, or *aPtr. 
}

When I call this with

unique_ptr<AClass> aPtr(new AClass(3));
takeUniquePtr(aPtr);

it does not compile. The error I see is

testcpp/hello_world.cpp:141:21: error: invalid user-defined conversion from ‘std::unique_ptr<AClass>’ to ‘const std::unique_ptr<const AClass>&’ [-fpermissive]

Shouldn't the conversion from unique_ptr<AClass> to unique_ptr<AClass const> be automatic? What am I missing here?

By the way, if I change unique_ptr<AClass const> const & aPtr to unique_ptr<AClass> const & aPtr in the function definition, it compiles, but then I can call functions like aPtr->changeAClass(), which I don't want to allow.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Yogeshwer Sharma
  • 3,647
  • 5
  • 25
  • 27
  • 6
    Why not pass a const reference, then? (instead of a `unique_ptr`) – Marshall Clow May 07 '13 at 17:50
  • Marshall Clow: I happen to have `unique_ptr`s around in the code. I can dereference the pointer and pass as const reference, but that is not ideal, given the code base I am working with. Also, sometimes, the `unique_ptr` might point to NULL in my case (not managing any object), so const reference will not work in that case. – Yogeshwer Sharma May 07 '13 at 17:55
  • 3
    In that case, I would just pass a `const AClass *`. No need for the callee to know that it's in a `unique_ptr`. Why couple that tightly? – Marshall Clow May 07 '13 at 18:04
  • Your question doesn't make a whole lot of sense. If you pass a `unique_ptr` to the helper, it will by definition free the object when its done with it -- that's the whole point of `unique_ptr`. If you don't want that, why not just pass a regular (const) pointer? – Chris Dodd May 07 '13 at 18:17
  • 2
    @ChrisDodd: notice he passes a *reference* to `unique_ptr`, so the object will live on when the helper is done. – syam May 07 '13 at 18:21
  • Marshall Clow: I might need to do that. `.get()` the unique_ptr, and pass the raw pointer as const pointer to const object. – Yogeshwer Sharma May 07 '13 at 18:30
  • 1
    @MarshallClow: But another question remains: Why is compiler not able to cast from `unique_ptr` to `unique_ptr`? (Implicit) non-const to const conversion happens all the time! :-) – Yogeshwer Sharma May 07 '13 at 18:33
  • 1
    Yes, it does - `T` and `const T` but are very different from `class` and `class`. In the first case, the two classes are the same, one just is marked const (there's even a set of template metafunctions for conversion `add_const` and `remove_const`. In the second case, there's no way the compiler can be sure that the classes are in any way related (except by name). I can specialize a template for const types and have it be *completely* different than for non-const types. – Marshall Clow May 07 '13 at 19:51
  • 1
    Just pass a reference to const object. It doesn't make much sense to pass a pointer at all (unless there's some reason it needs to be a pointer, and you haven't said there is) – Iron Savior May 07 '13 at 20:20

2 Answers2

45

Smart pointers are for managing ownership and lifetime, they allow us (amongst other things) to safely transfer ownership around the various parts of our code.

When you pass a const unique_ptr<T>& to a function (irrelevant of whether T is const or not), what it actually means is that the function promises to never modify the unique_ptr itself (but it could still modify the pointed-to object if T is not const) ie. there will be no possible transfer of ownership whatsoever. You're just using the unique_ptr as a useless wrapper around a naked pointer.

So, as @MarshallClow suggested in a comment, you should just get rid of the wrapper and pass either a naked pointer or a direct reference. What's cool with this is that your code is now semantically clear (your function's signature clearly states that it does not mess with ownership, which was not immediately obvious with a const unique_ptr<...>&) and it solves your "constification" problem at the same time!

Namely:

void someFunction(const AClass* p) { ... }

std::unique_ptr<AClass> ptr(new AClass());
someFunction(ptr.get());

Edit: to address your secondary question "why won't the compiler let me ... cast unique_ptr<A> to unique_ptr<A const>?".

Actually, you can move a unique_ptr<A> to a unique_ptr<A const>:

std::unique_ptr<A> p(new A());
std::unique_ptr<const A> q(std::move(p));

But as you can see this means a transfer of ownership from p to q.

The problem with your code is that you're passing a (reference to) unique_ptr<const A> to a function. Since there is a type discrepancy with unique_ptr<A>, to make it work the compiler needs to instantiate a temporary. But unless you transfer ownership manually by using std::move, the compiler will try to copy your unique_ptr and it can't do that since unique_ptr explicitly forbids it.

Notice how the problem goes away if you move the unique_ptr:

void test(const std::unique_ptr<const int>& p) { ... }

std::unique_ptr<int> p(new int(3));
test(std::move(p));

The compiler is now able to construct a temporary unique_ptr<const A> and move the original unique_ptr<A> without breaking your expectations (since it is now clear that you want to move, not copy).

So, the root of the problem is that unique_ptr only has move semantics not copy semantics, but you'd need the copy semantics to create a temporary and yet keep ownership afterwards. Egg and chicken, unique_ptr just isn't designed that way.

If you now consider shared_ptr which has copy semantics, the problem also disappears.

void test(const std::shared_ptr<const int>& p) { ... }

std::shared_ptr<int> p(new int(3));
test(p);
//^^^^^ Works!

The reason is that the compiler is now able to create a temporary std::shared_ptr<const int> copy (automatically casting from std::shared_ptr<int>) and bind that temporary to a const reference.

I guess this more or less covers it, even though my explanation lacks standardese lingo and is perhaps not as clear as it should be. :)

syam
  • 14,701
  • 3
  • 41
  • 65
3

Got to this old question on const smart pointers. Above answers ignore the simple template solution.

The very simple template option (option a)

template<class T>
void foo(const unique_ptr<T>& ptr) {
    // do something with ptr
}

this solution allows all possible options of unique_ptr to be sent to foo:

  1. const unique_ptr<const int>
  2. unique_ptr<const int>
  3. const unique_ptr<int>
  4. unique_ptr<int>

If you specifically want to avoid for some reason 3 and 4 above, add const to T:

Accept only const/non-const unique_ptr to const! (option b)

template<class T>
void foo(const unique_ptr<const T>& ptr) {
    // do something with ptr
}

Side note 1

You may overload "option a" and "option b" if to get different behavior for the cases where you can or cannot alter the pointed value.

Side note 2

If you do not want to make any changes to the pointed value in this function (never! whichever type of parameter we get!) -- do not overload.
With "option b", compiler won't allow to change the value we point at. Job done!
If you want to support all 4 cases, i.e. "option a", the function may still "accidentally" change the value we point to, e.g.

template<class T>
void foo(const unique_ptr<T>& ptr) {
    *ptr = 3;
}

however this should not be an issue if at least one caller has T that is actually const, the compiler will not like it in that case and help you get to the problem.
You may add such a caller in unit-test, something like:

    foo(make_unique<const int>(7)); // if this line doesn't compile someone
                                    // is changing value inside foo which is
                                    // not allowed!
                                    // do not delete the test, fix foo!

Code snippet: http://coliru.stacked-crooked.com/a/a36795cdf305d4c7

Amir Kirsh
  • 12,564
  • 41
  • 74