News for hackers. Stuff that matters.
Re:Modern C++ for C Programmers (2018) by galangalalgol on Friday July 23, 2021 @09:46PM (#27937732)
What is our option now? Rust? I love it but it is moving 5x the rate of c++. Static analysis may save us in this one. Good tools will tell you when you did something the "old" way. I'm not sure which part of our pipeline that is. Cppcheck, coverity or some clang checker probably.
Re:Modern C++ for C Programmers (2018) by dralley on Friday July 23, 2021 @11:21PM (#27938128)
>Rust? I love it but it is moving 5x the rate of c++.

I'm not sure this is true. It has been a year since the last really significant change to the language, whereas C++ is adding Ranges, Concepts, Modules, and so on and so on. The pace of change has slowed way down in Rust and sped way up in C++, it almost feels like C++ is moving "faster" now or they are at least close to matching pace.

But a lot of the Rust efforts at the moment are really just closing holes in the language - things that you would expect to be able to do, but haven't been able to do due to compiler limitations. Generic Associated Types and Const Generics both kind of fall into that category. The rate of true "new feature" development is way down.

Re:Modern C++ for C Programmers (2018) by Animats on Friday July 23, 2021 @11:15PM (#27938112)
Rust? I love it but it is moving 5x the rate of C++. Static analysis may save us in this one.

Yes. Rust is complicated, but the compiler is unusually good at telling you what you did wrong. Before it results in a crash.

The underlying problem with C/C++ is that it still doesn't have a decent array story. " char * " is so 1970s. That can't be fixed without breaking so much. All you can do is try to paper over the problem with templates. But the mold always seeps through the wallpaper. Look at the example in the original posting that reads:

    FILE* fp = fopen(fname.c_str(), "r");
There it is, access to a raw pointer. Why is that even allowed any more? You ought to have to wrap "Unsafe" around it, or something.*
Re:Modern C++ for C Programmers (2018) by clappski on Saturday July 24, 2021 @04:44AM (#27939304)
C++ has a great array story - `std::array<char, N>`. `fopen` is a C API, so yeah you have to pass it C-strings. Note that Rust has to do the exact same thing (albeit calling `open64` rather than `fopen`), you can see it in the source for the stdlib here https://github.com/rust-lang/rust/blob/master/library/std/sr...
Re:Modern C++ for C Programmers (2018) by MaxBarraclough on Saturday July 24, 2021 @04:44AM (#27939305)
> Why is that even allowed any more? You ought to have to wrap "Unsafe" around it

There have been a few different projects aiming to introduce 'proper arrays' into C, but they never get traction, even when carefully designed for good backward compatibility. C is seemingly just too resistant to change. Here's Walter Bright's suggestion. [0]

Aside: why is the word story so popular on Hacker News these days? We can just say C doesn't have decent arrays. We aren't discussing its history, we're discussing a specific technical facet of the language.

[0] https://www.digitalmars.com/articles/C-biggest-mistake.html

Re:Modern C++ for C Programmers (2018) by shaklee3 on Saturday July 24, 2021 @01:17AM (#27938613)
Because it needs to support 40+ years of legacy code. What's wrong with the more modern array types?
Re:Modern C++ for C Programmers (2018) by habibur on Saturday July 24, 2021 @02:43AM (#27938958)
Because the underlying OS API expects a char* as file name.
Re:Modern C++ for C Programmers (2018) by nyberg on Saturday July 24, 2021 @05:40AM (#27939500)
Rather, the underlying OS requires an array of bytes terminated by 0 which can be hidden behind a decent API. The type being poor at expressing this is not a limitation of the OS but of the C type system. E.g in zig this would be `[*:0]u8` which is "a pointer to a region of memory of unknown length terminated by a 0 sentinel value". It's still a pointer but nothing says the type system can't track more state around it.
Re:Modern C++ for C Programmers (2018) by Animats on Saturday July 24, 2021 @02:15PM (#27943195)
So why isn't the Linux API properly encapsulated for C++? Rust does that.

C++ got off on a weird I/O direction. That

   file << item << item;
syntax never really caught on. Someone liked Currying too much.
Re:Modern C++ for C Programmers (2018) by sseagull on Friday July 23, 2021 @10:14PM (#27937853)
I would love compiler options for enforcing better practices and prohibiting old style code (no using bare new/delete, stricter implicit casts, etc.) It wouldn’t fix everything but would go a long way.

It’s a little bit of a peeve of mine that people just decided on new languages instead. But it is what it is, and I am probably not knowledgeable enough to really judge.

Re:Modern C++ for C Programmers (2018) by ReactiveJelly on Friday July 23, 2021 @11:34PM (#27938194)
You can think of C++17 as a new language which happens to be able to compile most C++14, C++11, and C++98 programs.

The trouble is, if every new version is _technically_ its own new language, and if you have to break compatibility a little, why not just break a lot and fix everything?

It's not like C++ was ever known for ABI stability anyway.

Re:Modern C++ for C Programmers (2018) by sseagull on Saturday July 24, 2021 @12:00AM (#27938289)
The benefit is that ability to compile that old code, even if it takes some flags or marking it unsafe or whatever.

There is a lot of old code that will never be rewritten but is still valuable, and I believe we will waste a lot of time just language-shifting entire ecosystems as new languages come and go.

Look at python 2 to 3. There are still packages out there on python 2 that haven’t been migrated. And thats mostly the same language!

(Note that my view is colored somewhat because I am a scientific programmer)

Re:Modern C++ for C Programmers (2018) by alephu5 on Saturday July 24, 2021 @02:30AM (#27938918)
For those that don't want to upgrade, why not use a pinned compiler? Everyone else does this with other languages, and overall I think the benefits outweigh the costs.