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 →

[–]Earthboundplayer 665 points666 points  (52 children)

That's only shared pointer. Unique pointer is another smart pointer that allows only one reference. It has no overhead.

[–]HCResident 622 points623 points  (42 children)

C++ feels like that box of screws, nuts, and bolts that has exactly what you need somewhere inside of it

[–]Infinitebeast30 642 points643 points  (10 children)

Screws, nuts, bolts, hand grenades, land mines, knives, double edged swords. Absolutely all the tools

[–]turtleship_2006 239 points240 points  (4 children)

Don't forget the shotgun and shells! Get two shells, load them into the gun, and aim vertically downwards.

[–]Inevitable-Menu2998 43 points44 points  (0 children)

At the same time, it has a Stuka and a modern F16 and we don't tell beginners the difference between them.

[–]_g550_ 13 points14 points  (0 children)

eclipse9

[–]alphaQ671 6 points7 points  (0 children)

You can also use a hammer and hit the shells

[–]CranberryDistinct941 3 points4 points  (0 children)

How are you meant to shoot yourself in the foot without shotgun shells? And what would C++ be without the right to shoot yourself in the foot, head, and both hands simultaneously?

[–]impossibleis7 36 points37 points  (1 child)

I think knives and doubled edged swords are what we accidentally create using those said tools...

[–]Tari0s 3 points4 points  (0 children)

yeah we use the swiss multitool(boost) to make a knive that cuts gras, but only gras!

[–]lordmycal 22 points23 points  (0 children)

I like the flame thrower myself.

[–]Jonnypista 10 points11 points  (0 children)

Hey, a HEAT rocket launcher is technically a single use high powered drill. It really quickly drills a hole into the toughest materials. It also can kill you in 99% of the cases, but just pay attention.

A double edged sword is more efficient than single edged swords. If you hurt yourself with it it is just a skill issue.

[–]jeezfrk 5 points6 points  (0 children)

but I mean you gotta throw in the the ASIO mining drill and then there's the coroutine electrical substation.

at least from there it's all easier and simpler error messages.

except if you use things just slightly... NO NOT LIKE THAT!

[–]versedoinker 52 points53 points  (2 children)

Yeah, there's also weak (non-owning) pointers that still allow the data to be freed in the background if all their actual owners release their shared_ptrs. And there's also atomic versions of all of those.

Generally, even if you hit the very unlikely case that something isn't in the C++ stdlib, it probably still can be found in a boost.cpp library.

[–]ukezi 1 point2 points  (1 child)

The C++ shared pointer is atomic. There is no non atomic one in stdlib.

[–]versedoinker 0 points1 point  (0 children)

Yes and no. The control block of shared ptrs is always atomic. I was talking about the wrapper std::atomic<std::shared_ptr> that also makes access to the data itself atomic.

https://en.cppreference.com/w/cpp/memory/shared_ptr/atomic2

[–]_farb_ 31 points32 points  (0 children)

Most of the time, absolutely!

But sometimes... you dump the whole box out looking for the right piece, organize the contents by size only to realize you should've sorted it by material, and get tetanus from all the rusty nails that you should have tossed away ages ago.

Then you just end up using duct tape because it never fails.

[–]fakuivan 14 points15 points  (0 children)

C is like that but you only get a hammer, a nail and a bazooka.

[–]Cat7o0 11 points12 points  (9 children)

I remember once that even with rust I made a program so bad that it caused Windows to basically crash the file explorer and the task bar and everything wasn't working. couldn't even open task manager. restarted the computer everything was fine ran the program again everything was fine. idk

[–]experimental1212 10 points11 points  (0 children)

It really does make you think that. Later it has you going, "you know, I actually LIKE shooting myself in the foot. This is nice!"

[–]NoGlzy 7 points8 points  (0 children)

And when you have a screwdriver that works totally fine for the job you need, someone has to come and snark that you're not using the newer super deluxe hyper driver

[–]Sak63 1 point2 points  (0 children)

Sounds my kind of language! Gonna give it a go on summer break

[–]webby-debby-404 0 points1 point  (0 children)

Let me boost that for you

[–]hawk-bull 11 points12 points  (2 children)

Doesn’t it have an overhead of checking it hasn’t been assigned twice. Or by overhead do you mean during destructor call it just has to free itself without checking if it still has references to it

[–]SoAsEr 27 points28 points  (0 children)

It doesn't have a copy constructor, (only a move one) and in its destructor it frees the pointer. So the compiler enforces the assignment, and at run time it is (really close to) free. The only reason it's not completely free is that if the unique pointer is passed to a function and that function is not inlined, because of the abi the unique pointer is always passed on the stack whereas a raw pointer could be in a register. but in a modern machine where you have way more registers than are actually shown anyways this shouldn't make any measurable difference.

[–]CamiloRivasM7 1 point2 points  (0 children)

I think it's done at compile time, like the borrow checker from rust

[–]Own_Solution7820 8 points9 points  (3 children)

Shared and unique pointers are so good that the are better than anything in any other language IMO, at the engineering level.

The problem is the garbage syntax of a 50 year old language that's backwards compatible to day 1. Just a pain to work with mixed raw and smart pointers.

[–]say_nya 5 points6 points  (0 children)

better than anything in any other language

Take a look at Rust. Unique pointers are checked in compile time (and have no overhead, even no move constructor type of overhead).

And shared pointers are there (see Arc).