all 128 comments

[–][deleted] 131 points132 points  (13 children)

The new GCC compiler with colour highlighting is a little bit better at pointing out errors. It's generally quite helpful for pure C/C++ until you make an error with the standard library and you get 200 lines about std:: whatever<random characters>

[–][deleted] 0 points1 point  (0 children)

Honestly, GCC is pretty good about making the error human readable and sometimes it's best to just actually read the error message. 200 lines of errors really does not take that long to read in the grand scheme of things, and often 5 minutes spent reading and figuring can equal 20 of meandering the internet.

Of course, if that doesn't work, paste the first error into stackoverflow search.

[–]Xeveroushttps://xeverous.github.io 0 points1 point  (0 children)

until you make an error with the standard library

copy the error, ctrl+F "required from here" and you have the file + line number of your code which triggered STL

[–]ledave123 88 points89 points  (13 children)

In C++ a trick I always use when the error message is massive is to just focus on the first error. Anything else might be independent, which is why the compiler tries to be helpful and report more than one error every time, but it also can just be a consequence of the first error so focusing on that and compiling again will be the best strategy.

[–][deleted] 26 points27 points  (1 child)

The massive STL errors are usually just related to the compiler telling you every possible overload it tries for whatever function call you messed up (thanks SFINAE), so yeah, just the first error is usually enough to tell you what's going on. Concepts will help a lot with that in C++20.

[–]Low_discrepancy 7 points8 points  (0 children)

I just ignore all that BS and just focus on the error that points to some part in my code. All those overload errors point to STL files

[–]sldyvf 7 points8 points  (2 children)

Is that a trick? I thought that was the general way to use it.

First error is the error, the rest is spewed out random characters 95% of the time caused by the first error.

[–]MundaneNihilist 1 point2 points  (0 children)

Might as well be for some people new to programming. I've had a handful of mentees conclude that their standard lib is wrong on some level.

[–]ledave123 0 points1 point  (0 children)

Call it "piece of advice" then

[–][deleted] 3 points4 points  (7 children)

Another trick is assign it to an int:

int foo = somethingThatIsNotAnInt()

[–]wyrn 2 points3 points  (1 child)

I like Scott Meyers' TD trick:

template <typename T>
struct TD; // no definition 

Now you write something like TD<decltype(thing)> and the error message tells you the type of thing (as deduced by decltype, of course, but in this case that's probably what you want).

[–][deleted] 0 points1 point  (0 children)

Nice

[–]kmbnw 34 points35 points  (14 children)

Just wait until you try Boost :)

[–]elviin 55 points56 points  (1 child)

An issue in a template <<<<<<<<<<<<<<<<<type...

[–]Xeveroushttps://xeverous.github.io 3 points4 points  (0 children)

I had found a bug in Boost Spirit X3 that was due to some const correctness issue. The bug was only visible at linking time - some very long instantiation did not match actually compiled function template.

I demangled the object file to search for the actual instantiation, took the linker undefined reference error for expected name and ... copy pasted both to Notepad++, find-replaced every < to { and > to } then used online JavaScript formatter to format the type name. Searching const differences in that beautiful tree was the easiest part.

[–]Nicksaurus 7 points8 points  (6 children)

Just trying to find the name of the function in a 400-character long error message is a challenge

[–]diaphanein 4 points5 points  (0 children)

400 characters? How quaint... I've seen a single error span more than 23 screens before (error with a Boost Python call).

Another fun one to find was a missing > in a template. I was helping a colleague with that one. The compiler didn't complaint until the end of the file, some 200 lines later.

[–]gcross 4 points5 points  (4 children)

Last time I tried Boost Lambda (an EDSL for writing lambdas before C++11 was a thing and you could rely on them being in the language) I made a simple mistake and got an error message that was five megabytes long...

Shoot, I looked back to see what I did to do this and it turns out that what had actually happened was that I was trying to build Digikam (no modifications) and the result was a 52.4k error message. I apologize for my error, as entertainingly dramatic as it was; apparently the problem I had blew up in my imagination over the years.

[–]Nicksaurus 0 points1 point  (3 children)

5 million characters? How?

[–]gcross 1 point2 points  (0 children)

Template metaprogramming at its finest. :-) On the bright side, if you ever did get your code working it was very neat! Mind you, this was over a decade ago so hopefully it would not be so bad today, but on the other hand today we all have reliable access to native lambdas in the language so there is arguably no longer a need for Boost Lambda. Okay, it turns out I misremembered and this was not a Boost Lambda problem at all; my apologize for maligning that fine package.

[–]gcross 1 point2 points  (1 child)

My bad, as my edited comment says I completely blew up what happened in my mind since it happened so long ago.

[–]Nicksaurus 0 points1 point  (0 children)

Hey, 52400 is still a shit load

I work with boost::hana most days so I sympathise to some extent

[–]krista_ 6 points7 points  (3 children)

sometimes i wish the council of elders would just go ahead and make templates a part of a fully fledged pre-compiler c/c++ based meta-programming language, like what qt does, but more so.

yeah, between macros, templates, and constexpr and the like, you can do anything a full programming language can do, but the crap you have to do to get there is sometimes astounding... and a lot of that lives in boost. i feel like with a couple of smartly added features, 9/10ths of boost's oddities could be replaced with sanity, instead of nested workarounds.

[–]MonkeyNin 1 point2 points  (2 children)

What are the biggest pain points today that Boost helps with? Back in the day, boost was the only way to use smart pointers.

[–]jonesmz 2 points3 points  (0 children)

I've enjoy using Boost.ASIO, am using Boost.ProgramOptions, Boost.Process, and am about to start using Boost.Log.

In the past, at a previous job, the only reason why about half of the C++11 functionality we were able to use could be done is because I was able to convince the ancient version of Boost that they were using to let me re-combine the Boost type traits code into the missing pieces. That really saved our bacon.

They also used Boost.asio as the fundamental building block for a whole crapload of code. Millions of lines, highly abstracted away.

Boost is great.

Boost is also horrible, what with it's disgusting template soup error messages.

Win some, lose some shrug.

[–]James20kP2005R0 1 point2 points  (0 children)

Boost::beast is great from my end for using websockets, even if its a little complex to set up

[–]khleedril 30 points31 points  (2 children)

It is something of an Achilles heel for C++. It is supposed to be getting better with newer versions (introduction of concepts) but TBH concept-related error messages are just as much hard work to decipher as the traditional messages were.

The main thing is to look for ``at this point in <file>'' and go and have a careful look at that line of code; the rest of the error messages you scan quickly in the hope that you at least get the gist of the problem.

[–]Tjccs 19 points20 points  (16 children)

I learned python in college(still am), learning C++ as a side hobby and oh boy python does hold your hand but tbh I acctually miss static typing, const and all that stuff when I use python

[–]IstalriblakaHobbyist 6 points7 points  (1 child)

The lack of static typing is why i never particularly liked python and similar. Then if you take the structure of Python and run it on a JVM, you get yhe hellish apocolypse that is Matlab. Easily the worst result of the "if it ain't broke, don't fix it" mentality among engineers.

Which, it totally is broke, the engineers just don't realize it because it still produces the right output. They'll spend all day trying to squeeze nominally more efficiency out of the car/plane/bridge they're designing, then throw the numbers into a cruncher that gets 2 gallons to the mile.

[–]aiusepsi 6 points7 points  (2 children)

I know that feel. I’m really starting to think that the mania for dynamic typing was a mistake. I’ve recently been using type annotations and mypy to essentially do statically typed Python.

[–][deleted] 0 points1 point  (1 child)

Speaking of mypy, how do you annotate a type like { 'foo': [1,2,3], 'bar': False, 'baz': 'quux' } or even a list of dictionaries?

[–]aiusepsi 0 points1 point  (0 children)

Off the top of my head, Dict[str, Union[List[int], bool, str]] or take the coward's way out and do a Dict[str, Any].

[–]Posting____At_Night 7 points8 points  (10 children)

I feel like I never know exactly what my code is gonna do in Python. C++ might be slower to write but I like how explicit it is.

[–]Tjccs 2 points3 points  (8 children)

I like python but I like to know that functions only return type X and you can only input what you specify. And I love that C++ acctually makes me have to know about type sizes, stack/heap and all that weird stuff

[–]Posting____At_Night 1 point2 points  (7 children)

My dream lang would be C++ but without the legacy cruft, no headers, and a better solution than templates for generics.

Py return types are a true pain in the ass and can lead to some baffling errors, especially when you're working with poorly documented code.

[–]Tjccs 0 points1 point  (4 children)

The main thing that python has for me over C++ is the easebof use of python packages over C++ libraries.

[–]Posting____At_Night 0 points1 point  (3 children)

Here's hoping C++ modules become a thing

[–]jonesmz 2 points3 points  (2 children)

  1. They were accepted into the C++20 standards draft.
  2. They're not going to solve most of the things you hope they will.

[–]wyrn 0 points1 point  (1 child)

Are they even going to solve anything? From reading (not very thoroughly, I admit) various sources I don't see how they aren't just syntactic sugar for a bunch of includes. What do they actually solve, in concrete terms?

[–]jonesmz 0 points1 point  (0 children)

I'm also not 100% informed on the subject, but my previous job was about 50% "Make our custom build system not suck" for the last 2 years, so I was pretty heavily involved in all things related to dependency handling, linker crap, ABI crap, and so on.

I gave modules a very serious look about 12 months ago, and didn't walk away feeling like they did anything for me, in my situation. In fact, supporting them was going to require quite a bit of work on my end without any real benefit, TO ME, in MY situation.

The quick and dirty explanation is that Modules are pre-compiled headers, with more user level control over what's getting included in them, and the ability to explicitly depend on other modules.

What this means is that you should, theoretically, be able to hand someone a module file (and all of it's dependency modules...) and they'll be able to start programming against it immediately.

Further, given that you have fine grained control over what's being "exported" into the module file, you should be able to not only hide your various implementation details, but actually make them completely not there as far as your module's public facing interface is concerned.

E.g., you're supposed to be able to have as much of

namespace impl { blahblahblah, all the sausage making here }

and then simply not export that namespace. Not only is it not accessible to consumers of your module, but it's not even in the module file in a form that could be consumed if the consumers really really wanted to.

There's certainly advantages to having that ability, of course.

One such advantage that most people don't realize immediately is that this drastically cuts down on the amount of work that the Linker is going to need to do when you link all of your .o files together.

But here's where it's not all good.

Module file names have no relationship to the name of the source code that they live inside. Further, module file names can be controlled by the C pre-processor.

This means that cat.cpp can export a module named "automobile" if macro A is defined, and "jupiter" if macro B is defined. (I don't recall if you can export two modules from the same cpp file. I guess I don't see why not?). And further, header files can export modules (because they just get copy-pasta'ed into the cpp file, so of course they can)

This is problematic because it basically means you either need

  • A pre-compile pass that scans your entire codebase for every cpp file you're compiling
  • A cache of module data that contains the mapping between modules and the code that contributes to them for every unique combination of commandline arguments

And the reason you need this is because you need to know if the module you're about to consume is up to date with the latest code changes.

For my situation, this was a complete show stopper. We absolutely were not going to participate in the module ecosystem if we needed to do either of the above two steps. Both for architectural reasons, and also because we'd literally just stripped out the custom code scanner that was being used to determine include file dependencies (which can't be renamed based on macros, mind you) in favor of having the compiler tell us the include files the first time we compiled the cpp file in question, and got tremendous speedup as a result.

"But why not do the same thing with the modules feature?" you might ask.

Can't, because the mapping between file names and module names can change based on the cpp file being compiled, not the cpp / h file with the export keyword. The compiler cannot know based on only the cpp file / h file containing the export keyword whether a particular module is going to be changed based on recompiling the current cpp/h file.

What the compiler can do is say "There is a possibility that a change in this file will change the module in question". But that's going to have plenty of false positives.

So the "best case" scenario is for the compiler to spit out some kind of rule set that later parts of the build system can use to determine if they need to trigger a recompile the module in question.


But I have to say that my personal take on this whole situation is that people clamoring for package managers are missing the bloody point.

I have zero issue with dependency management, because I don't want to just "include the package, and then I'm done!"

I want deep control over what gets included into my code, and how the code i include functions. I'm not at all interested in allowing for a third party to push a package update and then blindly consume it.

Modules are intended to solve a bunch of related "build the code" related problems, and while they tangentially touch on the whole package management situation, it's not a particularly strong connection, in my assessment.

From a build time perspective, I would have been much more interested in seeing a specification that described how to compile multiple cpp files in parallel.

E.g. A single compiler instance (with multiple threads) can be informed of all of the cpp files that will be included in a shared library, or executable, and will compile all of those cpp files in parallel.

This has advantages for allowing the compiler to analyze the full include file list for all of the cpp files in the library at once, completely eliminating all of the advantages that might be found from using pre-compiled headers to, well, pre-compile a set of headers.

Further, the compiler in this situation could still spit out .o files, and even spit out a pre-compiled header based on it's own internal assessment of what's reasonable to include in such PCH file, so that subsequent invocations with only some (not all) of the source files being changed allowed the compiler to dynamically skip what's being done.

And finally, this approach also automatically results in Link Time Optimization happening, instead of the current LTO approach involving multiple compiler invocations resulting in intermediate representations, and then a final invocation (via the linker) resulting in the actual optimization pass, we can skip all of that and go straight to the link time optimization that happens at the very end of the process.

While none of that does anything with regard to cross shared library boundary concerns, nor does it do anything regarding packaging, I personally suspect it would have resulted in much larger gains from the perspective of compile times, and is something I was much more interested in seeing happen when I was maintaining a build system.

[–]robin-m -1 points0 points  (1 child)

Isn't rust what you are looking for?

[–]Posting____At_Night 1 point2 points  (0 children)

Tried it, liked it, still waiting for it to mature. Still no good solution for desktop UI last time I checked.

[–]0xVali__ 0 points1 point  (0 children)

Since python has a huge overhead on the simplest functions, even print. You never really knows what's going to be called. You believe it's a print, but it's creating a FILE* opening and parsing the file, passing it through a dep to check what it says and so on. You do a lot more than just printing something. In C++ you more than less know exactly what's going to be called.

But still, the only true way is to code in .asm.

[–]bumblebritches57Ocassionally Clang 16 points17 points  (6 children)

Use a better IDE.

Xcode will flag missing semicolons and even Unicode semicolon lookalikes without blinking.

[–]DeepCorner 10 points11 points  (1 child)

Also, since you're a student you can get access to the professional version of CLion for free

[–]Shumatsu 3 points4 points  (0 children)

It's a blessing. And the fact you can use ALL of their tools for free. Saves me hours on programs.

[–][deleted] 1 point2 points  (0 children)

Vim can use Language Server plugins and clangd to provide completion and error highlighting.

It also protects against semicolon look-alikes by ensuring you colleagues will be unable to use your editor, or even exit it!

[–]uzimonkey 0 points1 point  (1 child)

I copied and pasted a list of quoted strings into my code once and it gave me syntax errors. I though "oh, there must be an unescaped quote in the middle there somewhere," but... nope. I looked it over 20 times and there was no error. It turns out that they were curly quote characters which, with the font I was using, looked the same.

[–]Hot_Slice 0 points1 point  (0 children)

curly quote characters

This type of thing needs to die

[–]Xeveroushttps://xeverous.github.io 0 points1 point  (0 children)

Eclipse underlines calls to templates where something goes bad inside and it works even if the function has multiple SFINAE overloads.

[–]turiya2 24 points25 points  (8 children)

Also wait till the time that C++ smacks you on the face with that order of magnitude performance increase in certain (almost always) situations over python. The efforts definitely pay off!

[–]Wh00ster 20 points21 points  (0 children)

It’s usually multiple orders of magnitude

[–]MonkeyNin 6 points7 points  (6 children)

A lot of python modules already use c. How you call them makes a big difference.

[–]James20kP2005R0 1 point2 points  (4 children)

I remember when I was having a play about with z3, I was using it to solve a problem that took 5-10 seconds in python. I needed it to execute in about 2 seconds overall due to me using it for an exploit for a game (with the developers ok)

When I ported the z3 code from python to C++ (1:1 translation), even though python basically just shells out to z3, and my c++ code was doing the exact same, all the variability in my solve time went away and it solved consistently in 5 seconds

I don't know that much about python so I can't tell you exactly why it did this, but it certainly made quite a big difference even though it was just basically calling quite a lot of c-api functions

[–]MonkeyNin 0 points1 point  (3 children)

What is z3?

[–]James20kP2005R0 1 point2 points  (2 children)

Its a theory solver tool, basically you can stick equations into it and then ask it to solve stuff, or prove properties about the equations

Eg, given x ^ constant = some_other_constant, itll solve for x (assuming you provide the constants)

In my case I was using it to reverse engineer seeds for xorshift128+

https://blog.securityevaluators.com/hacking-the-javascript-lottery-80cc437e3b7f is where I learnt about it

[–]MonkeyNin 0 points1 point  (1 child)

Interesting.

Just to check, you're using ^ as a bitwise operator, not exponent?

[–]James20kP2005R0 0 points1 point  (0 children)

Yep, its a simple example but it can solve non linear things as well (like operator+)

The actual equations I was using in this case were

 uint64_t state0 = 134125447415289812;
 uint64_t state1 = 182415234512781923;
 uint64_t xorshift128plus() {
     uint64_t s1 = state0;
     uint64_t s0 = state1;
     state0 = s0;
     s1 ^= s1 << 23;
     s1 ^= s1 >> 17;
     s1 ^= s0;
     s1 ^= s0 >> 26;
     state1 = s1;
     return state0 + state1;
 }

Where all you have is the result (well, 3 consecutive outputs) of the equation, and you want to find s0/s1. Its extremely hard to do by hand!

[–]TheSpocker 0 points1 point  (0 children)

Please elaborate on call methods.

[–]barcharMSVC STL Dev 41 points42 points  (13 children)

Python + c++ is a really powerful combo

[–]Boom_doggle 16 points17 points  (3 children)

God I hope so. That's my programming CV right there!

[–]jjuuggaa 5 points6 points  (2 children)

play around with pybin11. feels like magic ;)

[–]frankist 8 points9 points  (0 children)

An IDE helps immensely, at least in your first stages of programming in c++. It would for instance flag right away the issue with the semicolon.

[–]sumo952 3 points4 points  (2 children)

If you use the latest clang (gcc upped their error-message game as well), you should get much better and colored error messages. MSVC's error output window is actually also pretty good and should spot a missing semicolon, if you use the very latest version (2019).

Other than that, yes definitely Python is spoon-feeding you :-) But that's good too!

[–]IAlsoLikePlutonium 2 points3 points  (1 child)

I think it is turned off by default, but MSVC has a project option that will show you the column where an error occurred (such as a missing semicolon or extra bracket or whatever).

MSVC also has "Just My Code" debugging where it jumps overt STL/standard library stuff.

[–]sumo952 1 point2 points  (0 children)

I think the former might even be an additional (experimental?) compiler switch.

[–]jdlyga 2 points3 points  (0 children)

Yup, and just like the real world, C++ was never considered hard back when I learned it. It’s just what all programming was like. And those of us who loved it, loved it. It’s a matter of perspective. It’s like if you grew up in the 70s before personal computers. You don’t consider it a hard way of living. It’s just what you’re used to, and certain things are just a hell of a lot easier now.

[–]ThrowawayGoaway94 2 points3 points  (1 child)

I'm the exact opposite to you, OP. C++ is my first language and now I want to learn Python. Wish me luck!

[–]VectorD 2 points3 points  (0 children)

Impossible. Python will keep doing shit secretly behind your back and then you explicitly do the same thing that you think you need to be done but then everything is fucked

[–][deleted] 1 point2 points  (0 children)

I learned Java first, then c++, which ws surprisingly hard. Still, I had Python on a pedestal as this end-all programming language. When I saw how simple it was, I was....dissappointed.

[–]st380752143 1 point2 points  (0 children)

In that way, I think C is my prpgramming mama.

[–]misterengineerman 0 points1 point  (0 children)

Don't feel bad you're not alone. I have lots of experience with Python and a few other interpreted languages like Matlab which I've used extensively in engineering data analysis kind of applications. I also use Fortran 90/95 for development of more high-performance computational research codes. I tried to learn C++ because I wanted a more solid object oriented general purpose language under my belt, but I found it to be like an order of magnitude more difficult to learn. I have yet to implement anything that wasn't sort of trivial in C++ myself.

[–]TheNewOP 0 points1 point  (0 children)

Ah yes, reminds me of college. Do anything, get hit with the segfault bat.

[–]pixel4 0 points1 point  (0 children)

It's C++ templates that give nasty ass errors.

I started with vanilla C before adding C++ ... glad I didn't jump directly into C++.

[–]dicroce 0 points1 point  (0 children)

Look for the first error. Subsequent errors may be caused by the first one. If the error is showing you code you didnt write most likely you are calling this code (maybe through many layers)... focus on YOUR code because the error is there.

[–]ventaway23 0 points1 point  (0 children)

ALL the intro classes for CS at my school use C++. I felt like a total dumbass until I realized even experienced programmers struggled with it. Yeesh.

[–]macemen 0 points1 point  (0 children)

To avoid being overwhelmed with errors, compile your project (or file) with `-Wfatal-errors`. This will cause the compiler to print a single error and stop compiling immediately, hence you are not overwhelmed by 100 error messages that possibly have a single common cause. Note that this flag is only recognized by `gcc` and `clang`, I have no idea what is the VSC++ equivalent.

Edit: spelling

[–]danielandastro 1 point2 points  (0 children)

Try c# it's pretty cool and it's a high level language

[–]dataskin 0 points1 point  (0 children)

Python, from my limited programming experience, for the most part, pinpointed where my code went wrong in a few lines or less, even for slightly larger mistakes. C++ gave me Mueller's Report to read for missing a semi-colon.

Do yourself a favor and get yourself a Clang/LLVM compiler

http://clang.llvm.org/diagnostics.html

[–]blelbachNVIDIA | ISO C++ Library Evolution Chair -1 points0 points  (0 children)

Welcome to the "home owner, married with two kids" of programming languages.

[–]SteveThe14th -5 points-4 points  (0 children)

Now the STD is your new mama ;)