Skip to main content


Bootstrapping a vcpkg-based project in Linux and Windows with idiomatic cmake

This blog is part #2 in the series of trying out different package managers to bootstrap a cmake project. Checkout part #1 about Bootstrapping a vcpkg-based cmake project in Visual Studio. The cmake code in the previous post works well on Linux too. After all, both cmake and vcpkg are designed for cross-platform build management. So what's new here?

This time around we'll get the same project off the ground in both Linux and Windows with cmake proper. Last time, the cmake script CMakeLists.txt felt like a poorly written bash script. Since that blogpost, I received a lot of feedback.

Feedback from Carlos ORyan (Google) forms the basis of this blog post. It would be more accurate to say that I'm downright stealing the cmake-vcpkg integration scripts he shared with me. They are open-source and available at google-cloud-cpp/super. I've copied them nearly verbatim to my vcpkg_cmake_blog branch for ease of use and long term stability of the hyperlinks. Thanks Carlos!

Recent posts

Bootstrapping a vcpkg-based cmake project in Visual Studio

After the last week's 2019 Microsoft MVP Summit, I decided to give Microsoft vcpkg a shot. I've a cmake project at work and we target Linux using the Hunter package manager. So vcpkg had been on the back-burner for me.

I'm targeting a 4 part blog series. Bootstrapping a cmake project based on vcpkg in Visual StudioBootstrapping a cmake project based on vcpkg in LinuxBootstrapping a cmake project based on Hunter in LinuxBootstrapping a cmake project based on Hunter in Windows As of this writing I'm new to vcpkg. So I apologize in advance if you are annoyed by noob mistakes. Please leave a comment if you notice something.

If you prefer to clone/browse a github project. All contents in this blogpost are available under cpptruths/cpp0x/vcpkg_test (branch vcpkg_cmake_blog).

To start with, I've a barebones C++ project with nearly empty driver.cpp and driver.h files. Later, I'll add Boost core and optional as third party dependencies. Both are header-only. Later,…

Review of Manning's Functional Programming in C++

Last year I reviewed the pre-print manuscript of Manning's Functional Programming in C++ written by Ivan Čukić.
I really enjoyed reading the book. I enthusiastically support that the book
Offers precise, easy-to-understand, and engaging explanations of functional concepts.Who is this book for This book expects a reasonable working knowledge of C++, its modern syntax, and semantics from the readers. Therefore, reading this book might require a companion book for C++ beginners. I think that’s fair because FP is an advanced topic. C++ is getting more and more powerful day by day. While there are many FP topics that could be discussed in such a book, I like the practicality of the topics selected in this book.

Here's the table of contents at a glance. This is a solid coverage of functional programming concepts to get a determined programmer going from zero-to-sixty in a matter of weeks. Others have shared their thoughts on this book as well. See Rangarajan Krishnamoorthy&…

Unit Testing C++ Templates and Mock Injection Using Traits

Unit testing your template code comes up from time to time. (You test your templates, right?) Some templates are easy to test. No others. Sometimes it's not clear how to about injecting mock code into the template code that's under test. I've seen several reasons why code injection becomes challenging.

Here I've outlined some examples below with roughly increasing code injection difficulty. Template accepts a type argument and an object of the same type by reference in constructorTemplate accepts a type argument. Makes a copy of the constructor argument or simply does not take oneTemplate accepts a type argument and instantiates multiple interrelated templates without virtual functions Lets start with the easy ones.
Template accepts a type argument and an object of the same type by reference in constructor This one appears straight-forward because the unit test simply instantiates the template under test with a mock type. Some assertion might be tested in the mock c…

Simple Template Currying

Currying is the technique of transforming a function that takes multiple arguments in such a way that it can be called as a chain of functions, each with a single argument. I've discussed Currying on this blog previously in Fun With Lambdas C++14 Style and Dependently-Typed Curried printf. Both blogposts discuss currying of functions proper. I.e., they discuss how C++ can treat functions as values at runtime.

However, currying is not limited to just functions. Types can also be curried---if they take type arguments. In C++, we call them templates. Templates are "functions" at type level. For example, passing two type arguments std::string and int to std::map gives std::map<std::string, int>. So std::map is a type-level function that takes two (type) arguments and gives another type as a result. They are also known as type constructors.

So, the question today is: Can C++ templates be curried? As it turns out, they can be. Rather easily. So, here we go... #includ…

Non-colliding Efficient type_info::hash_code Across Shared Libraries

C++ standard library has std::type_info and std::type_index to get run-time type information about a type. There are some efficiency and robustness issues in using them (especially when dynamically loaded libraries are involved.)

TL;DR; The -D__GXX_MERGED_TYPEINFO_NAMES -rdynamic compiler/linker options (for both the main program and the library) generates code that uses pointer comparison in std::type_info::operator==().

The typeid keyword is used to obtain a type's run-time type information. Quoting cppreference. The typeid expression is an lvalue expression which refers to an object with static storage duration, of the polymorphic type const std::type_info or of some type derived from it.std::type_info objects can not be put in std::vector because they are non-copyable and non-assignable. Of course, you can have a std::vector<const std::type_info *> as the object returned by typeid has static storage duration. You could also use std::vector<std::type_index>. st…

Chained Functions Break Reference Lifetime Extension

I discovered a reference lifetime extension gotcha.

TL;DR; Chained functions (that return a reference to *this) do not trigger C++ reference lifetime extension. Four ways out: First, don't rely on lifetime extension---make a copy; Second, have all chained functions return *this by-value; Third, use rvalue reference qualified overloads and have only them return by-value; Fourth, have a last chained ExtendLifetime() function that returns a prvalue (of type *this). C++ Reference Lifetime Extension C++ has a feature called "reference lifetime extension". Consider the following. std::array<std::string, 5> create_array_of_strings(); { const std::array &arr = create_array_of_strings(); // Only inspect arr here. } // arr out of scope. The temporary pointed to by arr destroyed here. The temporary std::array returned by create_array_of_strings() is not destroyed after the function returns. Instead the "lifetime" of the temporary std::array is extended …