Live data from Hacker News

Modern C++ Won't Save Us

alexgaynor.net

251–260 of 395 posts

Re: Modern C++ Won't Save Us

#251

Earlier quoted context omitted.

When I worked on a mobile C++ project at Google, we went exceptionally out of our way to avoid memory issues. We ran under valgrind and multiple sanitizers (and continuously ran those with high coverage unit and integration tests). We ran fuzzers. We had strictly enforced style guides. We still shipped multiple use after frees and ub-tripping behavior. I also saw multiple issues in other major libraries that we were…

Writing memory safe programs in C++ is possible. Most coding styles and some problem domains don't lend themselves to it naturally, though. In my experience, restricted subsets used for embedded software vastly reduce the risk of introducing errors and make actual errors easier to spot and fix.

> Writing memory safe programs in C++ is possible.

Everything "is possible" in the sense that in theory you can do it. But if time and time again people fail to do it. Even people who invest almost heroic levels of effort (see above: valgrind, multiple sanitizers, and so on) you get to the point where you have to accept that what is possible in theory doesn't work in practice.

Re: Modern C++ Won't Save Us

#252
post #65

Earlier quoted context omitted.

My opinion on that, is that such code MUST NOT be optimized away. Instead it should be a compile error.

You might wish for that, but the ship has sailed. Undefined behavior means that the implementation can do whatever it can. That said, I do expect tools, both sanitizers and static analyzers to improve to detect more of these kinds of cases.

The original intention of standardization was that compilers would gradually reach consensus on what the behaviour in certain cases should be, and once that happened the standard would be updated to standardize that behaivour. Compilers are allowed - indeed encouraged - to provide additional guarantees beyond the minimum defined in the standard (indeed part of the point of UB is that a compiler is allowed to specify what the behaviour is in that case).

Re: Modern C++ Won't Save Us

#253
post #127

Earlier quoted context omitted.

I agree with this and would take it a step further, and say that recent changes to the STL are the worst parts of modern C++. For example std::regex supports 6 distinct syntaxes, the PRNG stuff is massively over-engineered, the "extensions for parallelism" add complexity without giving enough knobs for any real perf improvement. Meanwhile there's gaping holes like UTF-8 support. It's a sad state.

How is the prng over engineered? I agree its a little clunky for casual use but it makes all the right decisions, imo, for serious use of prngs (e.g. reproducible experiments for Monte Carlo methods in simulation and statistics)

> it makes all the right decisions, imo, for serious use of prngs

Apart from an awkward API that's hard to use correctly, Mersenne Twister which is basically the main generator has been obsolete for years (bad quality RNs, slow, huge state, ...).

Re: Modern C++ Won't Save Us

#254
post #114

Earlier quoted context omitted.

> think if you need a very safe code, you shouldn't use the string_view or span without thinking about the potential consequences. That’s the whole point: your caveat shows that’s it’s C/C++ which are unsafe in their very nature and therefore should not be used in code exposed to potentially malicious (e.g. user or network) input. Which is just about everything useful. HPC are generally closed systems and have differ…

There is no such language as C/C++. There is C, which cannot be written safely, and there is C++, which can be, and quite often is. It has been many years since I shipped a memory bug in C++. It is just not a real worry for me. I am constantly dealing with design, specification, and logic flaws, which affect Rust equally, or moreso. I am aware that there are plenty of other programmers out there, writing bad code in…

> It has been many years since I shipped a memory bug in C++. It is just not a real worry for me.

Can you write down the algorithm that you use to avoid writing memory bugs? Can you teach others how to do it? Experienced C++ programmers do seem to learn how to avoid those bugs (although very often what they write is still undefined according to the standard - but e.g. multithreading bugs may be rare enough not to be encountered in practice). But that's of limited use as long as it's impossible for anyone else to look at a C++ codebase and confirm, at a glance, that that codebase does not contain memory bugs.

> C++ is (still) quite a substantially more expressive language than Rust, which is to say it can capture a lot more semantics in a library.

> So it's great that Rust makes some errors harder to make, but that is no grounds for acting holier-than-thou. Rust programmers have simply chosen to have many more of the other kinds of errors, instead.

Citation needed. What desirable constructions are impossible to express in Rust? I've no doubt that you can write some super-"clever" C++ that reuses the same pointer several different ways and can't be ported to Rust - but such code is not desirable in C++ either (at least not in codebases that more than one person is expected to use). Meanwhile Rust offers a lot of opportunities for libraries to express themselves clearly in a way that's not possible in C++: sum types let you express a very common return pattern much more clearly than you can ever do in C++. Being able to return functions makes libraries much more expressive. Standardised ownership annotations make correct library use very clear, and allow a compiler to automatically check that they're used correctly.

> Every programmer who switches from C to Rust makes a better world; likewise Java to Rust, or C# to Rust, or Go to Rust. Or, any of those to C++.

> Switching from C++ to Rust, or Rust to C++, is of overwhelmingly less consequence, but the balance is still in C++'s favor because C++ still supports more powerful libraries.

> You might disagree, but it is far from obvious that you are correct.

On the contrary, it's obvious from the frequency with which we see crashes and security flaws in C++ codebases that the average programmer who switches from Java to C++, or C# to C++ makes the world a worse place. It's overwhelmingly likely to be true for Rust to C++ as well.

Re: Modern C++ Won't Save Us

#255
post #127

Earlier quoted context omitted.

I agree with this and would take it a step further, and say that recent changes to the STL are the worst parts of modern C++. For example std::regex supports 6 distinct syntaxes, the PRNG stuff is massively over-engineered, the "extensions for parallelism" add complexity without giving enough knobs for any real perf improvement. Meanwhile there's gaping holes like UTF-8 support. It's a sad state.

How is the prng over engineered? I agree its a little clunky for casual use but it makes all the right decisions, imo, for serious use of prngs (e.g. reproducible experiments for Monte Carlo methods in simulation and statistics)

The problem is there's no easy to use sensible defaults, just a confusing bunch of options with a bunch of apparently easy but subtely wrong ways to use it. Having the power is useful, but I would also just like a rand() (or better a randrange()) which actually works.

Re: Modern C++ Won't Save Us

#256
> Dereferencing a nullopt however, gives you an uninitialized value as a pointer, which can be a serious security issue.

Is this really true? Surely it just gives you an uninitialised `int` (or whatever is in the `optional`)?

Re: Modern C++ Won't Save Us

#257
post #172
post #31

For systems programming languages, safe by default with scoped unsafe code is a Pareto improvement on unsafe everywhere.

A feature that exists since 1961, across several systems languages.

I never understood this type of comments, is what you are trying to say something like:

"It was already tried and failed, why is this time better"

"Mainstream languages always end up not using it"

"People should reference more the original works of the past"

...

One of the many explainations of the name Rust is that it represents a collection of old ideas. What was the point you were trying to convey in specific?

Re: Modern C++ Won't Save Us

#258

Without any data to back this up, my guess is that there is no good reason to pick C++ for a new project except when the developer is already fluent in C++. Assume we have this abstract developer that has a good knowledge in programming theory but has no experience in programming languages. The developer starts a new project, but in what language? web: Don't see any reason for this. Exist lots of great alternatives.…

Language VM:

HotSpot, ART, V8, SpiderMonkey, Chakra, JSC, Dart, and CLR are all written in C++. Are there any modern serious language VMs that aren't written in C++?

Re: Modern C++ Won't Save Us

#259
post #245

Earlier quoted context omitted.

desktop-GUI: I'm not a fan of Electron either, however the community seems not to be joking about picking it. It seems like most projects are going that direction. None of your arguments here are about a new projects, just about existing technologies. Embedded: But how is that an argument for C++? I can do safe stuff in Lua without the hassle of C++ & then use C when needed. Parallellism: Agree. Here we have a case.…

desktop-GUI: What community? Web devs trying to write desktop apps? Embedded: Try to write a safe string and vector with bounds checking, or IO port access in C like in C++ type system allows for. Lua is nice for hobby, not production class hardware deployments. Scientific: Depends, many libraries are yet to be written. IDE tooling: Typescript and C# audience isn't the same as those using raw C++.

desktop-GUI: Yes, frontend today is a combination of Web & apps, C++ does not fit in either. UWPs can be written with JS or C#.

Writing desktop-GUI is also on the decline, however I could see potential increase in desktop-GUI-programs if governments continue to pass bad regulations of the web like content filters & link tax.

Embedded: How come C++ is not dominating embedded? According to you, it should.

IDE tooling: If you are writing a 2D-game or desktop-GUI it is.

Even though C++ is used as an important building block for other technologies, too survive, C++ must attract a new generation of developers, to continue carrying the torch & develop it further. Does it? I have seen little evidence of that. My impression is that developers learn C++ because of existing projects. Sure, there exists lots of good C++ projects out there that will continue attract developers & push the language further, but will it be enough? I'm not convinced.

Re: Modern C++ Won't Save Us

#260

Without any data to back this up, my guess is that there is no good reason to pick C++ for a new project except when the developer is already fluent in C++. Assume we have this abstract developer that has a good knowledge in programming theory but has no experience in programming languages. The developer starts a new project, but in what language? web: Don't see any reason for this. Exist lots of great alternatives.…

> Any good arguments to pick C++ for a new project?

Probably niche, but almost all audio dsp (vst &co) uses C++. See JUCE framework. Possibly other "multimedia" stuff (video, image manipulation, etc)

Post reply on HN