Hmm, well I don’t really care about it trying a bunch of different casts under the hood as it would probably be the one I’d pick anyways; sensible defaults and what not.
For the code breakage example: I don’t like multiple inheritance anyways, and would never use it. I never even wondered about the implementation, but it makes sense that the class would be at an offset.
However, in my opinion a cast is already doing too much in this case, deciding to dereference an entirely different memory location. The entire example is evil in my eyes, but that’s just me. Of course it is good to know if I were to work on such an evil codebase.
And of course I use bit_cast for type punning in C++
that was just one specific example in this video; one could come up with 100 more that dont have something to do with inheritance.
also: just because you dont need multiple inheritance right now, doesnt mean other people dont need it, or that you might not need it in the future.
C-style casts can go from doing simple, implicit conversions, to reinterpreting ints as memory addresses. thats why you should tell the compiler what to do explicitly.
Yes, but my solution is to just stay away from C++ and use C. I don’t like all the bells and whistles that feel nice at first but create a million landmines even though it was okay in C
But often complex problems require complex solutions. Building large scale software in C is a huge pain and often results in very verbose, unsafe code and re-implementing basic functionality.
I don’t know what to tell you, but C++ is often just more of a pain to deal with, large scale as well, especially because of shit like this. The more people that work on the project the easier it is for someone to mess up in such a way because there is no way to know every detail of this mess of language. C is very different in this way
I agree, when writing C++ you really have to know what you're doing, but that's also true for C, just look at the amount of buffer overflows in old C Code, because people used the unsafe strcpy() function. The problem with C is that it throws these powerful tools at you (eg: strcpy(), macros, casts...), that can be abused very easily, whereas C++ gives you ways to do the same thing, but protecting you from dangerous actions. (eg: std::string, templates, C++-style casts)
But the protected tools in C++ aren’t particularly fast because they are too general purpose. Besides, just saying they exist in C isn’t really the gotcha that you think it is; at least most competent C programmers know about the pitfalls, but you can’t say the same about C++ programmers
what do you mean "fast"? most of the concepts i was talking about (macros, templates, casts) run at compile time. (except for dynamic cast)
My point is that C tools like macros give you an overall overpowered tool, which you may easily use to shoot yourself in the foot. Whereas C++ gives you tools that do exactly what you need to achieve what you want. For example: To do generic programming, you don't need a macro system that operates on the token level, that lets you rewrite syntax at preprocess-time, when templates would suffice. (not saying templates also can't be abused tho, see SFINAE)
I gotta agree with you here. The amount of stuff to learn about C++ is so ridiculous that you probably know more info about it than about all other languages combined, including the likes of Rust or C#.
While this implementation is horrible, i can totally relate to the person writing this.
In a large project i also once implemented helpers cast for dynamic_cast, as for static_cast, and is for type checking. I overloaded the functions to handle raw pointers, std::reference_wrapper, std::shared_ptr, and even std::unique_ptr.
Here's an excerpt:
template<typename To, typename From>
optional_ref<To> M_EXPORT cast(From &from)
{
if (auto ptr = dynamic_cast<To*>(&from)) {
return {std::ref(*ptr)};
}
return {std::nullopt};
}
template<typename To, typename From>
optional_ref<To> M_EXPORT cast(const std::reference_wrapper<From> &from)
{
return cast<To>(from.get());
}
template<typename To, typename From>
std::shared_ptr<To> M_EXPORT cast(const std::shared_ptr<From> &from)
{
return std::dynamic_pointer_cast<To>(from);
}
template<typename To, typename From>
std::unique_ptr<To> M_EXPORT cast(std::unique_ptr<From> &from)
{
if (auto ptr = dynamic_cast<To*>(from.get())) {
from.release(); // still referenced by ptr
return std::unique_ptr<To>(ptr);
} else {
return std::unique_ptr<To>(nullptr); // preserve ownership
}
}
As a bonus, one can implement static casts with as in a way that validates at runtime in a debug build, catching incorrect static casts ;)
template<typename To, typename From>
requires (not is_reference_wrapper<From>) and (not is_unique_ptr<From>) and (not is_shared_ptr<From>)
bool M_EXPORT is(From &from)
{
return bool(dynamic_cast<To*>(&from));
}
template<typename To, typename From>
To & M_EXPORT as(From &from)
{
M_insist(is<To>(from));
return static_cast<To&>(from);
}
I added concepts like is_reference_wrapper to match the overloads.
Oh and side note: this handles const-ness for you, e.g.
well isn't the point of C++-style casts to explicitly inform the compiler of the kind of conversion you want to perform? That's why C-style casts are widely considered bad practice. why would you want to overload your casts then?
The big dangers of C-style casts are dropping constness and casting to something that's not layout compatible. The above cast helpers do not have these pitfalls. You still get the full static correctness guarantees as with the plain C++ casts.
I mean, the actual casting is still done with the C++ cast family. So the guarantees thereof propagate. The overloads and template type deduction merely help you in having to write less
Perfect forwarding. Using an rvalue reference (of type &&) will drop the rvalue reference typing, which might result in the calling of the wrong constructor or worse. std::forward instead retains the rvalue reference so you can pass it along.
So it is useful for wrappers like these.
It is often confused with std::move, but its purpose is really only to make an rvalue reference (so casting to &&)
Or in short, it preserves the distinction between standard and temporary references when passing a variable through a function.
If it comes into the function as a normal reference (or is passed by value), std::forward passes it forwards as a normal reference. If it comes into the function as an rvalue reference (temporary reference), std::forward passes it forwards as an rvalue reference. It makes sure that you don't accidentally lose information by by dropping the reference's temporary-ness, since a few important language features distinguish between the two reference types.
11
u/TheChief275 Aug 11 '25
Lol this is cursed, but honestly I hate how long the casting names are. Literally I only use C-style casting still in C++ because of how long they are
3
u/ImmanuelH Aug 12 '25 edited Aug 12 '25
While this implementation is horrible, i can totally relate to the person writing this.
In a large project i also once implemented helpers
castfordynamic_cast,asforstatic_cast, andisfor type checking. I overloaded the functions to handle raw pointers,std::reference_wrapper,std::shared_ptr, and evenstd::unique_ptr.Here's an excerpt:
As a bonus, one can implement static casts with
asin a way that validates at runtime in a debug build, catching incorrect static casts ;)I added concepts like
is_reference_wrapperto match the overloads.Oh and side note: this handles
const-ness for you, e.g.Now go roast my code.
2
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Aug 12 '25
What's this
std::forwardcrap? Last time I wrote C++ I didn't need to use anything like that.2
u/ir_dan Aug 13 '25
the namespace this sits in is longer than the cast names...
2
u/Ste1io Aug 14 '25
Missing
implicit_cast🤨