Hey Albinopapa

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Hey Albinopapa

Post by chili » May 31st, 2015, 2:09 pm

What's up?
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Hey Albinopapa

Post by albinopapa » June 1st, 2015, 5:50 am

Uh, weird being called out like this...what's up bro? lol.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Hey Albinopapa

Post by chili » June 1st, 2015, 12:52 pm

Not much man. I've just gotten over an obsession with indoor rock climbing and semi-got over Texas Hold'em (still a little into that one), so now I'm looking to up my programming time per week.

Just fooling around with HUGS stuff this weekend. C++ giving me a goddamn hard time. By the way, I have a riddle for you.

You know how much of a hard-on I have for const-correctness right? (rock solid) What do you think is a good reason NOT to use const for data members of a class?
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Hey Albinopapa

Post by albinopapa » June 1st, 2015, 6:15 pm

Hmm, getting over an obsession of indoor rock climbing, sounds like there was a particular reason for getting into it in the first place...someone perhaps? hehe. Hold'em 'ay? Didn't like it at first, but the more I play the more I enjoy it. Still can't play for hours though. Glad to hear you are doing well and getting back to the giving us some HUGS.

As to your riddle, I've been trying const here and there for function params, but only have I ever consted data members is if they will never change. Have found one caveat to that however. Using the move semantics you end up having to cast off the const when using the move assignment operator (operator=( Object &&)).

Hmm, just thinking out loud here. The only data member types I would const would be,
min/max values, math constants such as gravity or speed of light, data that define a physical object like a model's vertices or it's size. At the moment I can't think of much more to const.

If you are looking for blanket reason NOT to const any data members, I would have to say when you are going to be moving the class when not using the constructor, as in the move assignment operator. The best example I can think of would be an array of objects can't be initialized except through the default constructor, but if you need the data to be different than default, you have to use the = operator, which would create the need for const_cast or whatever.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Hey Albinopapa

Post by albinopapa » June 2nd, 2015, 6:30 pm

So what's the answer? Or, just looking for a different outlook.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Hey Albinopapa

Post by chili » June 3rd, 2015, 2:20 am

You pretty much nailed it papa. Constant data members (i.e. constant embedded objects, pointers and refs to const are fine) cause problems when you wanna make copy/move assignment operators. Universal constants (ones that are constant across all instances of a class) are fine because you just ignore them in the assignment operator (they should by definition already be the value that you would have assigned them). But object lifetime constants (which vary from instance to instance but do not change during the lifetime of any one instance) are problematic.

Generally speaking, when I have a class (like say a Ship class), I don't generally use the assignment operator because it doesn't make much sense. Each ship is its own contained entity; it makes little sense to take the attributes of one and then overwrite another with them. However, that is exactly what will happen when you try and stick you ships in an STL container (specifically, a vector) and then sort them. The algorithm will use assignment as it's swapping them around, and you'll be f'ed in the a.

The idiom of making variable that do not change over the lifetime of an object const makes a lot of sense to me. Consider the case of a class that contains a unique ID member so that each instance is uniquely identifiable. You will likely have a function or operator that compares the uids of two ships to test if they are the same ship or not. A common typo is: if( uid = otherUid ). Try that with uid as a const member, and you get a compile-time error pointing you to the exact spot where you fucked up. Without const, you could be pulling your hair out for an hour or more trying to figure out why shit's not working.

That being said, I think I'm going to have to give up on consting the fuck out of class members, because I'm not trying to give up compatibility with the STL here just to satisfy my sensibilities.
One solution would be to use containers of unique_ptr which then hold objects dynamically allocated. That would however be a lot slower. Can't justify sacrificing what, in certain situations, would be a lot of performance for my const fetish either.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Hey Albinopapa

Post by albinopapa » June 3rd, 2015, 4:27 am

I would use a vector of pointers, ship addresses, in that particular case.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Hey Albinopapa

Post by chili » June 3rd, 2015, 6:02 am

chili wrote:One solution would be to use containers of unique_ptr which then hold objects dynamically allocated. That would however be a lot slower. Can't justify sacrificing what, in certain situations, would be a lot of performance for my const fetish either.
http://www.bfilipek.com/2014/05/vector- ... nters.html

The difference between a naked pointer and a unique_ptr (or even a shared_ptr for that matter, from the perspective of iteration over the collection) is negligible.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Hey Albinopapa

Post by albinopapa » June 3rd, 2015, 7:46 am

Yeah, speed vs ease of programming, it's a tough one. Thanks for posting the article, didn't realize that using a vector of pointers would be that much slower than to use just a vector of objects.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
thetoddfather
Posts: 338
Joined: October 1st, 2012, 9:53 pm
Location: Canada

Re: Hey Albinopapa

Post by thetoddfather » June 3rd, 2015, 9:21 pm

Literally walking out the door to go indoor rock climbing and I quickly check the forums and read this... Am I dreaming? Is this real life?

Post Reply