891
The C++ learning process
(aussie.zone)
Post funny things about programming here! (Or just rant about your favourite programming language.)
Rust's cargo is great, I'd say it would be best to make the switch sooner rather than later once your code base is established. The build system and tooling alone is a great reason to switch
I'm a gameplay programmer who have worked with Unity and Unreal and I've experiment with Rust for gamedev(though only for hobby projects) and for regular code. My conclusions so far is that Rust sucks for gameplay code, for most other things it's kinda nice.
The biggest reason is that it's much harder to write prototype code to test out an idea to see if it's feasible and feels/looks good enough. I don't want to be forced to fully plan out my code and deal with borrowing issues before I even have an idea of if this is a good path or not.
I would say though that because you are using ECS stuff it is at least plausible to do in Rust but at least for my coding/development style it still isn't a good fit.
There are options for this with Rust. If you wanted to use pure Rust you could always use unsafe to do prototyping and then come back and refactor if you like it. Alternatively you could write bindings for C/C++ and do prototyping that way.
Though, I will say that this process gets easier as you gain more experience with Rust memory management.
Not really. Unsafe doesn't allow you to sidestep the borrow checker in a decent way. And even if you do it the Rust compiler assumes non aliasing and breaking that will give you loads of unexpected problems that you wouldn't get in a language that assumes aliasing...
Testing something that only has side effects to the local scope is probably not too hard but that isn't the most common case for gameplay code in my experience...
Going through another language basically has the same issues as unsafe except it's worse in most ways as you'd need to keep up to date bindings all the time plus just the general hassle of doing it for something that could have been a 10 min prototype with most other setups...
Now sure it's possible that I would have better result after doing even more rust, especially with some feedback from someone who really knows it but that doesn't really change anything in just general advice to people who is already working on something in C++ as they likely won't have that kind of support either.
Those are fair points. I haven’t used it for a little while and forgot the exact usage of unsafe code. I love Rust, but I totally agree that it’s a rough language for game dev. Especially if you’re trying to migrate an existing project to it since it requires a complete redesign of most systems rather than a straight translation.
Unsafe doesn't let you just ignore the borrow checker, which is what generally tripped me up when learning to write rust.
That’s fair, I honestly haven’t used it in a while and forgot the real usage of unsafe code. As I said to another comment, it is a really rough language for game dev as it necessitates very different patterns from other languages. Definitely better to learn game dev itself pretty well first in something like C++, then to learn Rust separately before trying game dev in Rust.
Story by a game dev who gave up on Rust after 3 years https://loglog.games/blog/leaving-rust-gamedev/
Well if you're into game dev, ECS and Rust, there's like a 99% chance you know of it, but just in case you don't: We have bevy, now with an extra full-time dev (Alice, who'd been working hard at it for years, I think she's a bigger contributor than the author himself at this point lol)
Brackeys started a series on Godot recently. If you are writing a smaller game GDscript looks attractive and far simpler.
They're mucking it about a little though, like that post when checking if types don't match '! value is type' can now be 'value is not type' which is more readable but not as logical in terms of the language.
Being a Python simp, I find GDscript just different enough to nag. There's a lot of QoL stuff they don't have and aren't (currently) looking to add in order to keep the language simple. Honestly has me looking to use C# instead.
Being a Python simp, I find GDscript just different enough to nag. There's a lot of QoL stuff they don't have and aren't (currently) looking to add in order to keep the language simple. Honestly has me looking to use C# instead.
I believe the consensus nowadays is that abstract classes should be avoided like the plague even in languages like Java and C#.
I have not heard this consensus. Definitely inheritance where the base class holds data or multiple inheritance, but I thought abstract was still ok. Why is it bad?
In 99% of the cases, inheritance can easily be replaced with composition and/or interfaces. Abstract classes tend to cause hard dependencies that are tough to work with.
I’m not sure why you would use abstract classes without data. Just use interfaces.
How do you implement an interface in C++ without an abstract class?
Ask Bjarne to add interfaces enough many times until he gives in.
On a more serious note, I’m not exactly sure what the best C++ practice is. I guess you just have to live with abstract classes if you really want interfaces.
An abstract class with no member variables serves the same purpose in C++.
The only problem is to ensure the entire team agrees to only use it like an interface and nothing else. But I guess that’s the only proper way to do it in C++, for now.
That's not really the job of the language, though. If they can't read the design docs and source annotations, they don't really have any business touching anything.
this seems like the only proper way to do anything in C++. it’s a language where there’s 5 ways to do 1 thing and 1 way to do 5 things.
I know at least three ways, one of them involves variadic macros.
You don't even need to look that far, take any sufficiently aged library, like OpenGL.
It was rhetorical.
Yet I still had an urge to explain an obvious thing. Because it's C++, so everyhing goes. There are even tools to auto-generate C++ interfaces, because of course someone decided that C++ is inadequate and must be improved using some kind of poorly-documented ad-hoc extension language on top of C++.
Say List is an interface.
You have implementations like ArrayList and LinkedList.
Many of those method implementations will differ. But some will be identical. The identical ones go in the abstract base class, so you can share method implementation inheritance without duplicating code.
That’s why.
If the lists have shared components then that can be solved with composition. It’s semantically the same as using abstract classes, but with the difference that this code dependency doesn’t need to be exposed to the outside. This makes the dependency more loosely coupled.
In my example, how is the code dependency exposed to the outside? The caller only knows about the List interface in my example.
In your example, the declaration of ArrayList look like:
The dependence on AbstractList is public. Any public method in AbstractList is also accessible from the outside. It opens up for tricky dependencies that can be difficult to unravel.
Compare it with my solution:
Nothing about the internals of ArrayList is exposed. You’re free to change the internals however you want. There’s no chance any outside code will depend on this implementation detail.
That's not C++, which has more control over such scope.
Perhaps we have a terminology mismatch, I tend to use abstract class and interface interchangeably. I’m not sure it’s possible to define a class interface in c++ without using inheritance, what kind of interface are you referring to that doesn’t use inheritance?
You do have a terminology mismatch. In C++, an abstract class is a class with at least one pure virtual method.
Such classes cannot be instantiated, so they are useful only as base classes.
An interface is more of a concept than a thing.
Sure you can say that
Iterable
is an interface that provides theNext()
andPrev()
methods and you can say thatArray
is anIterable
because it inherits fromIterable
(and then you override those methods to do the correct thing), and that’s one way to implement an interface in C++.But you can also say that
Iterable<T>
is a class template that provides aNext()
andPrev()
methods that call the methods of the same name on the type that they wrap (CRTP aka static polymorphism).Or you can say that an algorithm that scans a collection
T
forward requires the collection to have aNext()
method by callingNext()
on it.And I can think of at least 2 other ways to define an interface that isn’t using abstract classes.
And even if using abstract classes, inheriting from them is definitely the least flexible way to use them to define an interface, because it doesn’t allow one to do something like mocking functionality in tests, because it’s not possible to redefine the class to be tested to inherit from the test interface implementation with mocked functionality, so one still needs something to the effect of dependency injection anyway.
So yeah, abstract class is very different from inheritance, and it’s also very different from interface, even though it relates to both.
I agree, my terms aren’t perfect, but as you stated there isn’t really such a thing as an interface in c++, traditionally this is achieved via an abstract base class which is what I meant by using them interchangeably.
I know there are many things you can do in c++ to enforce an interface, but tying this back to the original comment that inheritance is objectively bad, I don’t think there’s any consensus that this is true. Abstract base classes (with no data members) and CRTP are both common use cases of inheritance in modern C++ codebases and are generally considered good design patterns.
Meh. Been developing professionally with C++ for 10 years at this point. I’m one of the weird people that kinda likes C++ and its pragmatism despite all its warts.
I’d like C++ better if it didn’t have inheritance. There are better solutions to model interfaces, and without inheritance people can’t write class hierarchies that are 10 levels deep with a different set of virtual functions overridden (and new virtual functions added) at each level.
And yes, that is not hypothetical. Real codebases in the real world shipping working products do that, and it’s about as nice as you can imagine.
I don't think it's what the person you're replying to meant, but template metaprogramming in modern c++ allows the use of "duck typing" aka "static polymorphism" where you can code against an interface without requiring inheritance.
Typically this is done with CRTP which does require inheritance. But I agree, you can do some meta programming or use concepts which can enforce interfaces in a different way. But back to the original comment that interfaces via inheritance are objectively bad, I don’t think there’s any consensus that this is true. And pure virtual interfaces and CRTP are both common use cases of inheritance in modern C++ codebases and are generally considered good design patterns.
The way I was taught was that you usually start off with only an interface and then implementing classes, and then once you have multiple similar implementations it could then make sense to move the common logic into an abstract class that doesn't get exposed outside of the package
I usually break it out using composition if that’s ever needed. Either by wrapping around all the implementations, or as a separate component that is injected into each implementation.