126

I'm writing an accessor method for a shared pointer in C++ that goes something like this:

class Foo {
public:
    return_type getBar() const {
        return m_bar;
    }

private:
    boost::shared_ptr<Bar> m_bar;
}

So to support the const-ness of getBar() the return type should be a boost::shared_ptr that prevents modification of the Bar it points to. My guess is that shared_ptr<const Bar> is the type I want to return to do that, whereas const shared_ptr<Bar> would prevent reassignment of the pointer itself to point to a different Bar but allow modification of the Bar that it points to... However, I'm not sure. I'd appreciate it if someone who knows for sure could either confirm this, or correct me if I got it wrong. Thanks!

Dave Lillethun
  • 2,598
  • 2
  • 17
  • 24
  • 3
    It's exactly what you said. You can look at the documentation for operators `*` and `->` to confirm this. – syam Jul 22 '13 at 17:07
  • 2
    What's the difference between `T *const` and `T const *`? The same. –  Jul 22 '13 at 17:09
  • 3
    @H2CO3 Not at all. The `const` normally modifies what _precedes it, so `T *const` is a `const` pointer to `T`, and `T const*` is a pointer to `const` `T`. And it's best to avoid using `const` with nothing preceding it. – James Kanze Jul 22 '13 at 17:27
  • 6
    @JamesKanze, that's H2CO3's point: the difference between `T *const` and `T const *` is the same as the difference between `const shared_ptr` and `shared_ptr` – Jonathan Wakely Jul 22 '13 at 17:29
  • 1
    @JamesKanze Oh but yes. `T *const` is a const pointer to non-const `T`, so is `const shared_ptr`. In contrast, `T const *` is a non-const pointer to `const T`, so is `shared_ptr`. –  Jul 22 '13 at 17:42
  • 1
    @H2CO3 I misinterpreted what you meant by "The same". But I am curious about one thing: you write `T *const`, so why don't you write `shared_ptr const`? Similarly, you wrote `T const*`, so why not `shared_ptr`? Why not be orthogonal, and put the `const` after everywhere (since you have to put it after in some cases). – James Kanze Jul 22 '13 at 17:55
  • @JamesKanze Right. That's because I'm used to `const T`. I find it easier to read as well, and I generally prefer it over `T const`. In this example, I opted to write it the "orthogonal" way so that there cannot possibly be any confusion. When the declaration only consists of one part, i. e. just a single type, then it is (or least it should be) obvious that the `const` qualifies that one type (since it cannot qualify anything else). –  Jul 22 '13 at 18:00
  • @H2CO3 When nothing precedes the `const`, it modifies what follows. But it isn't always obvious: `typedef int* PtrInt; const PtrInt pi;`. Despite appearances, he definition is the equivalent of `int* const`, not `const int*`. (This is, IMO, the killer argument. Orthogonality is a nice arguement---since you have to put the `const` after in a lot of cases, why not everywhere---but it's not really an absolute.) – James Kanze Jul 23 '13 at 07:43
  • @JamesKanze I'm aware of what each syntax means - please don't assert I am not. –  Jul 23 '13 at 09:06
  • @H2CO3 I don't think (at)JamesKanze meant to assert that, but merely to show why he thinks his style is preferable to your style. Nontheless, this is becoming a quesion of style, which is opinion. I think we've all reached a concensus on what they syntax actually means. – Dave Lillethun Jul 23 '13 at 16:48

4 Answers4

189

You are right. shared_ptr<const T> p; is similar to const T * p; (or, equivalently, T const * p;), that is, the pointed object is const whereas const shared_ptr<T> p; is similar to T* const p; which means that p is const. In summary:

shared_ptr<T> p;             ---> T * p;                                    : nothing is const
const shared_ptr<T> p;       ---> T * const p;                              : p is const
shared_ptr<const T> p;       ---> const T * p;       <=> T const * p;       : *p is const
const shared_ptr<const T> p; ---> const T * const p; <=> T const * const p; : p and *p are const.

The same holds for weak_ptr and unique_ptr.

Cassio Neri
  • 17,293
  • 5
  • 43
  • 66
  • 1
    You also answered a question I had in the back of my head about regular pointers (const T* vs. T* const vs. T const *). :) I didn't mention that because I didn't want my quesion on SO to be _too_ broad, and this was the question pertinent to my current task. Anyhow, I think I understand very well now. Thanks! – Dave Lillethun Jul 22 '13 at 17:23
  • 9
    I'm glad it helped. A last tip that I use to remember about `const T* p;', 'T const * p;` and `T * const p`. See the `*` as a separator in the sense that what is `const` is what is on the same side of the `*`. – Cassio Neri Jul 22 '13 at 20:25
  • 5
    My rule of thumb is that `const` always refers to the thing on the left side of it. If nothing is on the left, it's the thing on the right side. – hochl Mar 03 '17 at 13:12
  • 1
    hochi - how about const T * p; equivalent to T const * p;? – Vlad Aug 29 '19 at 17:21
  • Cassio, you can add that in the case of returned type const shared_ptr, it cannot be used in non-const functions while this is not true for const pointers. – Vlad Aug 29 '19 at 17:22
2

boost::shared_ptr<Bar const> prevents modification of the Bar object through the shared pointer. As a return value, the const in boost::shared_ptr<Bar> const means that you cannot call a non-const function on the returned temporary; if it were for a real pointer (e.g. Bar* const), it would be completely ignored.

In general, even here, the usual rules apply: const modifies what precedes it: in boost::shared_ptr<Bar const>, the Bar; in boost::shared_ptr<Bar> const, it's the instantiation (the expression boost::shared_ptr<Bar> which is const.

James Kanze
  • 142,482
  • 15
  • 169
  • 310
1
#Check this simple code to understand... copy-paste the below code to check on any c++11 compiler

#include <memory>
using namespace std;

class A {
    public:
        int a = 5;
};

shared_ptr<A> f1() {
    const shared_ptr<A> sA(new A);
    shared_ptr<A> sA2(new A);
    sA = sA2; // compile-error
    return sA;
}

shared_ptr<A> f2() {
    shared_ptr<const A> sA(new A);
    sA->a = 4; // compile-error
    return sA;
}

int main(int argc, char** argv) {
    f1();
    f2();
    return 0;
}
vivek2k6
  • 99
  • 8
0

I would like to a simple demostration based on @Cassio Neri's answer:

#include <memory>

int main(){
    std::shared_ptr<int> i = std::make_shared<int>(1);
    std::shared_ptr<int const> ci;

    // i = ci; // compile error
    ci = i;
    std::cout << *i << "\t" << *ci << std::endl; // both will be 1

    *i = 2;
    std::cout << *i << "\t" << *ci << std::endl; // both will be 2

    i = std::make_shared<int>(3);
    std::cout << *i << "\t" << *ci << std::endl; // only *i has changed

    // *ci = 20; // compile error
    ci = std::make_shared<int>(5);
    std::cout << *i << "\t" << *ci << std::endl; // only *ci has changed

}
Jónás Balázs
  • 583
  • 9
  • 20