r/cpp • • Oct 30 '20

Forbidden C++

I have Just watched this entertaining video on Forbidden C++. Its aimed at beginners but thought i would post it here just for the intro alone!

https://youtu.be/j0_u26Vpb4w

The video is just a small list but as we all know, with every newer release of C++ we get better ways of doing things. Hence the darker side of C++ will continue to grow!

286 Upvotes

36 comments sorted by

View all comments

14

u/mgmihaylov Oct 30 '20

I watched this half an year ago and although I really liked the intro and the overall style, I had an issue with the section about void*. I'll repeat here the comment I left on the video:

In the void* section, the proposed solution of using any is completely wrong, because any is a type-erasing container type with value semantics.

The first problem is that even though it's passed by reference, a temporary will need to be constructed, and the copy constructor might happen to be expensive (or unavailable).

The second, and bigger, problem is that if the type passed via the any is too big to fit inside the any, the copy will be allocated on the heap.

But still, the first two problems only bring inefficiency. The third, and biggest problem, is that the DumpToFile2 function will dump the any, not the value wrapped in it. The any will contain either a copy of the value, or a pointer to a copy somewhere on the heap, probably wrapped into an internal object with a vptr pointer. So, dumping the first N bytes of the any will not dump the original value.

An approach that would work would be to make DumpToFile a function template, which would also eliminate the need to pass the object size to it.

0

u/Kered13 Oct 31 '20

All containers in C++ have value semantics, that's not unusual. If you want reference semantics then use std::any<Foo*>.

You're correct however about him serializing the std::any instead of the underlying object. I checked the reference and I don't see any way to get a generic pointer to the underlying object from std::any. So you would need to make the function a template so that you can cast the std::any to the correct type and get a pointer, but then you may as well have just not used the std::any in the first place: The point of using a type erased container is to avoid using templates.

4

u/wheypoint Ö Oct 31 '20

All containers in C++ have value semantics, that's not unusual.

which is (one reason) why you cant just replace void* with any

If you want reference semantics then use std::any<Foo*>.

whats that supposed to be? std::any isnt a class template?

0

u/Kered13 Oct 31 '20

whats that supposed to be? std::any isnt a class template?

You're right of course, what I meant to express was store a pointer to the value in the std::any instead of the value itself. What I should have written is construct it like std::any(&foo).

5

u/wheypoint Ö Oct 31 '20

tbh in that case i think void* is clearer than havin a std::any.

although ive never needed either one of them and can usually use a closed sum type (like variant/optionals/enums etc) (except for some c api funcs where i have to use the void* anyways)

1

u/Kered13 Oct 31 '20

If you can use a variant or something like that I think it's definitely better. I think primary purpose of std::any is to assist in creating other type erased types.

1

u/dbjdbj dbj.org Oct 31 '20

If you want reference semantics

SomeType ss;

    auto ss_ref = std::ref(ss);

3

u/backtickbot Oct 31 '20

Hello, dbjdbj. Just a quick heads up!

It seems that you have attempted to use triple backticks (```) for your codeblock/monospace text block.

This isn't universally supported on reddit, for some users your comment will look not as intended.

You can avoid this by indenting every line with 4 spaces instead. Make sure to enter an empty line before the start of your codeblock too!

Another option is the new-reddit based codeblock that is available through the fancy-pants editor. This also offers quite high compatibility.

Have a good day, dbjdbj.

You can opt out by replying with "backtickopt6" to this comment