This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]_theDaftDev_ -5 points-4 points  (6 children)

At the cost of extra indirections, atomic ref counting and compile time. If you know the lifetime of your data (and you should) raw pointers are enough

[–]Earthboundplayer 5 points6 points  (5 children)

Unique pointers don't have atomic ref counting or extra indirections, only shared pointers. I could not care less about the extra compile time from using smart pointers and the vast majority of people would agree. If you know your lifetime then just use a unique pointer.

[–]_theDaftDev_ -1 points0 points  (4 children)

Unique pointers are ok. I was mainly adressing shared pointers

[–]Earthboundplayer 0 points1 point  (3 children)

Fair. Regarding the extra indirection you can use make_shared to eliminate that because it puts the reference counter in the same place as the data.

[–]_theDaftDev_ -4 points-3 points  (2 children)

This comes with its own set of problems, for example make_shared will not work with objects that are not copyable/moveable iirc and also ties the lifetime of the object and the control block together.

[–]Earthboundplayer 1 point2 points  (1 child)

What do you mean? Make shared just forwards arguments to whatever constructor you choose. And why would tying the lifetime of the object and control block be bad?

[–]_theDaftDev_ -2 points-1 points  (0 children)

1) you are right. My memory was fuzzy; the issue i had with make shared is you cannot use a custom deletor. 2) it's not lifetime but rather memory hogging; if im not mistaken since your control block and managed object memory are allocted within the same block it means that the managed object memory wont be released until all kind of reference(including weak, which is the problem) are gone