News for hackers. Stuff that matters.
Re:Modern C++ for C Programmers (2018) by systemvoltage on Friday July 23, 2021 @08:16PM (#27937238)
What's the big deal with C++ being bloated (Linus, many experts don't like it) and I keep hearing that it has become unmangeable?

C++ doesn't force you to use everything it's got. Can't I just write basic C style procedural code in C++ and use OOP constructs if necessary? It is completely optional I think. Don't like templates? Don't use them. Set a policy in the team/company to avoid using advanced C++ features.

Re:Modern C++ for C Programmers (2018) by ncmncm on Friday July 23, 2021 @09:51PM (#27937758)
Avoiding the newer parts is the worst imaginable response.

If you write C code in C++, you will only have C code, and nothing better than C code. If you write Java code in C++ (which you can, and some do) you will only have Java code, and nothing better.

Leave the C parts behind. Leave the O-O parts behind. Lean into Modern C++, and reap the benefits of all the work put in to make the language better than C, better than Java (i.e. pre-C++98 plus GC), better than C++98, better than C++11, better than C++14, better than C++17.

Pointer arithmetic and inheritance are still occasionally useful; you can still wrestle in the mud. But if they show up in more than a few places in a program, you are Doing It Wrong.

Re:Modern C++ for C Programmers (2018) by systemvoltage on Friday July 23, 2021 @11:01PM (#27938063)
I’m not convinced about your counter argument, let alone worst imaginable response.

All I’m saying is if a new feature is added to C++, it doesn’t affect me and I can choose to pick which features are useful. I can still keep it absolutely minimal. C with some useful libs, and occasional OOP.

Why this is the worst possible answer? Obviously it works for me, it makes sense to me and it is perfectly logical in my view.

Others have said the language bloat creates burden when you read other people’s code. That makes sense to me.

Care to elaborate more? If you want to harshly criticize, it’s fine but you need to say a bit more so I can understand where you’re coming from. I just see a bunch of rhetorical statements.

Re:Modern C++ for C Programmers (2018) by ncmncm on Friday July 23, 2021 @11:14PM (#27938107)
Nothing is added to C++ without demonstrating usefulness. There is no "language bloat". If you fail to find any of it useful, it can only be by not paying attention.

Yes, you can limit yourself to the C and Java subset, just as you can drive everywhere in first gear, can eat all your food raw, can crap in a hole. Just because you can does not make it a good idea. Objectively, the more your code looks like C, the worse it is, because you are inviting into it all of the flaws endemic to C code.

Re:Modern C++ for C Programmers (2018) by dralley on Friday July 23, 2021 @11:28PM (#27938163)
>> Nothing is added to C++ without demonstrating usefulness. There is no "language bloat"

"Bloat" doesn't require that the features are literally useless, merely that their usefulness doesn't justify their added complexity.

And for many people, a huge portion of C++ falls into this category.

Just look at the 18 different methods of initialization for an example of this. Or look at ranges, which seem to simultaneously be far more verbose than the alternatives in every other language, while having measurable negative impacts on compile times (that have spread throughout the stdlib, so even if you don't use them you have to pay for them). Ranges are certainly "useful" but the cost/benefit ratio is questionable.

Re:Modern C++ for C Programmers (2018) by ncmncm on Saturday July 24, 2021 @01:58AM (#27938827)
Ranges had to be implemented without using the new language features that would make it easy and clean. Its authors had to use template-metaprogramming substitutes that are slow. As the language implementations catch up, the library can be upgraded and will get faster to compile.

There are new initialization modes that are simpler. The old ones still have to work. But you don't have to use the old ones anymore.

Re:Modern C++ for C Programmers (2018) by thesuperbigfrog on Friday July 23, 2021 @11:30PM (#27938177)
>> Lean into Modern C++, and reap the benefits of all the work put in to make the language better than C, better than Java (i.e. pre-C++98 plus GC), better than C++98, better than C++11, better than C++14, better than C++17.

Serious questions:

What subset of C++ is "Modern C++"?

Are there flags for C++ compilers to only allow modern C++ and generate warnings when non-modern C++ is used? (For example, if an older-style C++ idiom is used, the compiler might say: "use of Old_Idiom is deprecated. Consider using New_Idiom instead".)

I don't use much C++ and I have found the language daunting to learn because it is large, complex, and full of gotchas. A better subset that is enforced would help newcomers not get lost.

Re:Modern C++ for C Programmers (2018) by ncmncm on Saturday July 24, 2021 @01:00AM (#27938520)
There are not such flags, but they would not be useful if there were. Real C++ programs link code of many different ages. Generally, you can only afford to make the new code, that you are writing now, the new way, and maybe modernize stuff immediately around it. The old stuff is typically well-exercised, and as long as it does its job you have no reason to touch it or, often, see it.

But it is for the new code that the most value is available.

Generally, if there are two ways to do something, and one appeared in a recent Standard, using that is usually the better choice. Cppreference.com helpfully tags each bit with when it appeared.

Re:Modern C++ for C Programmers (2018) by gandvor on Saturday July 24, 2021 @02:01AM (#27938836)
Regarding modern check flags: clang-tidy has its modernize-* flags
Re:Modern C++ for C Programmers (2018) by bluGill on Friday July 23, 2021 @09:11PM (#27937547)
> Can't I just write basic C style procedural code in C++

You can, but the bloat Linus is complaining about comes from the ability to do that. C is a real mess and is a large part of what is holding C++ back from being simpler (for both better and worse). C seems simple, but you lose a lot of abstraction ability when you use it.

C++ seems a lot more complex, but that complexity allows you to write abstractions that do exactly what you need. Modern C++ is the discipline that lets you write good abstractions, with exactly the control needed.

Don't take the above as some sort of statement that C++ would be simple with C - that is completely untrue. Only that C++ is moving to giving people the power to write the simple and powerful abstractions they need - but the nature of those abstractions are very complex and that cannot be avoided if you need to write them.

> use OOP constructs if necessary

THIS IS FALSE TO THE CORE! C++ is NOT about OOP. C++ is about writing the style that makes the most sense. OOP is good for a subset of problems, but a lot of problems can be solved better by not using OOP, and C++ supports that. If you think of C++ as C with classes you are making a large mistake.

Re:Modern C++ for C Programmers (2018) by gh02t on Friday July 23, 2021 @08:26PM (#27937312)
Workable when you control the whole codebase, not so much when you have to deal with external dependencies or huge piles of legacy code that you're stuck with. I quite like writing C++ when it's just me or I have tight control, not so much when I have to deal with some of the large projects with code spanning 20-30 years of history and dozens of developers (some of which died before I even started my job) that I've worked on where it wasn't my call. Not that I have any specific traumatic experience in mind...

Admittedly such a situation is not entirely due to C++, but it didn't help. But again, I do quite like C++ especially nowadays.

Re:Modern C++ for C Programmers (2018) by systemvoltage on Friday July 23, 2021 @08:30PM (#27937329)
I was thinking of that - I agree that's a problem. In my work, we have some basic dependencies and not much else - Boost libs and that's all.

May we should create a new subset of C++. It will be called C+.

Re:Modern C++ for C Programmers (2018) by dilyevsky on Friday July 23, 2021 @09:41PM (#27937702)
Only takes a couple folks who want to use all the esoteric features of c++ at your workplace in a culture where nobody can tell them “no” (which is increasingly common) and big chunks of your codebase can evolve into unmanageable mess. Tbf i found the same to be true with Python but not with languages like Java, C# and especially Go
Re:Modern C++ for C Programmers (2018) by pjmlp on Saturday July 24, 2021 @02:02AM (#27938840)
Go would be modern in 1992, not 21st century.

While C# is now on version 10, and Java on 17, if we account for the releases in 2021, the large majority of the world is still delivering .NET Framework (C# 6)[0] and Java 8 code into production.

When I use modern C# and Java, always need to give a mini-lecture on current state of the world.

[0] There are ways to use newer C# version in .NET Framework, but with some caveats.

Re:Modern C++ for C Programmers (2018) by dilyevsky on Saturday July 24, 2021 @02:33AM (#27938923)
Go 17 is being released in aug so clearly it’s almost as modern as java. I for one is jelly of innovative features such as gc tweaking whackamole and never expiring dns ttl default
Re:Modern C++ for C Programmers (2018) by pjmlp on Saturday July 24, 2021 @03:23AM (#27939061)
Just wait for when Go 1.18 finally catches up with CLU, now that is blazing modern!
Re:Modern C++ for C Programmers (2018) by ratorx on Friday July 23, 2021 @08:22PM (#27937280)
It’s not just about writing code though. If you want to properly read code you have to understand all the “advanced” features they have used.

I’m reasonably familiar with C++, but not so much with arcane template meta programs. I never write templates, but it still trips me up from time to time when I have to understand one.

Re:Modern C++ for C Programmers (2018) by truncate on Friday July 23, 2021 @08:31PM (#27937343)
Yeah. All the arcane template stuff, even if I understand for sometime, you're bound to forget it as soon as you stop using it (which you probably should in most times).

It reminds me, every time I try to jump too implementation of some standard library method/class, it just seems like a completely different language to me. And C++ is the language I've most experience with.

Meanwhile, I jump to some Rust or Golang* standard library implementation, it makes perfect sense very quickly. Even though I just use them for hobby projects.

* Go is probably a bad example because it doesn't have any generics. But still love how much more approachable their stdlib code is.

Re:Modern C++ for C Programmers (2018) by beached_whale on Friday July 23, 2021 @10:13PM (#27937851)
A lot of that is that you have to protect against name collisions. You have things like c macro's so methods need to be wrapped in brackets{e.g (min)( a, b ); } and also internal names need to be prefixed with an underscore and start with a capital (e.g _Foo) as that is reserved.

The global namespace still exists so unqualified names that do not have a reserved id( eg _Foo ) cannot be used and need to be qualified (e.g ::std::(min)( a, b );). All this to protect against us.

Some codebases are easier to read, I would rank them from libc++, MS STL, libstdc++ is readability. libstdc++ also seems to have code all over the place and use far more inheritance for composition of things.

Re:Modern C++ for C Programmers (2018) by ncmncm on Friday July 23, 2021 @10:48PM (#27938009)
There is no reason to use "arcane template meta programs" anymore. That is all old-C++ stuff needed to work around missing language and library features. It has been literally years since I coded or even (visibly) used any template meta program construct.

That's not to say it doesn't exist in the implementation of the Standard Library. I just never have any need to see any of it.

Re:Modern C++ for C Programmers (2018) by rightbyte on Friday July 23, 2021 @08:29PM (#27937325)
I guess the messy ugly template headers could be hidden as compiler intrinsics, like in Go with its compiler blessed generics for standard containers. Or how C's printf is implemented as compiler magic instead of vardiac macros.

The complexity needs to be somewhere in the end. A charitable view would be that Cpp's approuch is more ... inclusive?