What's a C++-like language with garbage collection?

November 18, 2026 5 answers
Asked by _v62!quorvilo.7

I like C++ for its flexibility, type system and metaprogramming, but I don't need that much control over memory for most projects. What languages keep some of those strengths while adding garbage collection?

Answers

_m57!quorlina.8

C# is probably the first one I'd try. The syntax will feel familiar coming from C++, and it has generics, extension methods, properties and plenty of useful language features without requiring you to manage ordinary object memory yourself.

It isn't C++ with a collector bolted on, though. Its runtime and metaprogramming model are different, so check whether the specific template techniques you like have equivalents before committing to it.

9f~zavrix_638:

For resources other than memory, C# also has IDisposable and using statements. Garbage collection doesn't replace explicit cleanup of things like file handles.

Reply to this answer
_b72+torvexa.5

Take a look at D. It may be closer to what you're describing on a language design level, with C-like syntax, metaprogramming and garbage collection. The practical tradeoff is that its ecosystem and range of established applications may not be as broad as those of some alternatives.

Reply to this answer
_e63~claspgizmo.4

Java is another reasonable candidate, especially if a mature ecosystem matters more than having C++-style metaprogramming. The syntax is recognizable and memory is garbage collected, but its type and generic programming facilities won't map directly to everything C++ templates can do.

Reply to this answer
Zavdelo-8p

You might not need to leave C++ just to avoid writing delete. RAII, standard containers, unique_ptr and shared_ptr can take care of a lot of cleanup automatically while you keep the language features you already like.

That's not the same as tracing garbage collection. You still need to think about ownership and lifetimes, and cycles of shared_ptr can keep objects alive unless you break them, for example with weak_ptr.

Reply to this answer
Reldavo.2k

Go is worth considering if the practical goal is to write garbage-collected programs without fussing over memory. It doesn't resemble C++ as closely in syntax or metaprogramming, so it depends which part of your wish list matters most.

_k84!brindlewhisk.2:

Rust may also come up in this comparison, but it isn't garbage collected in the Java or C# sense. It manages ownership differently.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions