-
C++: Simple Permutation and Combination Parallelism
Primary Motivation Single Threaded Permutation Multi-Threaded Permutation Single Threaded Combination Multi-Threaded Combination Primary Motivation My Github repository, Boost Concurrent Permutations and Combinations on CPU, has been up since January 2017. However, download is close to none while my older single threaded next_combination has plenty of downloads. So I figured it must be the code usage…
-
Improved Next Combination with State
To speed up next_combination, we can store the state of generated combination so that it does not have to find which current combination elements correspond to the bigger collection. One way to do it is to store this state inside a class but this violates the design of STL algorithms. Another way to do it,…
-
Making HTTP REST Request in C++
Introduction Today, I am going to show you on how to make HTTP request to a REST server using C++ Requests library by Huu Nguyen. Mr Nguyen is heavily influenced by Python Requests design philosoply when writing C++ Requests. Those who had used or familiar with Python Requests, should feel right at home with C++…
-
C++11: Compile-time Conditional Types
Introduction C++11 introduces std::conditional to give C++ developer the flexibility to choose a type based on the compile-time condition. If the boolean parameter of the std::conditional is true, then the delved type is class T or else it is class F. Below is an example on how to use std::conditional. Before to use std::conditional, we…
-
Use STL copy, Not memcpy to Copy Array
Many C++ developers like to use memcpy() to copy POD arrays to extract the maximum performance during the copy. See the POD structure below. What if a new unsuspecting developer adds a string member and is not aware that the code uses memcpy() to do copying? memcpy() only makes shallow copies. The code no longer…
-
Erase-remove Idiom Revisited
Recently, I reread Erase-remove Idiom on classic Effective STL by Scott Meyers, which is dated by now, so I also consulted the same topic on another STL book published in 2017. How to remove elements from container is a common C++ interview question, so you may want to sit up and pay attention here. std::vector…
-
C++ Summing For Loop Benchmark
Introduction The initial motivation is to find out the overhead of different for-loop types in C++. Code Copy and paste below code into Godbolt Online C++ Compiler to see the generated assembly code. Note: The array or vector are initialized in the benchmark. The simplified code below is for you to copy and paste…
-
C++ Tip: Modification Inside const Function
Introduction There are times where modification inside const member function must be done (for example, to allow for caching or memoization). The mutable keyword and const_cast are well-known in the C++ circles to work around these. The 3rd way is to use a const pointer; we cannot modify the const pointer but we can…
-
C++ Tip: Make Your Class Non-Copyable Without Boost
Introduction There are times where an object should never be passed by copy but by reference or pointer. For instance, the class has a data member (like counter or mutex) which should not be duplicated. In this tip, we take a look at 2 techniques which declare the class to be non-copyable without resorting to using…