News for hackers. Stuff that matters.
Re:Will Carbon Replace C++? by hot_gril on Monday February 27, 2023 @06:24PM (#34963499)
It's not that people can't deal with pointers. The most common issue in C++ is how many ways to shoot yourself in the foot with common utils. Like, we use a lot of abseil utils at work that return string_views instead of new strings, and if you're directly passing that somewhere else, you easily corrupt your memory.

std::string foo = "foo";

const std::string foo2 = absl::StripAsciiWhitespace(absl::AsciiStrToUpper(foo)); // in Python: foo2 = foo.upper().strip()

std::cout << foo2; // garbage

Yeah, all technically the user's fault, but it gets pretty tiring worrying about this stuff when you're writing a high-level application and the few CPU cycles you save don't really matter. No matter how good you are, you will make mistakes, and the chance of one bug corrupting memory in your whole process increases exponentially with the LoC written. Also I have to point out the irony in a language being type-safe but not memory-safe.

Also, totally user error, but I didn't expect Java-looking class syntax to initialize member variables to garbage by default. That should at least be a compiler warning.

Re:Will Carbon Replace C++? by okamiueru on Monday February 27, 2023 @06:33PM (#34963600)
> Also I have to point out the irony in a language being type-safe but not memory-safe.

Misnomer aside, there is no contradiction. Type safety is much easier to reason about at compile time than memory management. So, it shouldn't come as a surprise. You need a rust like "borrow checker"-by-default functionality.

Re:Will Carbon Replace C++? by hot_gril on Monday February 27, 2023 @06:37PM (#34963643)
It's not surprising that they use static types in C++, but it's not about safety. It's about the compiler knowing how big each thing is at compile time for optimization. Dynamic typing requires moving more towards the "interpreted" side of languages.

The contradiction is when the veteran SWEs I work with say we use C++ instead of Python for the added safety of compile-time type checks, treating dynamic typing like a bigger risk than memory trampling (never mind that Python has type-checking too if you really want it).

Re:Will Carbon Replace C++? by bluGill on Monday February 27, 2023 @10:01PM (#34965139)
For very large projects static types in C++ are a lot safer than Python. For simple projects I agree with you, but as you get over 50k loc Python becomes hard to manage, while i'm work in 10 million lines of C++. Sure it takes more effort, but at that size C++ is a lot easier to manage.

C++ does have issues. However few languages can handle complex problems well. (Rust is very intriguing for the possible ability to do things at the same scale)

Re:Will Carbon Replace C++? by hot_gril on Monday February 27, 2023 @10:36PM (#34965325)
10 million C++ lines is 10 million chances for you to misuse memory and cause a bug that causes your entire program to do anything, from simply crashing to writing garbage directly into your database. Imagine you're on a team of 8 and find one day that your DB's indexes were mysteriously corrupted, what do you do next? It's happened here.

If you use the wrong type in Python, you get an unhandled exception failing to find a property or something, that's about it. Maybe there's a contrived scenario where it'll succeed in a wrong way, but again that's a smaller blast radius

Re:Will Carbon Replace C++? by bluGill on Tuesday February 28, 2023 @07:38AM (#34968593)
The likelyhood of Python crashing is a lot higher as it is much harder to know you didn't break your error handling path someplace that isn't tested. C++ sometimes has memory issues, but not nearly as often as Python has problems in the error paths.
Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @12:14PM (#34971867)
Python is exception-based, so it's easy to avoid a crash. Say it's a webserver, you catch all exceptions around each endpoint and give HTTP 500 if it's not something you understand to be a 4xx. Exceptions are nice in that you either explicitly handle them or the caller will.

C++ has exceptions too, but I've never used them, so idk. The abseil statuses end up being used like DIY exception handling, and it's ok but a bit easier to mishandle something than in Python. And a segfault cannot(? or shouldn't) be caught and will crash the whole program.

Re:Will Carbon Replace C++? by bluGill on Tuesday February 28, 2023 @01:37PM (#34973023)
I am not writing a web app, I'm writing an embedded system. Catching an exception is nice, but I still need my code to keep working, which memory leaks do allow, generally for a long time. yes we have had some 'memory scribbler' bugs that were a pain to track down, but they are very rare compared to changing python code and missing to make the right change to an error handling path and now Python unwinds to main instead of handling the error correctly. Note that I'm saying Python errors of that nature are more common despite comparing 50k Lines of Python to 15m lines of C++.

For short programs Python works great, but it doesn't scale to large programs.

Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @02:13PM (#34973498)
You can comfortably write a large web app in Python or similarly in JS, and people do. For embedded, it's already out of the question for other reasons. And you probably don't want exception-based handling in embedded, yeah.
Re:Will Carbon Replace C++? by titzer on Monday February 27, 2023 @07:20PM (#34964051)
> Yeah, all technically the user's fault,

The problem is C++'s priorities. Priority #1 is performance, and if you look closely, nearly every single instance of this buggy-program hostile attitude comes down to the language and libraries absolutely refusing to do any kind of dynamic safety checking by default. UB can be traced directly from "it's too hard to reason about the implications of buggy programs" to "well, don't do that, stupid programmer".

Re:Will Carbon Replace C++? by vlovich123 on Monday February 27, 2023 @08:01PM (#34964364)
There's a talk by Scott Meyers (at the D lang conference IIRC), where he methodologically takes apart the claim that priority #1 is performance or 0-cost abstractions. Basically the way the standards body seems to work is there's ~N different concerns and people will whatever set is appropriate to kill proposals. It gives a fantastic illusion of performance being #1, but C++ makes plenty of choices that are misaligned with performance (e.g. variable aliasing that it inherited from C but it let infect the C++ type system too).
Re:Will Carbon Replace C++? by hot_gril on Monday February 27, 2023 @08:42PM (#34964631)
There are tradeoffs. If performance were always #1 with the underlying requirement of portability, you'd be using C. And performance is more of a concern in C++ than in Java.

I'm no expert in this, I'm just a guy who's sick of writing web backends in C++ for no reason.

Re:Will Carbon Replace C++? by pjmlp on Tuesday February 28, 2023 @12:02PM (#34971716)
Ah the days of ISAPI ATL extensions. :)
Re:Will Carbon Replace C++? by vlovich123 on Tuesday February 28, 2023 @02:22PM (#34973608)
You'd think so, but C doesn't have the performance crown always either due to language design choices. A big one is aliasing (which languages like Rust and Fortran forbid) which inhibits the ability for very impactful and common low-level optimizations that come up all the time. Performance characteristic differences between C vs C++ are not all that interesting because the language models are so similar (& thus similarly the compilers for them basically have the same model). Indeed, higher level of abstractions can help improve performance as virtual classes will outperform manual attempts at doing similar things (due to devirtualization passes within the compiler). That's actually been a friction point for Rust using LLVM because LLVM is built around the C/C++ language model and it's hard to express certain invariants to it that would result in even more efficient code or you try to use optimization passes that turn out to be broken because they're not really used by the broader C/C++ community (https://github.com/rust-lang/rust/issues/54878).
Re:Will Carbon Replace C++? by j16sdiz on Monday February 27, 2023 @08:53PM (#34964699)
> Priority #1 is performance, ..

No. It's not. It's designed by committee -- impossible to deprecate old misfeatures, trying to entertain everybody but please nobody.

Re:Will Carbon Replace C++? by hot_gril on Monday February 27, 2023 @09:10PM (#34964798)
Plenty of people are pleased with it. It gets the job done, if you're using it for the right job.
Re:Will Carbon Replace C++? by opportune on Monday February 27, 2023 @09:57PM (#34965112)
Deprecating old features is not reasonable for a widely used language. Just look at what happened with Python 3.
Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @02:04AM (#34966492)
Yep. If I upgrade my version and just a basic `print` no longer works the same, I go back.
Re:Will Carbon Replace C++? by skitter on Monday February 27, 2023 @09:07PM (#34964782)
As far as I can tell, Priority #1 is backwards compatibility. For example, std::regex can be slower than launching PHP and running the regex there¹, but that won't be fixed because it would require an ABI break. I'm not sure how that happened, as neither the poor performance nor the unwillingness to change ABI were unknown. Performance seems to not have been the top priority in the design of std::unordered_map either, which requires reference stability and therefore boxing. This likewise isn't something unexpected.

¹: https://cor3ntin.github.io/posts/abi/

Re:Will Carbon Replace C++? by opportune on Monday February 27, 2023 @09:55PM (#34965098)
You can implement your own libraries that do this easily. The std libraries have to maintain backwards compatibility and typically solve the base case only - just wrap them to do whatever ref counting or bounds checking you want
Re:Will Carbon Replace C++? by codemac on Monday February 27, 2023 @08:06PM (#34964398)

    <source>: In function 'int main()':
    <source>:6:54: error: conversion from 'absl::string_view' {aka 'std::basic_string_view<char>'} to non-scalar type 'const std::string' {aka 'const std::__cxx11::basic_string<char>'} requested
        6 |   const std::string foo2 = absl::StripAsciiWhitespace(absl::AsciiStrToUpper(foo)); // in Python: foo2 = foo.upper().strip()
          |                            ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ASM generation compiler returned: 1
This is gcc with c++20, curious what the garbage is you're describing.
Re:Will Carbon Replace C++? by hot_gril on Monday February 27, 2023 @08:26PM (#34964544)
Oh sorry, foo2 should be auto in this example (which becomes string_view). StripAsciiWhitespace takes a string_view here: https://github.com/abseil/abseil-cpp/blob/master/absl/string...

The garbage comes from AsciiStrToUpper returning a new std::string which StripAsciiWhitespace takes as a string_view (implicit conversion). By the time you print foo2, the string is already freed.

Re:Will Carbon Replace C++? by opportune on Monday February 27, 2023 @09:53PM (#34965087)
This is a classic misuse of rvalues so I’m not sure it’s a huge problem. I don’t think it’s a problem with C++. I probably work at the same place you do btw and I am pretty sure there are rules/guides about how to handle string_view lifetimes

For those downvoting, please explain why you think string_view should take ownership of an rvalue string

Re:Will Carbon Replace C++? by hot_gril on Monday February 27, 2023 @10:29PM (#34965291)
When you're writing a ton of web backend code dealing with strings all over the place, it's easy to forget to check whether something is accepting and returning a string or string_view. `auto foo2 = StripAsciiWhitespace(AsciiStrToUpper(foo))` doesn't say what's going on.

I didn't downvote, btw. I don't do that in general.

Re:Will Carbon Replace C++? by opportune on Monday February 27, 2023 @10:54PM (#34965422)
Heh this is normally why I tell my new teammates to be careful about using auto in non-test code outside of well established patterns like iteration. Auto will bite everybody in the butt at some point!
Re:Will Carbon Replace C++? by LaLaLand122 on Monday February 27, 2023 @11:17PM (#34965557)
And yet "Almost Always Auto" is "Modern C++"...
Re:Will Carbon Replace C++? by UncleEntity on Monday February 27, 2023 @11:53PM (#34965777)
Your local style guide can’t override this?

Just turn people loose on your codebase without supervision and be really surprised that the hackers make off with your data?

You, sir, are arguing from bad faith as your obvious mission is to promote “rust in all the places”.

Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @12:42AM (#34966064)
Our style guide says "almost never auto," but most people auto everything. They can make the linter replace auto with the actual type if it's really that big a deal. Explicit type wouldn't have avoided this bug.
Re:Will Carbon Replace C++? by LaLaLand122 on Tuesday February 28, 2023 @01:03AM (#34966202)
Arguably an explicit type would have made the bug a little bit more obvious. You basically expect, by default, this kind of string manipulation functions to return a std::string. Obviously in this specific case you can return a view, and that makes it more performant; but it's kind of unexpected and using auto leaves the surprise a bit more hidden.

But sure, you can still make the error with expiclit types.

Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @02:03AM (#34966485)
I agree, the string_view on the LHS might make you think more about the lifetime, but I'm pretty sure we all would've missed it.
Re:Will Carbon Replace C++? by afc on Tuesday February 28, 2023 @12:08AM (#34965856)
Citation needed? My team generally avoids auto and tends to spell out the types most of the time, which we find makes everything more readable. Code is written once but read many times...
Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @12:23AM (#34965943)
imo types don't make things more readable. You know the "fluent programming" thing where you do obj.doThis().doThat().doThis().doAnotherThing() and probably 20 more lines of this? That's entirely because people didn't want to bother writing out LHS types, but whatever version of the language only let you avoid that if you chained things (there was no auto or var). Maybe it's more of a Java practice, though.
Re:Will Carbon Replace C++? by LaLaLand122 on Tuesday February 28, 2023 @12:51AM (#34966119)
A citation of what's "Modern C++"? The term is a made up concept with no proper definition...

But "Herb Sutter says you should use it, he even gave it a catchy name/acronym" is as good as it gets. And he does so AFAIK in the same place he created the "Almost Always Auto" name: https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style...

Notice that he says "the main reasons to declare variables using auto are for correctness, performance, maintainability, and robustness—and, yes, convenience, but that’s in last place on the list.". He is arguing literally the opposite of what you do, that using auto avoids bugs ("for correctness").

I'm not going to argue in favour or against "Almost Always Auto". But I see no problem calling it "Modern C++".

* Herb Sutter being the convener of ISO WG21

Re:Will Carbon Replace C++? by iknowstuff on Monday February 27, 2023 @11:05PM (#34965489)
Classic C++
Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @12:18AM (#34965915)
We don't got time for non-auto. Better yet, we should've just written all our stuff in Python or Java. We have no legitimate need (performance etc) for C++, Rust, or even Golang.

Even if you use string_view on the LHS instead of auto, pretty easy to miss the bug here.

Re:Will Carbon Replace C++? by FpUser on Tuesday February 28, 2023 @06:14AM (#34968069)
Same here. I avoid auto in my code except iterations. Just a mental pattern. I understand that people coming from "typeless" languages might not like it at all, but it is like with guns. You can play with water pistol but you do not play with real guns, you follow some rules if you want it safe. Modern C++ in my opinion supplies enough features and tooling to help with making reasonably sure the rules are followed.
Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @03:27PM (#34974359)
auto is the least of your worries when it comes to memory safety in C++.
Re:Will Carbon Replace C++? by LaLaLand122 on Monday February 27, 2023 @11:12PM (#34965527)
It's good that you can solve this with 100% of the team following 100% of the time the rules/guides.

It's even better if your language has a way to express "the return value points to data from the input argument, so it's a compile error to pass a rvalue string to this function". The second we got a language able to do that, usable everywhere where C++ is, (yes, that one) the incapacity of C++ to express this became "a problem with C++". Our expectations have just increased.

Surely it can be catched via static analysis if you suppose the common case that the return value is a function of the argument, and not pointing to some static global data. But you will get false positives when somebody does the uncommon case. There is a lack of expressiveness in C++ here.

Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @12:20AM (#34965927)
The "oh shit" moment when we found that our database's indexes got mysteriously corrupted and we had no idea from where. What do we do, fire the entire team to get rid of whoever made the bug?
Re:Will Carbon Replace C++? by account42 on Tuesday February 28, 2023 @06:09AM (#34968029)
But C++ can express that, with compiler extensions:

https://godbolt.org/z/ohxaGzsEM

Re:Will Carbon Replace C++? by LaLaLand122 on Tuesday February 28, 2023 @08:18AM (#34968934)
That's cool.

Last I remember, the lifetime profile stuff was there, but there was still no way to add your own annotations. For some reason, I didn't hear too much about any of this, it was

- clang is working on it

- clang is working on it

- clang is working on it

- Visual Studio has it

- clang is working on it

- Silence

gcc still has nothing, right?

Re:Will Carbon Replace C++? by LaLaLand122 on Tuesday February 28, 2023 @08:38AM (#34969145)
I have just taken a look... https://wg21.link/p1179 is actually still not there, right? I see some interesting stuff in https://discourse.llvm.org/t/rfc-lifetime-annotations-for-c/... from last year. But, in general... once Herb started the whole Lifetime safety profile thing, there seemed to be a lot of interest, but for the last few years things seems to have died? I think things are better with Visual Studio, but being on Linux... I may need to start developing for Linux from Visual Studio with WSL2 :-(
Re:Will Carbon Replace C++? by pjmlp on Tuesday February 28, 2023 @12:01PM (#34971701)
Only VC++ is working on it, the clang tidy developer looking into lifetime has moved on.
Re:Will Carbon Replace C++? by LaLaLand122 on Wednesday March 01, 2023 @04:29PM (#34988605)
That's just sad.
Re:Will Carbon Replace C++? by arka2147483647 on Tuesday February 28, 2023 @12:49AM (#34966102)
All this talk of rvalues, auto, and string_view is PRECICELY the problem with cpp.

There are so many details you need to keep track of, and eventually you make a mistake. Now, yes, i know what all of that does, and how they are supposed to be used, but it does not remove the cognitive load.

Many languages have a substantially lower cognitive load when doing something trivial, such as ToLower().ToUpper(), etc.

Re:Will Carbon Replace C++? by opportune on Tuesday February 28, 2023 @02:11AM (#34966534)
Every systems-level language will have these sharp edges handled differently. Have you heard of borrow checkers? Many languages don’t have to concern programmers with rvalues or equivalents at all because they are garbage collected - should everything be garbage collected?

Let’s not make this a language war, there has to be some programming language that does low-level memory operations without batteries included for some types of technologies. When I write code in C++ and I want something like garbage collection or ref counting I can reach for a shared_ptr.

If you don’t want to concern yourself with types, values vs references, or manual memory management don’t choose C++. The default handling is sane if not necessarily intuitive. You shouldn’t create a ref or pseudo-ref (string_view) to data that is not on the heap and no longer allocated to the stack - seems sane. This problem could be easily caught by breaking function calls into separate lines and explicitly specifying the types at each step.

Re:Will Carbon Replace C++? by pjmlp on Tuesday February 28, 2023 @02:49AM (#34966752)
Many languages don’t have to concern programmers with rvalues or equivalents at all because they are garbage collected - should everything be garbage collected?

From Xerox point of view, yes. Unfortunely they lost to the UNIX workstation market.

Interlisp-D, Smalltalk and Mesa/Cedar.

Re:Will Carbon Replace C++? by brobdingnagians on Tuesday February 28, 2023 @09:05AM (#34969395)
There might be a reason they lost.

Believing that one size fits all is a sure way to alienate a lot of people who don't fit that size. Right tool for the right job and giving the freedom for people to solve their problems in the best way is a better way to win people over.

Re:Will Carbon Replace C++? by pjmlp on Tuesday February 28, 2023 @11:58AM (#34971653)
Free beer UNIX on cheaper hardware, plus bad management, basically.

Nothing to do with the technology.

Hence why I love why Android is so locked down to native code, and Apple is doubling down on Swift.

Re:Will Carbon Replace C++? by hot_gril on Tuesday February 28, 2023 @12:02PM (#34971713)
Yeah, you shouldn't have GC in systems-level code any more than I should have lifetime concerns in application-level code. Problem is leaders end up choosing C++ for both and saying y'all should know how to deal with it.

Google also has a lot of stuff in the middle where they're fine with sacrificing a little speed for a lot more safety, but Java etc are too slow. Golang was supposed to satisfy that, but it was too big of a leap from C++.

Re:Will Carbon Replace C++? by arka2147483647 on Tuesday February 28, 2023 @03:30PM (#34974396)
> Every systems-level language ...

Is Cpp systems-level language? As I see, the problems arrives from the fact that Cpp is a multi-paradigm language, and as such contains a near endless wealth of features to combine. Also modern Cpp takes a clear direction away from low level to (relatively) more high-level language. The problem really comes from the combination of low level and high level paradigms in the same language/codebase, where the expectations of the behavior of each piece of code can vary wildly.

> ... will have these sharp edges handled differently

Perversely, plain C, often has a lower cognitive load than Cpp. This is because you must always manually handle pointers, types, object lifetimes, etc. Now, this does result in a high cognitive load, BUT, the cases as in Cpp where you might combine auto, with a temporary, with a pointer inside, referring to some unseen resource, cannot happen, because you cant do that in C. So the worst case in cognitive load is never as bad as in Cpp. The lack of features means less possible combinations to shoot yourself in the foot. (Though C does still have many, nor is C an ideal language)

> Let’s not make this a language war

Discussing the problems of Cpp is not language war. Nor is understanding the merits and problems of the Design of Cpp.

> If you don’t want to concern yourself with types, values vs references, or manual memory management don’t choose C++

But IF YOU DO write Cpp, there is no escaping them. Which is my point.

Re:Will Carbon Replace C++? by Camillo on Monday February 27, 2023 @08:18PM (#34964479)
Not to detract from your general point, but are you sure your example corrupts memory? AsciiStrToUpper returns a std::string (it has to), which in your example becomes a temporary object. Temporaries are destructed at the end of the containing full-expression, which in this case is the whole assignment expression. So StripAsciiWhitespace returns a view into a still-living temporary, and foo2's constructor allocates memory and makes a copy. Only then is the temporary deallocated.

Now, if you wrote absl::string_view foo2 = etc., you'd have a dangling view for sure. In practical industrial usage, you'd build with an address sanitizer (it's built into clang: -fsanitize=address), which should catch that issue.

Re:Will Carbon Replace C++? by hot_gril on Monday February 27, 2023 @08:30PM (#34964561)
I missed one important little piece in my example, should be `auto foo2` or `string_view foo2` and not `string foo2`, as you've noticed. There's an overloaded func taking string_view and returning string_view.

We use an address sanitizer in tests but not in production. IIRC we had one untested log output line like this that caused corruption in prod.