News for hackers. Stuff that matters.
Re:C++ Cheat Sheets by jokoon on Sunday March 06, 2022 @04:31PM (#30581460)
Wow, this is really good, there are still things I can learn.

C++ really has a lot of good things. It's just a shame it's so slow to compile.

I'm curious if anybody is working to make C++ faster to compile. Even if it was a subset of the language, with some features removed, it would be good enough for me.

Re:C++ Cheat Sheets by StephanTLavavej on Sunday March 06, 2022 @08:56PM (#30583347)
> I'm curious if anybody is working to make C++ faster to compile.

Yes - C++20 added support for modules to the Core Language (both "header units" and "named modules"; header units are an intermediate step between classic includes and named modules), and support for header units to the Standard Library. Compiler/library support is a work in progress (MSVC's STL, which I work on, is the furthest along - see https://github.com/microsoft/STL/issues/1694 for status), but header units are showing significant improvements in compiler throughput (build speed). This looks like `import <vector>;` in your source code (with significant build system changes to build vector as a header unit, producing vector.ifc and vector.obj).

There's a proposal under review for C++23 to add named modules for the Standard Library, see https://wg21.link/p2465r2 . If this is accepted, `import std;` (or `import std.compat;`) will be the one-line way to use the entire C++ Standard Library with (what we hope will be) even better compiler throughput.

The primary limitation of header units and especially named modules is macros: neither can be influenced by macros defined in the source file, and named modules can't emit macros at all. Thus, one will still need to `#include <cassert>` in addition to `import std;` if you want the `assert()` macro.

Re:C++ Cheat Sheets by pjmlp on Monday March 07, 2022 @02:02AM (#30584951)
Still looking forward to a solution for _ITERATOR_DEBUG_LEVEL in release builds with modules, VS DevCommunity ticket does exist.

Currently it appears anyone that cares about bounds checking and iterator validation in release builds, has to keep using global module fragments.

Re:C++ Cheat Sheets by Negitivefrags on Monday March 07, 2022 @03:30AM (#30585440)
I have the opposite problem. I can't use modules because I need _ITERATOR_DEBUG_LEVEL=0 in debug builds.

The standard library modules really need to be built as part of your projects build so that you can compile them however you want.

Re:C++ Cheat Sheets by pjmlp on Monday March 07, 2022 @04:10AM (#30585686)
Either that, or be like MFC/ATL and provide a couple of pre-compiled versions for common workflows.
Re:C++ Cheat Sheets by jokoon on Monday March 07, 2022 @07:10AM (#30586813)
Wow, thanks, can't wait for this.

How hard will it for library writers to supply their libraries as modules?

Will the only requirement be to remove macros?

Re:C++ Cheat Sheets by jcelerier on Sunday March 06, 2022 @04:34PM (#30581485)
* Compile with clang, link with mold or at least lld. mold can link gigabyte-sized binaries in, like, one second

* Use ninja instead of make

* Use PCH

* -gsplit-dwarf

Re:C++ Cheat Sheets by woodruffw on Sunday March 06, 2022 @05:31PM (#30581935)
This is all solid advice (particularly using a faster linker).

IME, the single best way to reduce your C++ compile times is to compile less code:

* Remove all unnecessary headers. Template expansion is slow, and preprocessing is even slower. Some of the standard includes (like `<regex>` and `<iostream>`) are notorious for slowing individual translation units to a crawl. `#pragma once` for your own headers also helps with cpp-time performance.

* Forward-declare as much as you can. Forward type declarations mean that the compiler doesn't need to process all of `Foo` when it sees `Foo&` or `Foo`.

Use pImpl wherever you can (and makes sense). Private implementations similarly reduce the amount of code the compiler needs to analyze.

For better or worse, the current winds suggest that C++ compilation times will only continue to get worse (more constexpr/consteval, even more complex templating features/concepts, etc.).

Re:C++ Cheat Sheets by ncmncm on Sunday March 06, 2022 @09:39PM (#30583621)
Constexpr improves compilation speed by the degree that it displaces template metaprogramming. Template metaprogramming runs at Python speed, where constexpr runs orders of magnitude faster. Anywhere you have a choice between them, choose constexpr.

Substantial improvement in compilation speed might depend on use of JIT techniques, running generated code in the compiler to perform template evaluation. I.e., a template is not just a data structure, it is a compile-time function that, where used much, is compiled to optimized machine code, its run-time values being what we think of as types. Thus far, all these functions are run like an interpreter walking a syntax tree.

Precompiled headers, or module intermediate files, could have this code in them already optimized.

Re:C++ Cheat Sheets by McKayDavis on Sunday March 06, 2022 @06:33PM (#30582358)
> * Remove all unnecessary headers. Template expansion is slow, and preprocessing is even slower. Some of the standard includes (like `<regex>` and `<iostream>`) are notorious for slowing individual translation units to a crawl. `#pragma once` for your own headers also helps with cpp-time performance.

> * Forward-declare as much as you can. Forward type declarations mean that the compiler doesn't need to process all of `Foo` when it sees `Foo&` or `Foo`.

I've found the include-what-you-use (IWYU) tool [1][2] can help immensely with automating this process, especially on large code-bases.

It uses LLVM/Clang to analyze a .cpp file / translation unit and produces the minimal subset of exactly which includes are necessary and what types can be forward declared.

[1] https://include-what-you-use.org/

[2] https://github.com/include-what-you-use/include-what-you-use

Re:C++ Cheat Sheets by jcelerier on Monday March 07, 2022 @03:24AM (#30585411)
Yep, all of this is good advice but requires being able to change the code, which is far from a given in c++ where you may have to work with a dozen proprietary SDKs.

I personnally use clang -ftime-trace to see which headers take the most time and then Qt Creator's "find all places where this file is included" feature to try to make the most efficient cleanups

Re:C++ Cheat Sheets by gpderetta on Monday March 07, 2022 @04:33AM (#30585809)
All good suggestions. I might add: ccache and distcc to speed up full rebuilds.
Re:C++ Cheat Sheets by techas on Sunday March 06, 2022 @04:44PM (#30581557)
Off topic. Could you recommend an ide to work with c++?
Re:C++ Cheat Sheets by jll29 on Sunday March 06, 2022 @04:49PM (#30581615)
CLion from JetBrains and Microsoft Visual Code are the best commercial ones for Linux, NetBeans is a free (as in freedeom and open source) one (that also supports many other languages like Java and Python).

What I don't recommend is Eclipse - it's complex and confusing for beginners (but a bit faster than some others).

Re:C++ Cheat Sheets by MetricExpansion on Sunday March 06, 2022 @05:22PM (#30581863)
I didn’t think NetBeans supported C++ anymore?
Re:C++ Cheat Sheets by mdaniel on Sunday March 06, 2022 @07:47PM (#30582820)
Yeah, at least according to the repo it seems they now delegate to the ccls language server and just do syntax highlighting via the textmate plist grammars that are so commonly paired with language servers

* https://github.com/apache/netbeans/tree/12.3/cpplite#cpplite

* https://github.com/apache/netbeans/blob/12.3/cpplite/cpplite...

Re:C++ Cheat Sheets by ibobev on Sunday March 06, 2022 @04:48PM (#30581599)
Microsoft Visual Studio for Windows and QtCreator for Linux.
Re:C++ Cheat Sheets by CornCobs on Sunday March 06, 2022 @07:53PM (#30582858)
I find visual studio to be absolutely horrendous for C++. Refactoring doesn't work (even a simple rename symbol!) The errors aren't accurate, autocomplete seems to suggest every symbol from the stdlib before the class defined in the file itself and can't tell when it should suggest classes vs functions/variables.

Oh and of course compile errors are absolutely useless but I suppose that's a C++ thing in general. The error panel shows the (last? First?) Error in a chain of errors, which is always something deep in library code. E.g. accidentally tried to copy a non-copyable object using unique_ptr? You can't find the offending callsite, it just points you to xmemory and at best the class being wrongly used.

Re:C++ Cheat Sheets by ncmncm on Sunday March 06, 2022 @09:46PM (#30583677)
Compile errors will anyway be getting better as concept support gets built out.
Re:C++ Cheat Sheets by mpreda on Monday March 07, 2022 @12:49AM (#30584584)
qt-creator is a nice open-source IDE:

https://github.com/qt-creator/qt-creator

They offer binary releases, but it's also possible to compile from source without too much trouble. Written in C++, it's pretty snappy. Supports meson.

Re:C++ Cheat Sheets by dwrodri on Sunday March 06, 2022 @05:23PM (#30581873)
I can testify to JetBrains products, they're quite good. I haven't used CLion in a few years because I don't like depending on a dev tooling that costs money. I use VSCode because I hop between Python, Bash, Go, C, and C++. That being said, CLion definitely offers a much better debugging experience, even when VSCode has all the right plugins.
Re:C++ Cheat Sheets by mattgreenrocks on Sunday March 06, 2022 @06:15PM (#30582249)
CLion is quite good IME. Visual Studio, historically, has been excellent, but I haven’t used Windows full time in a decade.
Re:C++ Cheat Sheets by jcelerier on Sunday March 06, 2022 @05:21PM (#30581853)
I use Qt Creator on every platform
Re:C++ Cheat Sheets by whimsicalism on Sunday March 06, 2022 @05:31PM (#30581933)
CLion, VSCode, Vim - in order of decreasing tooling.
Re:C++ Cheat Sheets by harry8 on Sunday March 06, 2022 @10:49PM (#30584037)
vim with the YouCompleteMe plugin (which uses clang-complete) is pretty good.

Contrary to a sibling post eclipse CDT is fine (especially with vim bindings).

It depends what you want from your IDE maybe?

Re:C++ Cheat Sheets by pubby on Sunday March 06, 2022 @09:39PM (#30583623)
Is clang still faster? I thought it had slowed down in the past decade and was now worse than GCC.
Re:C++ Cheat Sheets by jcelerier on Monday March 07, 2022 @04:03AM (#30585648)
For my own projects at least it is, by 10-20%
Re:C++ Cheat Sheets by jcelerier on Monday March 07, 2022 @09:55AM (#30588493)
to give a very flagrant example on a project that i am currently working on:

- GCC 11 build: 4m20 (https://github.com/celtera/avendish/runs/5449993123?check_su...)

- Clang 14 build: 2m40 (https://github.com/celtera/avendish/runs/5449993033?check_su...)

Re:C++ Cheat Sheets by gpderetta on Monday March 07, 2022 @04:36AM (#30585819)
Had the same experience. It is not a huge difference, but it is noticeable, especially in the incremental edit-build-test cycle.
Re:C++ Cheat Sheets by peter303 on Sunday March 06, 2022 @07:46PM (#30582818)
In the old days C++ pre-processed into C. Then you had to run a special linker to handle how the preprocessor created class part names.
Re:C++ Cheat Sheets by gpderetta on Monday March 07, 2022 @04:40AM (#30585845)
I thought that the old point of C++ mangling was that it could use existing linker tech. Do you have some references?

Also, my understanding is that, except for the very first few prototypes, starting from cfront on the C++ compiler was a real compiler. It just happened to target C instead of asm.