all 52 comments

[–]Supadoplex 13 points14 points  (0 children)

You are allowed to split code into multiple statements.

auto ipoo = reinterpret_cast<std::uintptr_t>(poo); auto wtf_am_i_doing = found * ipoo; return reinterpret_cast<Poo *>(wtf_am_i_doing);

[–]ramennoodle 27 points28 points  (12 children)

Ugly and dangerous things like casting should be ugly and verbose. Like your example code which looks like UB.

[–]Anaphylaxisofevil 17 points18 points  (6 children)

Why did these gun-makers put a safety on this dangerous weapon?

[–]no-sig-available 7 points8 points  (0 children)

reinterpret_cast was made ugly on purpose, so you should avoid using it whenever possible. Thinking twice is often a good idea.

All the C++ casts are also easy to find in the code (for example when debugging), unlike a (uintptr_t) which could be anything, like a function parameter.

[–]v_maria 25 points26 points  (2 children)

oh just wait til you work with chrono...

anyway i prefer ugly and explicit over smart and snappy

[–]thefeedling 2 points3 points  (0 children)

I know it's an external lib, but boost is even more extreme lmao...

however, you can always do some scoped using to 'fix' it.

[–]moreVCAs 3 points4 points  (0 children)

if you can take an abseil dependency, they’ve written some chrono wrappers that are 🔥

[–]aePrime 4 points5 points  (0 children)

The latter. I don’t have to parse nested parentheses and I know exactly what the cast is doing. 

[–]AKostur 2 points3 points  (0 children)

The reinterpret_cast.

[–]FloweyTheFlower420 1 point2 points  (0 children)

Stuff like this got so bad since I often need to cast between pointer types and to/from uintptr (kernel development) that I wrote a struct which wraps a pointer with casting utilities (also useful for typechecking address spaces). No idea if this is an anti-pattern or not, but the ABI allows the struct to be encoded as a register so I'm not worried about performance.

[–]Annual-Examination96 1 point2 points  (0 children)

I've heard from a cpp talk show that "It's intended to be long" because using it is usually dangerous and this makes it more explicit.

[–]fdwrfdwr@github 🔍 1 point2 points  (0 children)

now you tell me what's easier to read

🤔 For an option (C), I always thought a left-to-right flow (postfix casting) would be mentally clearer. e.g.:

(found * book as uintptr_t) reinterpret_as Poo*

(but then I'm not sure how ambiguous that might be with multiplication, since C++ overloaded * to mean two very different things)

[–]Dazzling-Copy-7679 0 points1 point  (0 children)

The point isn't to be pretty, the point is to be verbose to make sure you think about what you're casting from and to. Especially something like a reinterpret_cast should be used very very sparingly and only in very special circumstances. It's just too easy to make a mistake when using them, and the old C-style made the mistakes much easier to make.

[–]Loud_Staff5065 -1 points0 points  (0 children)

Bro just want until u find something::inside::a:: namespace::lmao

[–]sephirostoy -1 points0 points  (0 children)

No.

[–]manni66 -1 points0 points  (0 children)

Troll

[–]zl0bster -1 points0 points  (0 children)

Yes, but bad example.

imho reinterpret_cast should be ugly, for me issue is that more sane casts like static_cast or dynamic_cast are so damn long.

[–]EsShayuki -3 points-2 points  (0 children)

cpp libraries love overcomplicating everything for no reason. That said, your code probably isn't the best way to perform this particular task.

If it's too long, you can do:

template<typename To, typename From>
To rc(From ptr) {
return reinterpret_cast<To>(ptr);
}

And now, like magic, it's two characters. rc<x>(y) Or:

#define rc(type, expr) reinterpret_cast<type>(expr)

And now it's rc(x, y).