5 ms·
So, I'm curious, what is it about modern C++ that you think is better than Java? Because I know (old) C++, some of the modern stuff and a bit of Java, I don't
by raverbashing 8y ago
So, I'm curious, what is it about modern C++ that you think is better than Java?
Because I know (old) C++, some of the modern stuff and a bit of Java, I don't know the full history.
- badfrog 8y agoOP already said: > That lets you move the vast majority of java runtime type errors to compile time That's a huge win.
- pvg 8y agoIs it? It suggests that typical Java code is for some reason full of run time type errors which it isn't. Whatever the differences between and respective advantages and disadvantages of Java and C++, runtime type errors are just not a some typical Java problem, let alone one that C++ solves.
- koala_man 8y ago>which it isn't NullPointerException is a run time type error and quite common. I'm not convinced it's a problem C++ adequately solves, though.
- pvg 8y agoYou can take a view of types that is broader than the one Java actually uses such that an NPE is a 'runtime type error' but you can do that with lots of runtime errors. It's not what the poster is talking about.
- koala_man 8y agoYou sure could -- Coq programmers might say that any error is a runtime type error. I don't think NPEs go much beyond Java's own definition though. If you have a reference to a String, it's not necessarily of type String. You'd get a runtime error if you try to treat it as such, just like you would in Python. This is not the case in C++, though obviously it's not an example of template metaprogramming.
- rplst8 8y agoI'm guessing this is related to the "syntactic sugar" that generics use in Java? If so isn't this largely covered by the warnings from a decent IDE?
- corysama 8y agoI'm not familiar with modern Java, but modern C++ has been advancing so fast that people are calling for a cool-down. The counter for "C++ is already too complex" is that many of the new complexities are being put in place as simpler replacements for common boilerplate. For example: Pointers, new, delete, malloc, free are all still there. But, you really don't need them any more. You can write large-scale programs using only objects, containers, iterators and unique_ptr (and shared_ptr if you can't clean up your design enough to avoid it). You basically get full memory management without garbage collection and while retaining RAII destructor capability. "Optionally define this function based on these class properties. Else, define this alternative implementation" used to require a significant about of template boilerplate to pull off SFINAE. That has been cut down dramatically by if constexpr (). constexpr in general has been expanding to the point that someone wrote a compile-time ray tracer just to see if he could. Also, the oddly-named void_t makes it easy for if constexpr to "define this code if this statement will compile, else do this alternative". Between if constexpr, modules and a few more extensions (source_file/source_line?) there is a goal of eliminating 90%+ of the need for the preprocessor. Functions with auto parameters, auto return types and containing some if constexpr can effectively be powerful templates without any <>. Lambdas are obviously a shorthand for "functor" style class definitions. And, they can (auto)->auto. range based for(auto &x:container) is much simpler than for(int i=0; i<n; i++) even though you are so familiar with for(;;) that you barely see it any more. But, even better, the standard algorithms spell out intent much better than a generic loop in 90% of cases. Coroutines are already implemented in multiple compilers and are being standardized currently. Variadic templates, parameter packs and tuples make wrapping arbitrary function signatures fast and easy. nodiscard return types eliminate a whole class of errors. optional<T> return types are nice. The upcoming "zero-overhead deterministic exceptions" and expected<T,Error> are big improvements to error handling.
- raverbashing 8y agoThanks, that make a lot of sense. I'm favouriting this comment.
- tigershark 8y agoHow in the world would you get full memory management without garbage collection in c++?
- rienbdj 8y agoC++ supports more complex inheritance models. Operator overloading The const keyword C++ can guarantee stack allocations C++ has faster boot times C++ templates are more powerful than Java generics C++ latency is better, which is important for some applications such as audio C++ can manage external resources better using RAII Not saying C++ is better in all cases, but it does bring things to the table that Java does not.