A Quick Reminder About shared_ptr constness

Short blog here, just pointing out a weird problem I ran into the other day in the handling of shared_ptrs. I have some classes that keep shared_ptr members, and sometimes I need to hand out more references to callers... you know, share them. That getter might look a bit like this:

class Klass
    shared_ptr<int> GetX() { return x; }
    const shared_ptr<int> GetX() const { return x; }

    shared_ptr<int> x;

Right? Looks good? That's the normal way I write my pointers, anyway. But... there's something wrong here, and maybe you've already spotted it. Here's the code that triggers it, anyway.

int __cdecl wmain(int argc, wchar_t* argv[])
    Klass k;
    k.x = shared_ptr<int>(new int(3));

    const Klass* pk = &k;
    shared_ptr<int> spX(pk->GetX()); // invokes const GetX()

    *spX = 13;
    wprintf(L"x: %d\n", *spX); // x: 13

    return 0;

Hey, wait a second! This invokes the const getter, but then the caller is able to modify the value. How did that circumvent the const? Well, the problem is that you returned a const shared_ptr<int> value, pointing to a non-const int, so the shared_ptr object can't be modified, but the thing it points to can. Moreover, since it's returning a value (rather than a pointer), the const doesn't even matter.

The fix is simple. Here ya go:

shared_ptr<const int> GetX() const { return x; }

// and later in wmain()

shared_ptr<const int> spX(pk->GetX()); // invokes const GetX()

Now we get a compiler error, as we want:

main.cpp(35) : error C3892: 'spX' : you cannot assign to a variable that is const

This difference is analogous to the difference in constness on raw pointer types: int* const vs const int* (same as int const*). These rules don't seem very inuitive or nice, but you should know them if you're trying to make your code correct.


Sandwich Mechanics: An Honest and Unironic Chat About Sandwiches

Well, it's 2013, and it's looking like it'll be another great year for sandwiches... in that sandwiches still exist, and I'm still going to be eating them. Life is good.

Sometime back in December, my college friend Kirill hit me up on AIM, and we proceeded to have a totally unironic, legitimate conversation about sandwiches. It's got a smattering of stupid college-time inside jokes, but there's real magic here, you guys.

Kirill: OK
Kirill: your latest post about sandwiches made me realize something
Kirill: I toast the shit out of everything
knutaf: uh oh
Kirill: which means I might be MISSING OUT
Kirill: on soft texturezzz
Kirill: and whatnot
knutaf: that's possible
knutaf: i like toasting, too, but not on regular lunchmeat
knutaf: somehow it never comes out right for me
knutaf: kind of this weird mix of hot and cold
Kirill: OK here's something bufu
Kirill: for me, cheese works as a bridge between the hot and the cold
Kirill: I don't know how to describe it further
knutaf: i guess I kind of buy it
knutaf: but meat is the core for me, and that ends up unsatisfying in the end
Kirill: oh wow, TWO levels of qualifiers
Kirill: a provisional purchase
Kirill: AND a Keith-grade yig
knutaf: yeah, i've got some pretty strong reservations about that
knutaf: oh man, haven't thought about keith in like seven years
knutaf: "keith"
Kirill: no, I completely buy your reservations
Kirill: and even reserve another batch of your reservations
Kirill: in a meat-centric sandwich, things either serve the meat
knutaf: oh man, a twofold reservation
... idiotic nonsense ensues...
Kirill: a comment I'd meant to make earlier is that, for me, there are many sandwiches where meat is merely a participant, not the star of the show
Kirill: I can think of no better example than the corn beef sandwich
knutaf: really? corned beef is pretty strong in taste, if i remember right. it's been a while
Kirill: right, so check it
Kirill: you also add something equally strong -- sauerkraut -- and use cheese to bind them together
Kirill: not to mention that the rye bread has a very strong character of its own
knutaf: so it's more like a saurkreef sandwich than a corned beef with saurkraut
Kirill: sure
knutaf: i dig it. that sounds pretty good
Kirill: in the USSR, there's also a nontrivial fondness for open-faced sandwiches
knutaf: i have to be wary of saurkraut though. too often it ends up too wet, and sogs up the sandwich
Kirill: yeah, this is very important
knutaf: maybe not "too often", but it happens
Kirill: of all the ingredients in a corn beef sandwich, the kraut demands the most scrutiny quality-wise
knutaf: open faced sandwiches just seem inconvenient. two slices of bread is a totally fine ratio of bread to filling
Kirill: well, I'll describe to you the various pros and you can decide whether you buy them
Kirill: a beloved children's cartoon has a cat that urges kids to eat their open-faced sandwich face-down so that your tongue can directly contact the top parts
Kirill: it's completely cheeky and it's obvious that the cat is just making a joke
Kirill: but I was one of the kids that took it seriously
Kirill: and there are many meats and spreads alike where the cat's advice made a difference with respect to how much mileage you got out of the top stuff
Kirill: which invokes the further point that, yes, 2:1 bread to filling ratio is reasonable for many -- but not all -- things
knutaf: so you attempted to eat your open faced sandwich upside down? how'd that work out for ya?
Kirill: it's a subcultural joke at this point
Kirill: you'll see people eating it face-down and you KNOW where they got the concept from
knutaf: that's awesome. but doesn't your sandwich just fall all over the place?
Kirill: not if you're careful, which might be a terrible answer
knutaf: what the heck. i can't even picture this
Kirill: for me, it was a take-it-or-leave-it concept until my mom gave sandwiches with these truffle-infused cheese spreads and whatnot
Kirill: I didn't always enjoy them to the extent that my mom did
Kirill: but I did know that I wouldn't even be able to ACCESS the nuances to evaluate them had I added an extra portion of bread
knutaf: hmm
knutaf: this may not have been a luxury for back there, but sounds like the panini might be a good compromise here
Kirill: panini are a good counterexample
Kirill: they rely so much on the structure (i.e. the things being properly joined) that an open-faced rendition will be harder
Kirill: but to me, that's just part of a spectrum
Kirill: if I want to try out a new kind of salami for the first time, after I've had it without any bread, I'll have it on an open-faced sandwich first
knutaf: right, an open faced panini is probably not going to work, but the compressed bread reduces the ratio while still giving structure
Kirill: yeah, I was about to mention bread compression
Kirill: because slicing the bread thin Doesn't Cut It (HUUHUHHUHUHHH)
knutaf: hahahaa noooooo
knutaf: noooooo eeeeeeyes
knutaf: nooooo ryyyyyeeees
Kirill: less bread, however, is something I completely buy
Kirill: i.e. a small sandwich not too far from serving something on a ritz cracker
Kirill: incidentally
Kirill: caviar is something I'd NEVER serve close-faced
knutaf: yeah, I buy that. caviar is ultar expensive, though, so you really want as minimal a vehicle as possible
Kirill: I'd argue that it's not really about cost, but texture
Kirill: how familiar are you with caviar?
knutaf: oh. i've barely ever had caviar. maybe just once
Kirill: I smell an experiment
knutaf: it didn't make much of an impression on me. but i have a pretty pedestrian palate sometimes
Kirill: I can mail you a jar of caviar for you to perform sandwich experiments on
knutaf: like, are you suggesting using it as a spread?
Kirill: good caviar is not easy to find and many crooks sell shitty ones
Kirill: well, I can describe it how most Russians serve it
Kirill: there are two kinds of well-known caviar
Kirill: black and red
Kirill: black might be banned or something
Kirill: but it's rare to the point that I would have to do a LOT of homework to trust it
Kirill: red is easier to find
Kirill: black is very small granules and more subtle, red is larger granules with a stronger flavor
Kirill: so I'll be talking about red
Kirill: most of the time, you serve it as an appetizer on tiny slices of a white baguette
Kirill: butter or another spread optional
Kirill: I personally think that dark breads, like rye, bring to it another dimension
Kirill: but I can also respect the view that chooses the white bread as a blank slate to let the caviar shine
knutaf: I could see either way working, yeah
knutaf: I think I had red, the one time I had it
Kirill: I think this is what I'll do
Kirill: I'll find out from relatives who sells the most trustworthy caviar and mail you a can
Kirill: and you should just treat it like an alien mailed you some alien bizsauce
Kirill: and see where it works and doesn't
knutaf: i'm intrigued. sounds great
knutaf: gonna get foodz in a sec. i'll probably end up uploading this chat to the blog (edited some)
knutaf: it's too sandwich related not to
Kirill: word
Kirill: spread the word