Hacker News new | ask | show | jobs
by bro-stick 3870 days ago
The other big change that would be beneficial in (?:Obj)?C(?:++)? would be const-by-default, requiring something like `var` for anything that's truly a variable, since `mutable` has a specific meaning in (?:Obj)?C++. It would be consistent with the theme of least-privilege, and allow for more optimizations of one-time and per-loop iteration values.

Another great, modern C++ feature: `constexpr` functions, methods and compile-time constants have the potential to put most, but not all, macros out-of-business... Macros are powerful for DRYing C/C++ similar code sections which cannot be expressed with templates, lambdas or function pointers, for macro pasting identifiers together and for project configuration management for (ie, kernel hardware support). Defines also don't occupy space but also lose semantic value in debugging because they're computed at compile time and spread out wherever they're used... debuggers encountering `constexpr`s should offer both the computed value and a way to debug or debug (not program code at all) how that value was constructed: gdb and lldb should be able to handle that with plugins.

Here's also how we compile most our code, using both gcc and clang, on both stable and head:

    clang++ -std=c++1[14z] -Weverything -Werror -Wno-c++98-compat -Wno-weak-vtables ...

    g++ -std=c++1[14z] -Wpedantic -Wextra -Werror -Wformat=2 ...
I would also really like to see C++1[7z] concepts implemented by major compilers, because this will make our code a whole lot simpler by reduce the hacks for runtime and compile-time type checking, and make generic programming much cleaner by being able to constrain requirements via arbitrary bool constexprs.
1 comments

> const-by-default

const isn't the same as immutable. 'const' implies a (mostly) read-only view, not read-only data. That is, C++ does not guarantee that the memory does not change, just that some parts of the code will not change it (and even then, there are ways to get around 'const').

It would be marginally better to be const by default, because there would be less code to consider when analyzing changes to data, but the cost of this feature is much larger than the benefits we'd see from it.

And, to top it all off, dealing with a lot of immutables really requires different syntax and different style. Read through the rust book and then try to do the examples in C++. You can do pattern matching and variant types in C++, but they are ugly and probably slower than what we do now.