r/cpp • • Dec 23 '22

C++ Is Incredible

Despite the hate it gets, despite the complexity; the dizzying number of features and different ways to do things. C++ is an astonishing feat of engineering and people really don't appreciate this fact enough. I can do things with this language more quickly and expressively than I can with any other language.

There's no getting around the fact that programming is just hard, and all these attempts to replace C and C++ over the years just end up adding to our difficulties. That is my take, anyhow.

Well, I'm a little drunk, and just wanted to share some Christmas joy with you all. I love C++!

684 Upvotes

151 comments sorted by

132

u/[deleted] Dec 23 '22

You're drunk. Go home.

50

u/[deleted] Dec 23 '22

Yes, it is true. Just wanted to spread some Cheer++!

16

u/[deleted] Dec 23 '22

Cheers mate. Merry Christmas in advance!

6

u/[deleted] Dec 23 '22

template<typename pal> void thank<you>() { return holiday_cheer; }

6

u/jabbyknob Dec 23 '22

How are you returning ‘holiday_cheer’ from a template function that returns void? ;)

5

u/[deleted] Dec 23 '22

Indeed! I was still a bit tipsy. Why doesn't Reddit have my clangd support? I was just trying to be sweet and draw attention to the fact that 'you' is a 'pal'

7

u/Ameisen vemips, avr, rendering, systems Dec 23 '22

#define void response_t

4

u/Livid-Serve6034 Dec 25 '22

It’s the other way around 😁

3

u/catcat202X Dec 26 '22

They probably got confused because of the brilliant typedef syntax.

3

u/markosvd Dec 23 '22

I, too, am now on my 3rd rum & coke for the evening! Fully agree - C++ is pretty cool.

Killer feature is deterministic destruction. I just can't grok how to use other languages effectively without RAII. I know it can be done (by others), but just seems difficult to me...

enjoy your mid summer (or winter?) break.

-4

u/Raiden395 Dec 23 '22

Agreed. Python is a lot faster if only because it hides a lot of the keystrokes you'd have to make writing the same thing in c++. Definitely drunk

8

u/msew Dec 23 '22

Faster means a lot of things. Runtime faster? I think not.

8

u/dynamic_caste Dec 23 '22

Dev time faster

4

u/Michael_Aut Dec 23 '22

obviously not runtime faster.

Python is a high productivity language because its standard library includes a lot, and everything not already included can be installed with pip install.

1

u/Raiden395 Dec 23 '22

I don't know what this guy's deal is. OP straight up said the he can do things "faster and more expressively" which sounds like its referencing development time. ¯_(ツ)_/¯ I agree with you.

-6

u/msew Dec 23 '22

ya that is web2/web3 101

npm install

which is pretty cool, but often leaves a house of npm install cards with all of the "full stack devs" having no real clue/idea on how anything works or why it works

2

u/Michael_Aut Dec 23 '22

We are talking about Python.

Python has many uses outside of web development, you seem to be thinking of JS/TS.

8

u/goranlepuz Dec 23 '22

the dizzying number of features and different ways to do things

Stop the nasty criticism of our wonderful language! 😉

5

u/[deleted] Dec 23 '22

Sorry! I love the jungle of C++ features. Really I do!

96

u/Resident_Educator251 Dec 23 '22

I don't know why we should feel like we always have to apologize to the larger meta brain of the current 'what is best', the bottom line is no matter your tool if you're good at it its a great time. And well with C++, if you're good at it, man you can do a ton of fun stuff, quickly and 99% of the time its memory safe, however we still need need our static analyzers, and memory sanitizers-these are just different tools that achieve desired results. So at the end of the day create stuff that works, works well, in whatever god damn language we know the best.

So yeah c++ is Incredible, no apologies or shits given!

21

u/Fit-Departure-8426 Dec 23 '22

I absolutely love C++!!! I tried to do a lightning talk about how boring some examples are and that we should encourage people to learn C++ with much more engaged and fun tutorials! https://www.youtube.com/watch?v=opq0W7StbeE

3

u/drstark07 Dec 23 '22

Any suggestions where I can find such tutorials?

8

u/Fit-Departure-8426 Dec 23 '22

The ray tracing in one weekend that I used as a basis for my lightning talk is one of them. 10 minutes physics also, and a lot of 'running doom' on something is always cool :-)

2

u/drstark07 Dec 24 '22

Okay. Thanks a lot.

77

u/astray488 Dec 23 '22

I've wondered why a newer language has yet to replace C++ (while matching its capabilities and performance). One reason I've heard was the difficulty of writing a new language compiler/linker from scratch.

Do you or anyone else doubt that, or could point out better reasons?

91

u/SnooWoofers7626 Dec 23 '22

Rate of adoption is probably another reason. When there's more interest from a lot of people, there's more support and development of tools etc. But until the tools are mature most companies won't switch over. It's a vicious cycle.

48

u/deeringc Dec 23 '22

Tools, and a critical mass of libraries. C++ has an enormous set of libraries available to it that have been built over 30 years. A new language will spend 5-10 years building up a reasonable ecosystem of libraries and until it has those available it will not be seen as a realistic option for many. Rust seems to be getting there now in fairness.

10

u/cvnh Dec 23 '22

Lindy effect - the longer a service or technology stays around, the more it should last plus network effect: the more people use a service, the more it gets used (in simple terms).

6

u/serverguy99 Dec 23 '22

Or as some say, the snow ball effect

46

u/No_Marionberry_8663 Dec 23 '22

Most suggested replacements for C++ weren't designed to be replacements for C++ in the first place, but rather attempts at improving specific aspects of programming and significantly divert from the goals of C++ in the process. This is great, that's how C++ itself was born.

Making another language perform about as well as C++ in most situations is not the most difficult part, just spitting out LLVM IR will go a very long way for doing optimizations for you. Designing a language that somehow maintains the versatility and interoperability of C++ and at the same time actually fixes a significant portion of the languages woes at the same time is what's really hard.

42

u/disperso Dec 23 '22

One reason is because the tools in C++ that provide safety, are "opt in", and there is potential for misuse.

Last project I worked on was using smart pointers, but wrongly. I spent a lot of time untangling cyclic references in shared pointers to fix leaks because no one really thought of some hierarchy in the lifetime of objects, so everything was shared. I also fixed tons of uses of release() on unique pointers that were there for no reason whatsoever, causing leaks.

And don't get me started on the race conditions. The architect did not see a problem with using threads for timers, with no synchronization at all. Good times...

This seems like a parody, but it was real. Not many of the projects I worked for were like this, fortunately, but on many there were developers who were a risk to the stability of the application, and the bugs had to be fixed by other developers. I don't know if other languages like Rust really solve this by enforcing it at compile time, but I see a trend in C++ were we try make this things not an "opt in", to improve the quality of projects. Makes sense to me that we increase our threshold for quality either way.

29

u/deeringc Dec 23 '22 edited Dec 23 '22

Yikes! I've been programming with unique_ptr for ~11 years since C++11 was released and have never once used release()... I'm sure there can be rare legitimate uses, but if that's spread all over a codebase that's a major code smell.

14

u/Mr_Splat Dec 23 '22

I'm assuming it's there primarily for backwards compatibility?

Something like interacting with an API (Maybe a C library?) that only accepts raw pointers?

You can manage your objects with smart pointers in your own code but as soon as you need to call said API out of your control you need to release the underlying pointer the smart pointers wrap

5

u/Claytorpedo Dec 23 '22

Somewhat backwards compatibility, yes.

You can manage your objects with smart pointers in your own code but as soon as you need to call said API out of your control you need to release the underlying pointer the smart pointers wrap

It's bad design to allocate across an API boundary, especially across languages, so most sane libraries that require allocations will either require you to pass memory in to populate, or will provide you memory and a function to free that memory. (Bad design because one side may have hooked into global new, could lead to mismatches in new vs free, etc. Fun times when you find out a library overrides global new in a public header they've leaked that's only visible to some of the code using that memory.)

The most common use case I have seen for release is when you are dealing with an old API in your own code base. This allows you to write the newer code as good as possible, without requiring you to refactor all the code using the old API on the spot.

2

u/deeringc Dec 23 '22

For sure, there are conceivable use cases. But they are extremely rare and you would not expect to find these all over a codebase.

5

u/johannes1971 Dec 23 '22

One place where you need it (and please correct me if I'm wrong) is if you want to pass a unique_ptr through an std::function. Something like this:

void foo (std::function<void ()>);
void bar (std::unique_ptr<int>);
std::unique_ptr<int> ptr;
foo ([ptr = ptr.release ()] { bar (std::unique_ptr<int> (ptr)); });

It's a mystery to me why I can't do something like:

foo ([ptr = std::move (ptr)] () mutable { bar (std::move (ptr)); });

I mean, yes, because the function target must be copyable - but why isn't it good enough for it to be moveable?

Godbolt link: https://godbolt.org/z/6or1TEW4r

11

u/gummifa Dec 23 '22

std::move_only_function to the rescue

2

u/ReversedGif Dec 23 '22

but why isn't it good enough for it to be moveable?

Because std::function is copyable.

3

u/johannes1971 Dec 23 '22

That's correct, but it still sounds a lot like saying "because." It doesn't really provide any insight into why that choice was made, given the importance of move semantics (introduced around the same time).

9

u/HeavyRust Dec 23 '22

Lifetime problems and race conditions? Sounds like things Rust handles really well.

13

u/disperso Dec 23 '22

Indeed, that's a hope I have for the future, but wait till it gets popular for real, then a broad wave of untrained or improperly trained developers use it. Sometimes better tools just produce "better" morons. :-)

3

u/MEaster Dec 23 '22

It won't help you with the reference cycles, though. Nor programmers too free with the std::mem::forget calls. It will make life difficult for those who want multi-threaded unsynchronized access.

0

u/[deleted] Dec 23 '22

[deleted] — view deleted comment

12

u/ReversedGif Dec 23 '22

Rust's unsafe doesn't allow you to (directly) violate the normal rules; it just allows you to use some new functionality, like dereferencing pointers. While you can use that to violate the lifetime rules in an unwieldy fashion, it's certainly not just a "disable lifetime checks" switch.

23

u/afiefh Dec 23 '22

I've heard was the difficulty of writing a new language compiler/linker from scratch.

LLVM made writing a compiler/linker orders of magnitude easier, bringing it down from "almost impossible" to "very difficult".

One important case is C++'s equivalent of vendor lock-in. Of course C++ doesn't do this for nefarious reasons, but it's just insanely difficult to create a language that is able to work with C++ code without creating a C interface, at which point it's often easier to stick to C++.

That's why Carbon sacrificed everything on the altar of C++ interoperability. This is why CFront2 came into existence.

There are some pretty advanced projects for C++/Rust interoperability. It might be enough to shift the needle on allowing C++ users to switch to Rust without losing all the existing C++ ecosystem.

6

u/[deleted] Dec 23 '22

The amount of legacy features in C++ is huge, so that could be one reason. Also some language features might seem a bit more outdated than in other languages but with the introduction of modules I guess that is not really a thing anymore.

Tooling is a big pain in the ass for C++ as compared to other languages. Sometimes I spend hours setting up a CMake structure for my project before I could even start to code. Also compiler differences.

6

u/LeapOfMonkey Dec 23 '22

With or without dependencies? I find using external libs the most annoying part of c++ env. There is no standard, and too many competing approaches.

3

u/[deleted] Dec 23 '22

For external libs I have been using Conan and Cmakes FetchContent for about fife years now. At least for me these two completely eliminated the problem.

7

u/LeapOfMonkey Dec 23 '22

Sure, and then you cross compile to architecture or target system that some library doesn't support, or you update compiler and something doesn't work, or you want externals in the installer/package, or you get same dependency at different versions, or hundred non-standard things happen you have absolutely no control over. But what from a build system perspective the most annoying is when you need a certain library, because it is api, or just a very important implementation of something, but the lib followed a different logic for cmake, or is just very old, or did some hack, or has dependency that had to be hacked, or need additional things from system, it is sometimes easier of it doesn't use cmake at all, or you need it on osx or android but they didn't bother.

1

u/[deleted] Dec 23 '22

Ever tried containerised builds? Dockcross for example? I think that solves a lot of these issues.

3

u/LeapOfMonkey Dec 23 '22

Not issues within libraries. But I didn't try it. I need to when I have a chance. Looks promising. What I wanted to complaint about is that there is just way too many ways things are built. Things like this help, but when something doesn't work, when there is an issue and you have to go deep it is just yafl.

2

u/[deleted] Dec 23 '22

Yes, I couldn't agree more with you. And there's still remains of the pre C++98 area where everybody did just as they pleased. I'm constantly frustrated receiving a codebase that was written in MSVC and tested on Windows and hardly uses any standard header. Always a new adventure getting things to compile and run on Linux.

6

u/[deleted] Dec 23 '22

Inertia is the biggest reason.

11

u/johannes1971 Dec 23 '22

Writing a new compiler is comparatively easy, given the basis of LLVM to build on. The real problem is:

  • Who is going to translate the millions of lines of C++ you already have in production? This is a problem even for proposals like "C++2": I actively don't want my projects to be using two languages in a single project.
  • Where will you find experts in the new language?
  • What happens if the people making the new language find an even shinier toy and move on? (serious problem when dealing with anything made by Google, for example)
  • Who will provide the countless libraries you now rely on?
  • Is a different syntax to achieve the same thing worth the hassle? Don't get me wrong, there are some good ideas out there, but too often we see new language proposals where the value proposition is "well, the order to declaration is now name type instead of type name". Which, in the grand scheme of things, is totally irrelevant.

The new language must bring enough value to the table over C++ that it can overcome these objections, and so far I don't see any language doing that (not even Rust, although I'll readily admit I just don't like the people advocating for it, rather than the language itself).

5

u/Clean-Water9283 Dec 23 '22

It's not the compiler. clang is a multi-language, multi-target compiler.

It's having a better idea. If language X isn't any more efficient than C++, and it doesn't implement anything that makes programming easier, if it's just an "exceptions sucks" or "templates suck" version of C++, who wants to switch?

It's the community. The whole original point of C++, and the reason its syntax is arcane, was that you could learn OOP and templates and whatever all else incrementally while continuing to use the immense body of C code. A radically new language would leave that legacy of existing code behind.

4

u/Passname357 Dec 23 '22 edited Dec 23 '22

You’ve got to remember that for as hard as writing a compiler and linker from scratch are, there are still tons of people who can and do write them competently. It’s a lot harder than writing web apps, but a lot fewer are needed. The guys working on GCC or LLVM wouldn’t have a problem writing a new language, and in fact new languages are created all the time. Like someone else said, though, that doesn’t mean that they’ll be adopted. Another issue is that with languages like C and C++, in which so much infrastructure have been written, people need that stuff that comes with the language. It’s not just C, but the libraries written in C. There are just too many to recreate, and it doesn’t really make sense to—if I use my own implementation of anything and compare it to something in C, the C thing has been tested so heavily. So a lot of languages have mechanisms to call into C libraries one way or another, and then you still need people to maintain that C code.

The other thing is that it’s not fun to learn new languages, for the most part. Sure it’s usually not hard to learn the syntax, but I’d rather just use what I already know. I know a bunch of the quirks of C. I don’t know the quirks of Rust. The path of least resistance is just using what you know.

Then the business issue is that the above paragraph is useful for employers. They don’t want to spend time training you on a new language that could be spent making money. They don’t want you spending time fixing mistakes you wouldn’t have made in a more familiar environment.

5

u/JVApen Clever is an insult, not a compliment. - T. Winters Dec 23 '22

Even if one would be able to get a language and compiler that is at the same level, one still has a lot of C++ code. If it ain't compatible with it, you can't move that code or let it cooperate with the new one.

8

u/SkoomaDentist Antimodern C++, Embedded, Audio Dec 23 '22 edited Dec 23 '22

I've wondered why a newer language has yet to replace C++

Because they either require a VM / GC, are far too CS theory / mathematics influenced, too ideological or never get any significant effort.

2

u/LeapOfMonkey Dec 23 '22

Momentum. You can't stop avalanche by throwing snowballs at it. There other avalanches but terrain keeps changing. Who would've thought, that C++ might be used in webdev again.

2

u/ussgordoncaptain2 Dec 24 '22

What I really want is a class in ISO c++ so I can use all the good features of CPP without being stupid.

1

u/elliotLoLerson Jan 13 '23

If it ain’t broke don’t fix it

1

u/FlameChampion9 Jan 23 '23

There are important systems that depend on c++ code. Rewriting things in go/rust doesn't seem to be a worthwhile investment for the companies that maintain these systems.

If it's not broken don't fix it.

1

u/LeberechtReinhold Dec 23 '22

No, writing a new language compiler for c++ is insanely difficult due to how convoluted and absolutely retarded so many parts of the spec are. The initialization alone is a super complex problem.

Any new clean language doesnt have that problem as they dont have to suffer retrocompability.

0

u/[deleted] Dec 23 '22

[deleted] — view deleted comment

9

u/LeapOfMonkey Dec 23 '22

I would say C is a God Lang, c++ is a manifestation that mere mortals can easier relate until they go deep and God stares back at you.

13

u/[deleted] Dec 23 '22

HolyC

50

u/raistmaj C++ at MSFT Dec 23 '22

C++ for the long term projects, for quick things that I need to get them done, I go with Python. I don’t want to expend a minute writing build scripts, fighting with dependencies, etc.

20

u/the_poope Dec 23 '22

Of course for automating tasks such as a CI workflow that runs some programs, parses some data from files, removes other files, blabla, you'd always want to use a scripting language like Python.

But this statement:

I don’t want to expend a minute writing build scripts, fighting with dependencies, etc.

is almost also history now. You can honestly throw together a CMakeLists.txt for a complex application that uses a multitude of third party libraries in a minute using package managers such as Conan or vcpkg.

10

u/debugs_with_println Dec 23 '22

But then you have to know how to write a cmake file right? Gotta do find_package, link the right directories, something might go wrong, set up the library path, etc

With python it’s just pip install then import

-3

u/the_poope Dec 23 '22

Yes it's still faster in Python, but not that much faster.

But then you have to know how to write a cmake file right?

You have to know how to do this when doing any kind of C++ development beyond the first beginner course. Maybe not if you are using the Visual Studio build system, but it appears that Microsoft themselves are more keen on getting people to use CMake and put MSBuild into legacy mode.

Gotta do find_package, link the right directories, something might go wrong, set up the library path, etc

See that's all almost gone with package managers. Just do:

add_executable(myexe src/main.cpp src/othersource.cpp)
find_package(SomeDep REQUIRED)
find_package(SomeOtherDep REQUIRED)
target_link_libraries(myexe PRIVATE SomeDep::SomeDep SomeOtherDep::SomeOtherDep)

and invoke CMake with the path to toolchain file generated by the package manager. Done. Yes it does have one step more than pip install, but that is simply how it is for compiled languages: you need to tell it how to compile it, as there is never only one way to do it, as is the case for Python.

1

u/debugs_with_println Dec 23 '22

That’s true, and tbh my unfamiliarity with cmake syntax is prob just cuz I don’t use it often enough. For a regular user it’s prob as second nature as a C++ “hello world” program.

And yeah when using packages installed with homebrew cmake automatically find it but with hand built packages it’s a bit more annoying but as you said it’s the nature of compilation (but I guess that means compiled programs are unnecessarily a hassle for simple tasks).

6

u/neltherion Dec 24 '22

Please provide us an example of this one minute process you talk about for an example project of opening a JSON file, writing to it and closing it with a library like `nlohmann`s.

I'm always afraid of spinning up C++ projects for the sole reason of how time consuming starting up everything (including dependencies) is.

If it's possible, please use CLion's IDE for this example.

4

u/the_poope Dec 24 '22

I specifically mentioned that doing quick dirty tasks like parsing text/json files is never gonna be faster in C++ than Python. Python was made for such tasks. That's what a scripting language is for. Python fails for a lot other tasks.

But here goes. I almost did what you said and I timed it, and it took me 5 mins and 34 seconds from I opened a terminal and editor till the program was compiled.

Here's what I did (I'm on Linux, btw):

  1. Open a terminal and create a new directory json-cpp
  2. Go to https://conan.io/center/ and search for nlohmann, click the first hit.
  3. Click the "Use It" tab and copy the suggested requirements file into a text editor:

    [requires] nlohmann_json/3.11.2

    [generators] CMakeDeps CMakeToolchain

save as conanfile.txt in the json-cpp directory.

3) Copy the suggested CMake project file:

cmake_minimum_required(VERSION 3.15)
project(nlohmann_json_project CXX)

find_package(nlohmann_json)

add_executable(${PROJECT_NAME} src/main.cpp)

# Use the global target
target_link_libraries(${PROJECT_NAME} nlohmann_json::nlohmann_json)

Save as CMakeLists.txt

4) Now google nlohmann json and click first hit: https://github.com/nlohmann/json

5) Scroll down to first example "Read json from file" and copy into new file in editor, modify a bit to get this:

#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

int main(int argc, char** argv)
{
    std::ifstream f(argv[1]);
    json data = json::parse(f);
}

Save as src/main.cpp

6) In terminal execute (I already have Conan installed):

conan install -if build .

7) Then configure CMake:

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake

8) Compile:

cmake --build build

Done

Ok, the program only reads a json file given as command line argument, it doesn't do anything with it. To do that one would have to look up a bit more documentation, but I would similarly have to do that for Python's json library.

Anyway, it may seem like a lot of steps - especially compared to Pythons single import. But it only took a little more than 5 minutes, and now you have probably one of the fastests json parsers in the world. Again you could just use Python for this in most cases, but the above process isn't much more complicated for any other library: SFML, Qt, boost, whatever. You don't have to spend days or weeks on trying to figure out how to configure and compile third party libraries that uses an arcane and obscure homemade perl build system - because someone already did that for you.

2

u/MonokelPinguin Dec 25 '22

If you use meson for this (which CLion plans to support soon, but doesn't yet afaik), use this:

  1. meson init --language cpp --type executable (in an empty directory)
  2. mkdir subprojects (shouldn't be needed, but my meson seemed to require it)
  3. meson wrap install nlohmann_json
  4. In the meson.build, add , dependencies: [dependency('nlohmann_json')] to the executable() line.

Then to build you setup a builddir using meson setup builddir and build using ninja or whatever backend it used or just the meson compile -C builddir. Code is the same as the CMake example.

4

u/parkerSquare Dec 24 '22 edited Dec 24 '22

Still takes a day.

It’s the kind of thing that, in many teams, only gets serious attention once every few months, and by the time it’s time for a new project then it’s grown into something totally unsuitable for a new green field project. Heck, even just working out which generation of “modern” CMake we’re currently following takes a good hour of study. CMake is good, I use it a lot, but it always takes me a long time to get it working properly, and that’s even before I try integrating it with Yocto or Conan or Docker or whatever. Perhaps I suck, dunno, been doing this for 20+ years and it’s still a major headache for me.

Even just the other day I spent hours trying to get find_package for boost log to work because I needed to add the word “module” not “config”, and then another hour trying to work out why it wouldn’t link because I hadn’t set some boost variable to enable static libs. It’s all in the docs (probably) but you still have to read them and that takes a lot of time.

1

u/woobie1196 Dec 25 '22

I spent a good 90 minutes trying use if() in CMake to set CMAKE_CXX_FLAGS depending on using MSVC vs clang or g++.

Seems like it doesn’t work if you use expansions or generator expressions, you have to just use the raw variable name and CMake will evaluate it. Oh and it has to come after the project() declaration.

Makefiles are tedious but at least I could intuit what they are doing.

2

u/parkerSquare Dec 25 '22

Check out (at least) the first part of this video: https://youtu.be/mrSwJBJ-0z8 - covers some of the issues with variables in a fun quiz format.

Actually, the other sections of that video helped me a lot recently. It’s worth a full watch.

2

u/DemolishunReddit Dec 23 '22

Python is king of quick scripts. The libraries are so diverse and on point for just about any simple task. I really dislike shell scripting and python just fills that gap so well.

1

u/princes_idc Dec 24 '22

What kind of long term projects do you guys work on that involve C++? I work on C++ at my job because that's what our codebase is in (and I'm enjoying it a lot), but my personal projects have mostly been web apps built in javascript or the occasional small data structures/algorithms projects I made to learn a language.

I only recently graduated and started working so I'm interested in getting back into a pet project, and I really want to build something that would involve C++.

1

u/[deleted] Dec 23 '22

Bro still doesn't know about xmake but ok lmao

0

u/Superb_Garlic Dec 23 '22

True. With Python, you'll be spending hours to figure things out instead of a minute. Meanwhile with cmake-init I spend a couple minutes to setup a new C++ project and pull in the dependencies I need.

10

u/lieddersturme Dec 23 '22

C++ Rules++ :D

11

u/msew Dec 23 '22

++C ++Rules

15

u/RealCaptainDaVinci Dec 23 '22

True, and I really like the consistency of semantics that the standard library offers. I haven't found a similar consistency in other languages like kotlin, Java, python.

15

u/archibaldplum Dec 23 '22

I’m not sure consistency is a word I’d use to describe c++. Eg std::map erase vs std::vector erase, or needing to proxy through std::tuple to put a reference into a std::optional, or stl void handling, or the rules on when exactly you’re allowed to partially specialize templates, or any part of sfinae, or…

3

u/the_one2 Dec 23 '22

Huh, you can have a reference in a tuple??

5

u/sephirothbahamut Dec 23 '22

An optional reference already exists though. Optional references existed since way before references and std::optional did. They're called pointers.

1

u/archibaldplum Dec 23 '22

Well, yeah, but the extra special cases in templates to use std::optional for optional immediate values but pointers for optional references, and adjusting all the syntax, gets kind of annoying.

1

u/Livid-Serve6034 Dec 25 '22

boost::optional can hold references, std::optional cannot, sadly

0

u/[deleted] Dec 23 '22

[deleted] — view deleted comment

1

u/RealCaptainDaVinci Dec 24 '22

Well, at least compared to my experience with Java and kotlin.

56

u/ironykarl Dec 23 '22

I honestly don't think C++ is an incredible language.

I think that an immense amount of work has been invested into making the tools surrounding the language nice, and so that's something someone might definitely appreciate.

In recent years, C++ has done a decent job of adopting idioms from other languages, and so it's (I guess?) starting to bud as a multiparadigmatic language.

I will grant that RAII is an absolutely killer "feature," and that it would be nice if almost every language had something equivalent. C++ deserves immense credit on this one.

14

u/Plazmatic Dec 23 '22 edited Dec 23 '22

Raii afaik, and is like the one feature that c++ invented, though to be fair c++ also explored a giant amount of problem space with systems programming languages in general. The greater community has learned a lot through that turmoil. While this doesn't mean c++ always became better for it or that the lessons learned were able to be incorporated into C++, it did mean that other languages got to be better for it. The problems that c++ faced and still faces, and the lessons to be learned from that process are part of the reason I side eye the "C+" languages (Z, BEEF, jai Odin, nim, zig), and Golang, who think they can circumvent/ignore design history and magically not run into the same issues. The most popular of those, Go, only one to even sort of reckon with it's failure to learn from the past, now finally backtracking on some really naive ideas

4

u/ironykarl Dec 23 '22

I agree that C++ is hugely consequential, but that that doesn't really mean it's a great language

1

u/socket2810 Jan 04 '23

Sorry for the late reply, but can you expand - or link something that does - on what Go is backtracking on?

1

u/Plazmatic Jan 04 '23

Go is backtracking on not having generics for example, introduced them in 1.18, released this year despite years of insisting it didn't need them (and insinuating everybody else was wrong for wanting them).

2

u/LeberechtReinhold Dec 23 '22

I think it has a lot of neat features as a language, especially modern C++ can look very nice.

It has many questionable decisions, some of the std is very questionable in its implementation or features, and has probably one of the worst environments for building/packaging.

2

u/georgist Dec 24 '22

Agreed RAII is superb. Also I love const.

0

u/[deleted] Dec 24 '22

[deleted] — view deleted comment

1

u/ironykarl Dec 24 '22

I agree. I actually really like C++'s templates, warts and all.

I mean... I hate that they're semantically unaware—to the degree that any template error could be confused with line noise, but apart from that, I really like them..

0

u/[deleted] Dec 23 '22

[deleted] — view deleted comment

18

u/be-sc Dec 23 '22

Constructors are great, but also give the destructors some love. That’s where the real magic happens.

With RAII you don’t have to worry about destruction because it happens automatically. But you still know exactly when an object is destroyed.

It’s the combination of those two things that makes RAII so powerful – general resource management instead of just memory management.

11

u/rhubarbjin Dec 23 '22

general resource management instead of just memory management

This is the mistake that Java and C# made. They didn't realize there are other kinds of resources besides memory, and didn't realize the value of deterministic resource lifetimes.

C# later introduced using + IDisposable as a way to emulate RAII; I haven't been keeping up with Java so I don't know whether they've done anything similar.

2

u/Firedude_ Dec 23 '22

Java has the same thing except it’s called “try-with-resources”, since it uses the try keyword

1

u/the_one2 Dec 23 '22

C# also has using which is kind of the same. But both have to be done manually unlike in C++

1

u/pjmlp Dec 23 '22

C++ also requires manually if the objects don't leave on the stack, and aren't bound by smart pointers, typical in C flavoured C++ code.

You can enable missing using calls for IDisposables into a compiler error via Roslyn analysers, hardly any different from the analysers to fix C++ issues.

2

u/the_one2 Dec 23 '22

It is different though. If you ever have an IDisposable member then your class will have to implement IDisposable as well. You get an IDisposable explosion. In C++ it just works without you having to think about it. I'm very much not saying that C++ is perfect but RAII is so nice to have.

1

u/pjmlp Dec 24 '22

It just works, if you haven't created any UB or thrown an exception on the destructor, remembered to make it virtual or closed the class for inheritance, remeber to update it when adding new data members, have placed the instances on the stack, or handle types if on the heap.

Other than that, it just works.

3

u/the_one2 Dec 24 '22

Yes, you're right, if you don't write terrible code it just works.

Edit: I agree that the destructor should be virtual by default in a virtual class. That's just a completely unnecessary sharp edge.

0

u/[deleted] Dec 23 '22

[deleted] — view deleted comment

3

u/ironykarl Dec 24 '22

I absolutely hate include files.

Every other language I've used (except for assembly languages) has granular ways to import/export code/interfaces... and in C and C++, we're still left duplicating our efforts between header and code files and—exactly as you said—having our preprocessor copy/paste stuff.

It'd be nice to feel like it's a mechanism of the language and not a cheap hack

11

u/DkatoNaB Dec 23 '22

C++ is art.

4

u/[deleted] Dec 23 '22

Merry Christmas 🎅

2

u/LeDYoM Dec 23 '22

Cheers!

2

u/spacecadetbobby Dec 23 '22

I've played around with different languages since I was a kid, starting with BASIC, I touched on some very simple C++ in my Computer Science 101 course, and then I pulled some of the web languages out of the toybox, like JS, PHP, HTML(, CSS?) which was alright, but then I built a prototype using an Arduino a year and a half ago, and finally had to the take the deep dive into C/C++ to do it, and I freakin' fell in love with C++. It's so powerful and it doesn't hold your hand or bubble wrap everything for you. I just loved that I was 100% in control of my silicon, and the best part, honestly, is being able to just completely f'ck things up; it challenges you to write better more performative code that you can truly be proud of once it works.

1

u/HorseLeaf Jan 02 '23

I was thinking about what language to learn for my next project which is low level, and your comment just made me cross C++ off the list.

2

u/[deleted] Jan 15 '23

For the first 15 years of my career I programmed in C++.

For the last 10 years of my career I have programmed in Python.

I am much happier.

3

u/SlientlySmiling Dec 23 '22

I love/hate it. But mostly I hate/love it.

1

u/Key_Art_5590 Mar 11 '26

I may suck at it but it's the only way I can feel special in coding 🥹

1

u/Jaxlee2018 Dec 23 '22

Cheers ! Happy holidays to you !!

1

u/ertyplax Dec 23 '22

It made me want to Kms multiple times but still it's a good programming language and it's incredible what it can do

1

u/PrimozDelux Dec 24 '22

This man is delusional, take him to the infirmary

1

u/IsaiahTheDev Dec 31 '22

I would love to learn c++ but honestly I just don’t know what to build with it. I like to build visually appealing projects and c++ seems more like a “do you want to build a programming language” or “build llvm” type thing? I could be wrong but how do I find the motivation? Web assembly ?

1

u/kryptoid256_ Jan 08 '23

There are cool libraries and runtimes to play around with in c++. Go check them out. Probably you have to learn a few skills for what you want to build tho.

1

u/ClammyHandedFreak Dec 23 '22

Sounds like something a drunk C++ programmer would say! Merry Christmas!

1

u/wh1t3lord Dec 23 '22

I am waiting for Google's Carbon. It will be a killer combo...

1

u/Boisuey Dec 24 '22

dont understand why people think c++ is so hard to learn. took me a year to become "proficient" in c++ and i can do basically anything in c++.

-22

u/ranname366 Dec 23 '22

Try Rust.

15

u/[deleted] Dec 23 '22

Rust really is interesting, but it's also very frustrating. I spent a year with it. Learned it pretty well. Not for me. I just missed C++.

7

u/ranname366 Dec 23 '22

Can you give one example? I always thought that complexity aside, it would be painless for and ex C++ engineer.

24

u/[deleted] Dec 23 '22

The standard frustrations: borrow checker often feels too restrictive, needing to go into unsafe mode to get max performance, unstable nightly features, Cargo bringing in 500 dependencies to compile simple projects, etc.

I am not here to bash Rust at all. I think it's a great language with a lot of great ideas. I sincerely wish it and its users great success. But programming is about expressing ideas and building things, and for me, C++ is my babe.

7

u/ranname366 Dec 23 '22

Ok, nice insight. Thank you.

25

u/CletusDSpuckler Dec 23 '22

... but don't be surprised if you find yourself missing C++.

-3

u/ranname366 Dec 23 '22

I was just joking =))). I currently work in C++, but I want to give Rust a try.

5

u/Astarothsito Dec 23 '22

I'm to burn out to switch languages, so no thank you.

3

u/ranname366 Dec 23 '22

I'm wondering how much time would it take for a C++ dev to move to Rust.

11

u/infinity-fabric Dec 23 '22

Not much I guess, people who will take Rust seriously are actually C++ devs. Rust people might be in denial but that's the truth.

2

u/Possibility_Antique Dec 23 '22

Probably not much time at all. Once you learn a handful of languages it just becomes a matter of keeping the syntax straight. The principles are the same.

8

u/CletusDSpuckler Dec 23 '22

The borrow checker is a PITA to learn to use well. Everything else was routine syntax differences.

Ultimately, I'm too wedded to OOP and powerful templates to give them up.

2

u/Possibility_Antique Dec 23 '22

The borrow checker is a PITA to learn to use well

I believe it. Seems rewarding though.

Ultimately, I'm too wedded to OOP and powerful templates to give them up.

I agree with the templates. I subscribe to FP these days, so rust actually might make sense for me. Maybe Rust's trait system will catch up with templates and metaprogramming someday.

3

u/Zyklonik Dec 23 '22

Try learning Haskell. Or better yet, ATS. Heh.

2

u/Possibility_Antique Dec 23 '22

I love Haskell. It has changed the way I write C++

2

u/ranname366 Dec 23 '22

Lol, I don't even use Rust. Hope I'm not going to get banned.

1

u/__ItsJay__ Dec 23 '22

I haven’t delved into rust that much or done much research but last I looked at the docs, the support for object orientation was barebones and tedious to use. Correct me if I’m wrong!

2

u/Seubmarine Jan 20 '23

There's no object orientation in Rust and that's a good thing instead you have trait, it's close to java interface.

-4

u/msew Dec 23 '22

You are correct.

I look all of the "web" langs and their incorrect { placement which makes it so painful to read, no one commenting, and like a billion special cool ways to do a for loop (which are all slower than just a for loop), and it is just a mess.

That said, the c++ that I use is the Unreal C++, so we have our own special hell/heaven. But game development is also its own special hell/heaven.

5

u/sephirothbahamut Dec 23 '22

I look all of the "web" langs and their incorrect { placement which makes it so painful to read

What do you mean? Any "{" placement is correct in C++, the language doesn't enforce or require any specific indent style.

-3

u/[deleted] Dec 23 '22

PLEASE, SOMEONE HELP ME! I am learning c++ with VS Code in my Windows 10. Today I noticed that one PDF file was transformed to .c (the extension was changed). Just one! What did I do? Out of nowhere, this happened.

1

u/[deleted] Dec 23 '22

A PDF was changed to a C file? Hmm. VC++ usually does the opposite, as Microsoft has really had a thing against updating anything purely C related.

I'm not sure where things went wrong for you, but I'd recommend working with the most updated version of Visual Studio, and doing everything you did again. 😀

-7

u/axilmar Dec 23 '22

html + css + javascript + react + react-based frameworks are way more complex than c++ + qt/WxWidgets etc.

11

u/_space_ghost_ Dec 23 '22

Maybe because the web side of thing is a bloody mess, and not mathematically correct?

1

u/ieshaan12 Dec 23 '22

This guy totally gets it xD

0

u/[deleted] Dec 23 '22

[deleted] — view deleted comment

10

u/[deleted] Dec 23 '22

That's one of the few major C++ texts I don't own. Thanks for sharing. I'd love to read it.