aardvark179 ([info]aardvark179) wrote,
@ 2009-03-24 11:23:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
I thought I was being stupid, but apparently I wasn't.
I have been full of cold since Thursday, but couldn't take Friday or Monday off because I was at least trying to deal with an urgent customer issue, so completely collapsed at the weekend (to the extent of not getting out of bed and not even looking at my email, which chose this weekend to pile up so I've got 60 things to look at even after the spam filter had done its job).

That urgent customer issue is now dealt with, but instead of going back to what I was meant to be working on I was asked to work on another issue which somebody had foolishly promised to resolve, and given a date for. It looks like a fairly simple problem, but it's clearly going to be DOOM because even the example code provided with the library uses const_cast.

For those of you lucky enough not to do C++, const is a way of saying something is immutable and should be used a lot and const_cast is a way of taking something which is meant to be immutable and making it mutable, sometimes. It's implementation dependent and not guaranteed to work, and is normally an indication that something was designed badly in the first place.

I also finally had my annual review, which was good but annoyingly would apparently have been even better if it were offset by three months. It's nice to be told that if you keep up what you've been doing you'll be classed as a Role Model in your next assessment, it's annoying to know you'll probably collapse in a gibbering heap before then.

Oh, and my LJ user pic seems to have gone awol, I'm not sure if there was a screw up in their database or what, but I've put back again now.



(6 comments) - (Post a new comment)


[info]1ngi
2009-03-24 12:36 pm UTC (link)
Get well soon. And don't abuse your body like that again. There's only one of you, and some of us are quite fond. x

(Reply to this) (Thread)


[info]aardvark179
2009-03-24 03:16 pm UTC (link)
I think I am pretty much over the cold now (excitingly coloured mucous excepted) and I do at least have a long weekend coming up.

(Reply to this) (Parent)


[info]bellinghman
2009-03-24 12:40 pm UTC (link)
const_cast is a way of taking something which is meant to be immutable and making it mutable

Well, no.

Well, it can be misused for that, but that's not its purpose.

const_cast<> is meant to allow you to remove a 'const' that has been added to a previously mutable object. If you use it on a genuinely const object, then you are a sinner, and should be cast into the outer darkness, because then you are invoking undefined behaviour.

But if, on the other hand, you have something like:

char* thirdchar(char* ptr) {
  return const_cast<char*>(thirdchar(const_cast<char const*>(ptr)));
}

char const* thirdchar(char const* ptr) {
  return ptr+2;
}

then you are removing the const previously applied. Here, you have one implementation, with the non-const function forwarding to the one implemented in const terms.

Agreed, const_cast<> can be misused, just as reinterpret_cast<> can be. At least, unlike those poor beggars using C, you can grep for their uses.

Oh, and the classic smelly case is the library APIs that never heard of const: in those cases, you may know that the parameter is not modified, and cast away const. Even so, I'll frequently use a temporary vector of char instead of passing in a std::string::c_str() pointer.

(Reply to this) (Thread)


[info]aardvark179
2009-03-24 02:51 pm UTC (link)
Clearly I'm still suffering from cold, as you say I only gave the nasty case for which it can be used. You could grep for their use, but you'd better grep your library headers as well, there are some really scary surprises lurking in some those things.

In this case the issue is that a couple of methods for accessing some important member variables are not declared as const *thing class::getThing() const; but rather as const *thing class::getThing(); There may be something going on in internally to do with reference counts or something, buy considering the example code provided I'm surprised they didn't do public method which was declared as working on a const object and hide all the const_casting in an internal private method.

Oh, and the only example I can find in Stroustrup of const_cast is of an object casting itself to its non-const type to populate ab internal cache.

(Reply to this) (Parent)(Thread)


[info]bellinghman
2009-03-25 10:14 am UTC (link)
I think your cold is pretty bad, your type and const keywords are all messed up.

Yes, the declaration you show us is odd: a member access method requiring a non-const object while still returning a const pointer (and why a pointer rather than a reference? Might the member not be there?!) looks silly, though it's definitely better than the opposite - a const object returning a non-const pointer.

Regarding stuff in library headers: that's part of the implementation and may sidestep the rules that application-level code must adhere to. It may also include assembler or other means to do the things the implementation needs to do that aren't actually possible in the pure language.

(Reply to this) (Parent)


[info]bugshaw
2009-03-24 06:22 pm UTC (link)
I thought it had been rather devoid of aardvark scratchings around here lately. A Role Model! Oh my!

(Reply to this)


(6 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…