How to add libraries in C++?

This would probably interest you, but here is a short version: When you assemble the .cpp, .c or whatever files, each translation unit (that is, each file) generates an object file. When creating the final executable, you combine all the object files into a single binary. For static libraries, you compile the static archive (.a … Read more

CMake install (TARGETS in subdirectories)

According to this bugreport, install(TARGETS) command flow accepts only targets created within the same directory. So you need either move the add_library() call into the top-level directory, or split install(TARGETS) call into per-target ones, and move each of them into the corresponding subdirectory. Since CMake 3.13 install(TARGETS) can work even with targets created in other … Read more

Why does the standard C++ library use all lower case?

Main reason : To keep compatibility with the existing code, since they have done it with C also. Also have a look at these C++ Coding standards, which presents some generic reasoning regarding the importance of convention. These links discusses about the naming conventions of C/C++ Standard Library. Naming Convention for C API C/C++ Library … Read more

C++ vector of arrays

Unfortunately, std::array does not have an initializer list constructor. Indeed, it has no user-defined constructor whatsoever — this “feature” is a leftover from C++03 where omitting all user-defined constructors was the only way to enable the C-style brace initialization. It is IMHO a defect in the current standard. So why doesn’t built-in brace initialization work … Read more

Implementing Move Constructor by Calling Move Assignment Operator

[…] will the compiler be able to optimize away the extra initializations? In almost all cases: yes. Should I always write my move constructors by calling the move assignment operator? Yes, just implement it via move assignment operator, except in the cases where you measured that it leads to suboptimal performance. Today’s optimizer do an … Read more

Should I use std::default_random_engine or should I use std::mt19937?

For lightweight randomnes (e.g. games), you could certainly consider default_random_engine. But if your code depends heavily on quality of randomness (e.g. simulation software), you shouldn’t use it, as it gives only minimalistic garantees: It is the library implemention’s selection of a generator that provides at least acceptable engine behavior for relatively casual, inexpert, and/or lightweight … Read more

Copy Constructor in C++ is called when object is returned from a function?

It’s called exactly to avoid problems. A new object serving as result is initialized from the locally-defined object, then the locally defined object is destroyed. In case of deep-copy user-defined constructor it’s all the same. First storage is allocated for the object that will serve as result, then the copy constructor is called. It uses … Read more