Python, as Reviewed by a C++ Programmer

  • Posted on: 21 April 2017

I haven't written here in 8 months. Part of the reason for that is that I've swapped jobs. At the old place, I spent about 90% of my time writing C++, and about 10% of the time using other languages, including Python, Perl, web languages, and last, and oldest, Fortran.

Aside: Did anyone see that movie, Hidden Figures? I loved it. But it made me a little uneasy when I realized people have been writing Fortran for about 60 years. I felt like a part of history? Not sure if good or bad.

Variadic template make_CString for MFC's CString

  • Posted on: 27 February 2016

For reasons beyond my control, I often work with a codebase that makes use of Microsoft MFC's string class, CString. CString has its detractors and proponents. I don't have a whole lot to add to the debate that hasn't been said already. Instead, I'm going to focus on using C++ variadic templates to get around one of my least favorite CString "anti-idioms". Since I'm not overly familiar with variadic templates, I plan to go over them in slow-mo.

My First Experiment with C++'s std::thread

  • Posted on: 30 January 2016

std::thread is a big step forward for multi-threaded applications written in C++. I suppose people have written their own cross-platform thread wrappers before, but for the rest of us, there's no more dealing with POSIX threads vs. Windows threads etc. std::thread is also far more 'natural' to C++ than openMP is out of the box, which is what I used pretty often in the past. By natural, I mean that one can write:

Tutorial: using cURLcpp to Create a C++ Web Reader in Ubuntu

  • Posted on: 8 March 2015

In this article, we're going to create a C++ application with cURLcpp that reads a webpage and prints the HTML source to stdout.

I wanted to try cURLcpp as opposed to the tried and usually true cURLpp, mostly for fun.

The bulk of the article is really a "for-dummies" guide to setting up your environment to do so. I won't really go into detail about how to use cURL or cURLcpp. In fact, we're just going to compile the first example from the cURLcpp readme. Once you've got a good cURLpp development 'environment' setup, it shouldn't be too hard to cut and try different cURLpp features and code.


Debugging Custom C++ Types in Codeblocks (Code::Blocks)

  • Posted on: 14 December 2014

When I develop C++ on Linux, and I'm not in a purist/makefile/command-line mood, I like to use Code::Blocks. It doesn't quite have the feature set that Eclipse does, but Eclipse always feels a bit cumbersome to me. On the flipside, Code::Blocks seems much more lightweight.

In this article, I'm going to explain how to debug custom types with Code::Blocks. First, let's look at a motivating example. Here's a cpp file that defines and uses a simple array class, myarray:

#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cstdlib>

class myarray

    myarray(int sz):
        arr( new double[sz] )

    ~myarray(){ delete[] arr;}

    double* arr;
    int     n;


int main ()
    std::cout << "custom type test" << std::endl;

    int n = 5;

    myarray the_array(5);

Reading an STL File With C++

  • Posted on: 21 October 2014

STL is a widely used 3D CAD-related filetype which, essentially, represents one or more surfaces as a bunch of triangles. Each triangle is represented by a unit normal and its three vertices.

Here’s a synopsis of the entire (binary) format, adapted from Wikipedia:

char[80] (80 bytes) – Header
unsigned long (4 bytes) – Number of triangles

foreach triangle
    float[3] (12 bytes) – Normal vector
    float[3] (12 bytes) – Vertex 1
    float[3] (12 bytes) – Vertex 2
    float[3] (12 bytes) – Vertex 3
    short (2 bytes) – Attribute byte count

Pretty simple. Just read the header, read the number of triangles, and then start reading the triangles. Each triangle's data is 50 bytes long, but the last 2 bytes are rarely used, so they could be cased to anything, really.

Triangle and Vector Classes

It'll make things simpler if we have a triangle and vector class. Here's the declaration for the 3d vector class:

Converting MATLAB to C++ -- LU Decomposition Example

  • Posted on: 24 August 2014

Some time ago, I talked about a way to convert matrix-operation-heavy MATLAB code to C++ code. Here's an outline of the method:

  1. Most importantly, there's really no conversion at all. My idea was to create some C++ which would allow MATLAB to be directly compiled as C++ -- with minimal manual changes to the original source.
  2. This is a work in progress! There are still some kinks to work out, and we make no attempt to convert code for which there is no native equivalent, like MATLAB's roots() function. We also can't fix fundamental differences in the languages, ie, in C++, variables have to be declared.
  3. You can download all the code (and a Code::Blocks project) for the example demonstrated here, here.

C++ Lambda Functions with std::sort -- A Brief Tutorial

  • Posted on: 17 August 2014

C++11 added Lambda Functions to C++, a feature many developers felt was long overdue.

There are a lot of tutorials on the web already about using Lambdas in C++. These tutorials cover all the wide variety of ways which Lambdas can be used in C++, as well as possible reasons for doing so.

I thought I'd throw my hat in the mix, and try to provide an ultra-short, ultra-simple example, adapted from code you may have seen before. We're just going to use a Lambda function to extend the functionality of std::sort.

First, I'm going to use this code for a starting point for this tutorial. So make sure that that's fully digested before heading over here.

A C++ (complex) Polynomial Root Finder -- We'll Do It Fast, We'll use Eigen

  • Posted on: 11 July 2014

Ever wonder what's behind MATLAB's roots() function?

Well, it's not really a root finding algorithm at all. It uses (I believe), the idea of Companion Matrix to cast the root finding problem into an eigenvalue problem. I won't go into the proof, but if you're familiar with the eigenvalue problem, and by extension, a matrix's characteristic polynomial, you could guess some connection might exist between polynomial root finding and the eigenvalue problem.

So, let's cook up a C++ root finding algorithm based on the companion matrix.

Once again, we're really just solving an eigenvalue problem, so there's no need to re-invent an eigensolver. Instead, we'll use Eigen to the computation. This leaves us with very little work to do. In fact, let's just skip to the code:

#include <Core>