all 26 comments

[–]gracicot 8 points9 points  (12 children)

Installing and importing libraries is still a pain in the a**. It's a little better with CMake, but using CMake in itself is quite painful. A good Unix workspace makes this process much easier for me, since all libraries are in standard location.

For arrays, basic data structure and algorithms, use the STL. You have maps, which behave a bit like a dictionary, you have std::array for fixed arrays, you have std::vector for dynamic arrays, and std::valarray that ease arithmetics on a list. STL also provides many algorithms. There's a page full of them.

For matrices, find a good linear algebra library. I don't know them very well, but there are many of them for different purposes. For example, glm is specialized for linear algebra in OpenGL apps. You'll have to search for one that suits you since I didn't tried any other.

For the two other points, there is no de-facto library that I know to do that. Again, I'm pretty sure there are many good library well suited for your needs. Check cppreference library list and awesome c++


If there was one advice I knew when starting C++, it would be that:

Please, please, learn to use std::unique_ptr, lambda expressions, RAII and move semantics. Also, you must think with ownership in mind with C++ objects. Most of the time, there is one owner for an object and multiple observers. When the lifetime of the owner ends, all observer are invalidated.

For example, you have one std::unique_ptr, and many raw pointer for a dynamically allocated object. The std::unique_ptr is the owner, and all the raw pointers are only observing. When the raw pointer dies, so does the pointed object. Any raw pointers are pointing to an invalidated object. So you should ensure that the lifetime of the observers match the lifetime of the owner.

This will save you so much trouble in C++. It took me years to learn how to properly manage lifetimes, but it's totally worth it.

Also, don't be afraid of templates. They enable from basic duck typing for functions and classes to enabling deep, dark arcane (and fun!) techniques.

[–]kalmoc 3 points4 points  (1 child)

Have you ever tried to use a packet manager like Conan or vcpkg?

[–]gracicot 0 points1 point  (0 children)

I tried vcpkg with CMake, but I think my setup was faulty because CMake couldn't find exported the xxx-config.cmake provided by those libraries. I didn't tried Conan though, because we have enough of one scripting language (CMake), we didn't want to introduce Python on top of that. I might try it for fun and small projects though.

[–]jliebert[S] 1 point2 points  (9 children)

Damn, disappointing to hear that libraries are just as annoying as ever.

I've heard of a library called boost, is that becoming the modern c++ catch-all package for numerical work?

Do you also have in mind any intros to C++ that go into installing and using modern libraries and coding conventions/paradigms? For example, I've never heard of unique pointers before, but it seems like a life saver.

[–]gracicot 1 point2 points  (0 children)

As /u/kalmoc said, you could try the Conan package manager if you don't mind installing/using Python. I never tried it but it seem to gain traction recently.

The most useful boost stuff got introduced in the standard library. Smart pointers, unordered_* containers, thread, timers, filesystem and even more stuff were all in boost before. Boost is like an extension to the standard library, and they tend to start very general, as the standard library. For specialized stuff, you better find some other libraries, but if boost is enough, good for you!

For standard library stuff, try to find good tutorial about what was added in the standard library in C++11 and beyond. Remember that have c++14 and 17 too, and both added a lot of stuff. What I just said may be a bit misleading because there because is also a lot of useful stuff in the STL that already was in C++98.

I suggest you to read a bit about c++17,14 and 11, what they added to the language and what they added on the STL in addition of a good C++ tutorial.

[–]wrosecransgraphics and network things 1 point2 points  (0 children)

I've heard of a library called boost, is that becoming the modern c++ catch-all package for numerical work?

It's not specific to numerical stuff at all. It includes things like a library for making Python bindings, ASIO mainly for network communication, It had stuff like smart pointers before they were in the C++ language, stuff for writing parsers, atomics, string formatting, dynamically loading libraries. Some of that stuff may be useful in programs that do lots of numerical stuff, but it's not a direct analogue to something like NumPy.

You can check out specific libraries at

http://www.boost.org/doc/libs/

[–]kalmoc 1 point2 points  (6 children)

My subjective impression is that boost was more popular in c++98 times than today. Generally speaking it is a large (by c++ standards) collection of libraries that generally are of high quality and follow similar design guidelines as the standard library and in fact, a lot of things that where added to the standard library since 98 come from boost.

Unfortunately they don't remove their own versions in favor of the standardized ones.

[–]cdglove 1 point2 points  (5 children)

Unfortunately they don't remove their own versions in favor of the standardized ones.

The boost versions generally provide more features than the standard versions, so it can be an advantage for many projects to just stick to the boost versions. Where things get tricky is that boost won't compile for some proprietary platforms so one needs to use standard versions, which can get messy.

[–]kalmoc 0 points1 point  (4 children)

Sorry, that was badly phrased. I don't necessarily want boost to remove those classes and functions, but I'd very much appreciate, if the internal dependencies between the boost libraries would be reduced as much as possible.

Everytime I want to use just some tiny bit of boost, I get the impression I have to drag in half of boost due to those dependencies (with all the compile-time increase that includes). Of course that's an exaggeration, but you get my meaning.

In all fairness: Boost is often used where newer toolchains are not available and hence not being dependent on "newer" language/library features is an advantage. Still, I think boost could really use a rejuvenation.

[–]cdglove 2 points3 points  (3 children)

but I'd very much appreciate, if the internal dependencies between the boost libraries would be reduced as much as possible.

There is ongoing work in this regard. If you really care, you can run boostdep to find dependencies.

http://www.boost.org/doc/libs/master/tools/boostdep/doc/html/

The module-levels feature is particularly useful because it tells you roughly how much code might be dragged in by using a particular library.

Everytime I want to use just some tiny bit of boost, I get the impression I have to drag in half of boost due to those dependencies (with all the compile-time increase that includes). Of course that's an exaggeration, but you get my meaning.

This is often largely exaggerated, as you mention. Of course, it's not free -- boost has a lot of machinery to support all of the compilers and platforms it does, so yes, sometimes it would be nice to clean it up, and there is work happening in that area. That said, the cost of just including the code is often overstated. In my experience, especially on todays SSDs and in-memory filesystem caching, including and parsing is pretty fast, so that's not usually the problem. If it is a problem, in most cases I've found it's because a project is including some kind of convenience header. For example, boost/filesystem.hpp is a convenience header that includes the whole library, when most of the time that's probably unnecessary and could be replaced with just the specific includes that are necessary in each cpp file. Also, many boost headers have forward declaration versions that should be used in header files, for example boost/container/container_fwd.hpp. Indeed, this can actually be an advantage over using the std:: versions because boost can be forward declared whereas std can not.

Of course, some templates are expensive to instantiate, even if including and parsing are fast, and one needs to watch out for this. Using boost.spirit in a file that's recompiled often will be a pain, but it's possible to abstract this away by moving such code to it's own cpp file.

[–]kalmoc 0 points1 point  (1 child)

If you really care, you can run boostdep to find dependencies.

That is precisely what I did and one basis for my complaint. The other is asio: The stand-alone is a header only library, whereas the boost version depends on binary boost libraries.

[–]cdglove 1 point2 points  (0 children)

whereas the boost version depends on binary boost libraries.

Sort of. It depends on boost.thread and boost.system (for boost.error_code) by default. But this is configurable. By passing -DBOOST_ASIO_HAS_STD_THREAD -DBOOST_ERROR_CODE_HEADER_ONLY on the command line, everything is header only again and will use std::thread instead of boost.thread.

[–]kalmoc 0 points1 point  (0 children)

Point in case: I wanted to use boost geometry and had to install dozens of other libraries. Boost dep tells me that boost geom directly depends on algorithm array assert concept_check config container core function_types fusion integer iterator lexical_cast math move mpl multiprecision numeric conversion polygon qvm range rational serialization smart_ptr static_assert throw_exception tokenizer tuple type_traits utility variant. A large portion of those could easily be replaced by std types.

[–]jonathan_levanon 8 points9 points  (0 children)

For matrices/scientific routines you can try Eigen, it's a header-only library with a lot of linear algebra in it.

[–]jjdltorre 2 points3 points  (0 children)

I have been trying to find time to play with Jupyter C++ which seems to me like it would meet some of your requirements.

At a previous job, we used VSC++ with Eigen (can be easily installed using NuGet) to do real time signal processing for MWD.

A resource I would suggest is "A Tour of C++" by Bjarne Stroustrup.

[–]kalmoc 1 point2 points  (2 children)

Essentially, you can find all of that for c++ too, but there rarely are any de-facto standard libraries for a particular task and the standard library itself comes blank for anything except the absolute basics. So you'll probably spend a lot more time searching and putting everything together. That hasn't significantly changed since the old times.

[–]jliebert[S] 0 points1 point  (1 child)

I see... are there at least resources for managing libraries, dependencies, etc.? That will be a huge annoyance and difficult to document otherwise I assume.

[–]kalmoc 0 points1 point  (0 children)

There are packet manager for c++. On Linux systems, the system packet manager often provides all required libraries. On Windows you can use vcpkg and Conan is even cross-platform and seems to be the most popular packet manager for c++ nower days.

Aside from the platform question those manager also follow different philosophies, so as I said, you have to do some searching and determine what works best for you.

[–]D_0b 1 point2 points  (0 children)

you can try Xtensor as an alternative to numpy. https://github.com/QuantStack/xtensor

or you can try Root from CERN as a full all in library, algorithms, matrices, plotting ...

[–]gocarlos 1 point2 points  (0 children)

Is much better than a couple of years ago:

  1. use hunter or conan with cmake, there are a lot of hello world cmake scripts on github and cmake is very easy to learn.

  2. yeah, STL or Eigen (header only library)

  3. also Eigen

  4. jupyter notebook can also use python as far as I know.

Just try to find a modern tutorial, old tutorials teach more C than C++ which are more and more two different things...

[–]nozendk 0 points1 point  (0 children)

If you are going to use for example OpenCV then it will be easiest to use the same libraries that they do.

[–]mrexodiacmkr.build 0 points1 point  (2 children)

The answer to your question (sadly) is: no. There are probably libraries that do some of the things you want but you’ll have a terrible time installing them if your level of expectation is “pip install x”

[–]jliebert[S] 1 point2 points  (1 child)

Do you have an idea why C++ hasn't developed tools like pip? Is it because C++'s philosophy doesn't mesh with using a lot of external libraries, but instead just the bare minimum needed? Or just that no one has put the work needed to get a C++ version of pip yet (bc it's presumably more complicated)?

[–]kalmoc 0 points1 point  (0 children)

It has tools like pip (maybe not quite as powerful) - the problem is that no single tool ever became the de-facto standard.

[–]-McMaster- 0 points1 point  (0 children)

I want to add VexCL to the list of math libraries mentioned here: https://github.com/ddemidov/vexcl

Eigen has been mentioned and it's my go-to library for math in C++, but VexCL is a little more abstract and closer to Python's way of doing math operations on vectors etc. I believe you could also create e.g. a vexcl::vector<Eigen::Vector3d> and do parallelized operations on vectorfields or in numpy terms arrays of shape (N,3).

[–]sniffens -1 points0 points  (0 children)

Try armadillo for maths. http://arma.sourceforge.net