Ukulele Caving

I took my Ukulele into a cave in China a couple of years ago (it goes on all caving expeditions with me, to the apparent annoyance of almost everyone except me) and did some recording. The acoustics were awesome! Unfortunately I can’t remember the name of the piece though. I saw someone playing it on Youtube at some point and got hold of the music, but I’ve completely forgotten what it’s actually called (and it probably doesn’t much resemble the original anyway…

The particular cave was at the bottom of a local villager’s garden, with the entrance being used to dump rubbish into (sadly). But after pushing past, you arrived at some lovely tall slender galleries, and some formations. After a few hundred meters, the way is blocked with mud, but the passage may continue down to give another entrance into the master system we were exploring, hundreds of meters below.




Things I should know about C++ by now

At some point I started collecting a list of “Things I Should Know About C++ By Now”. Why? Well, the problem is that C++ is an extremely subtle language, and if you forget something important, it will very happily bite you in the ass. Having a list of reminders has been a good way for me to (try to) remember not to be stupid. I have tended to write down short items which make perfect sense if I can remember what they are about. Then if I don’t understand the item, I know it’s worth Googling to remind myself. I think this is a good way to remember lots of things without having to keep extensive notes.

Most of these things come from watching videos by the great Stephan T Lavavej, reading Herb Sutter’s blog, reading books by Scott Meyers, and watching videos by Chandler Carruth. But going through all of that material every time I want to remember something cool is… impractical.

So whenever I come across something that I don’t want to forget, or that I find cool (and obscure) I put it on the list. It’s got to be quite the reference, and so I thought it would be nice to share it with others. However, by now it has grown to be rather… long. So perhaps I will do multiple parts. For now, here is…

Part 1

Disclaimer: A good rule when reading this kind of thing is: “Don’t believe anything you read without checking it out yourself”.

  • Consider not making your << operators friends, use a print(std::ostream) function in the class, and call it from an external function. You will get better encapsulation.
  • It is considered fashionable to complain that iostream is horribly inefficient.
  • In C++, don’t use “math.h”: use “cmath”. This is works for all standard C libraries (cstdlib).
  • Use std::size_t for indices into memory, since the standard guarantees that it will be big enough. Even better: consider using vector<T>::size_type for indices of vectors, and std::iterator_traites<T>::difference_type when given iterators.
  • Always consider explicit when creating a class with one parameter. It can make difficult bugs into compile time errors.
  • Understand, and beware of slicing.
  • Don’t forget about the ‘override’ keyword in C++11. You will save yourself a lot of bugs.
  • Consider trying lldb instead of gdb. Use “f n” to visit a stack frame.
  • Prefer 0 or nullptr (>= C++11). Never write NULL unless the value is a pointer.
  • Don’t use old C style casts! Use double() not (double).
  • include all includes, even if your compiler doesn’t complain. For example,  not everyone uses the same implementation of the standard library, and you  might not have the includes you expect.
  • Use <iosfwd> when you only need the declarations for the standard streams,  and not the definitions.
  • Understand that iostream is not really part of the STL, and works using polymorphism.
  • Understand the most vexing parse (MVP).
  • Consider whether or not it is a good idea to use static keyword inside a function? (think singleton pattern and threads). In C++11 the standard now guarantees it’s thread safe.
  • i = i++; – an expression that is undefined.
  • Note, you can overload the comma operator (and friends: &, |).
  • Learn function pointer syntax… and then try to never use it.
  • Notice that functions can decay to their pointers.
  • You can have pointers to members. X::* pmd = &X::a; X x; x.*pmd;
  • Understand elision, NRVO, URVO, and when they apply.
  • Learn about initialisation: hi{}, hi(), hi.
  • Catch exceptions by reference(?).
  • Don’t inherit from classes without virtual destructors. override will tell you if you are doing this wrong. For example, don’t inherit from the standard containers.
  • Exceptions à la boost:
    class my_exception : public std::exception {
        virtual const char * what() const;
  • Use [&] in lambdas to catch all used variables by reference. Use [=] for values. (If you do this, then the lambda is const by default. adding mutable undoes that. weird..).
  • Understand mutable and when to use it. (it is your friend in C++11 threaded environments, see talks by Scott Meyers).
  • Understand what volatile means, and then never use it (unless you have to).
  • Yes, you can use template types. in templates:
    // Simple template class
    template <typename Type>
    class Foo
        Type bar;
    // Template template class
    template <template <typename Type> class TemplateType>
    class Bar
        TemplateType<int> bar;
  • Consider using boost::optional instead of using pointer semantics and nullptr.
  • Use RAII (read about its relation to exception handling).
  • Sometimes goto isn’t that evil…
  • Pass functors by value OR universal references (Scott Meyers lingo) (&&). Why? Because they can bind temporaries, such as lambdas.
  •  Read this:
  • Learn to recognise and use CRTP.
  • Learn about SSO and when it can be a problem.
  • Understand SFINAE
  • Use delegating constructors (but only in C++11).
  • Read about std::move_if_noexcept and why it is used in push_back.
  • Note that in C++11 you can give members default values!
  • Understand storage duration specifies.
  • Understand thread_local and… avoid it?
  • Use the correct syntax to free blocks: new[] delete[].
  • Note that you can overload new and delete operators.
  • Learn about allocators (and that they have default members in C++11. (And that they require type erasure tricks!).
  • Note that class and typename are equivalent in templates. The reason class exists is because BS didn’t want to add a new keyword. This is no longer a problem since typename exists.
  • A metafunction is a function that takes a type and returns a type:
    template <typename T>
    struct identity {
        typedef T type;

    The metafunction is used (“invoked”) like a normal type definition.

    typedef identity<int>::type mytype; 
    // Or
    identity<int>::type x;
  • Functions to count elements added to an output iterator:
    int n=0;
    // C++14:
    boost::make_function_output_iterator( [&n](auto const& e)(++n) );
    // C++11: 
    boost::make_function_output_iterator( [&n]( decltype(...) )(++n) );
    // C++03: (check?)
    boost::make_function_output_iterator(++boost::lambda::var(n)) );

    (then use std::for_each).

  • Learn how to correctly pass values to functions.. See Talks by Chandler Carruth to learn when to use value semantics (your compiler loves value semantics).
  • Know what std::unexpected is.
    look up dynamic throws?
  • Read and understand this:
    (monads, futures.)
  • Read at least a few articles here:
  • Read all of these 🙂 (Herb Sutter, C++ problems and solutions.)
  • With volatile, all bets are off. No concurrancy, synchronisation
    or optimisations. It might be multiply addressed, randomly
    changed by hardware, or just strange.
  • Read this:
  • Try looping over matrix with cols in inside and rows on inside with (with optimisations off).
  • C++11 Memory Model : SC for DRF code (sequential consistency for data race free code) (used by various other languages too.)
  • You can write int:9 to declare an ‘int’ with 9 bits (careful when using this with parallelism?)
  • “Guideline: Prefer using pimpl_’s (pointers to implementations) to insulate client code from implementation details.” – Sutter. (The idea is to hide all class internals in a struct, which is then pointed to by the class header file.)
  • Did you know, you don’t have to write return 0 at the end of int main()?
  • Play with default constructers, and find out how overloading works.
  • Look up RTTI
  • Use type erasure when a class doesn’t need to know details about something it contains.
  • Read all about the std::deque container, and how it is implemented. Take note of the iterator validity.
  • Read this:
  • Use std::transform. It’s more powerful than you probably realise.
  • Know the difference between value initialisation and default initialisation:
    int a(); int b{}; array<int, 2> foo{}...
  • Put ‘hottest’ class members first for cache efficiency. Look at class layout!
  • – 0,1,-1 are your compiler’s favourite constants. But prefer 0 (sometimes 1). Often results in much less code being generated. e.g. in enums, set most used element to be 0.

Sublime Clang Format

DragonSmallI recently wrote another plugin for Sublime Text 3 – It provides a slightly more feature-rich wrapper around llvm’s excellent ‘Clang Format’ tool, which can turn hours of clumsy reformatting C++ into a single key press.

Here it is on Package Control and GitHub.

And here’s the video that got me interested in it on the Channel 9 blog. – Note that this site is a real treasure trove for those wanting to learn some of the more crazy things about C++. My favourite series is the set of videos from Going Native 2013, I think I learnt something profound from every single video.

Anyway, being someone that is particularly picky about code formatting, this has saved me a lot of time. Having said that, I can think of a couple of improvements that would make this excellent tool perfect (for me).

  • Firstly: There should be a way to output the bounds of the new regions. This would be really useful for updating the ‘selected region’ in text-editors when the plugin is run. Perhaps this exists and I have missed it. If I have I would love to know about it! At the moment I just leave selection as it was, which means the newly selected region is essentially unrelated to the newly formatted code.
  • Secondly: Something that I very often like to do is to line up operators. This can make it much easier to spot patterns, and also bugs. Here’s an example from some code I have open right now. Here’s the my version:
    using Kernel      = CGAL::Exact_predicates_inexact_constructions_kernel;
    using Point       = Kernel::Point_2;
    using Creator     = CGAL::Creator_uniform_2<double, Point>;
    using Generator   = CGAL::Random_points_in_square_2<Point, Creator>;
    using Delaunay    = CGAL::Delaunay_triangulation_2<Kernel>;
    using Face_handle = Delaunay::Face_handle;
    using timer       = std::chrono::steady_clock;
    using ms          = std::chrono::milliseconds;

    And now the Clang Formatted version:

    using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
    using Point = Kernel::Point_2;
    using Creator = CGAL::Creator_uniform_2<double, Point>;
    using Generator = CGAL::Random_points_in_square_2<Point, Creator>;
    using Delaunay = CGAL::Delaunay_triangulation_2<Kernel>;
    using Face_handle = Delaunay::Face_handle;
    using timer = std::chrono::steady_clock;
    using ms = std::chrono::milliseconds;

    Hopefully you agree that the first of these is much easier to read quickly. I think this is the biggest thing I would like to see.

  • Finally: I don’t really like any of the default options for formatting function declarations. Personally I generally write code with two different styles: When the function signature fits on a single line, I just leave it on a single line. When it takes multiple lines, I give every variable its own line, with lined up names, and with one level of indent. I find it makes skim reading signatures very fast. But this is really an option that I can live without (it’s already fairly similar to the ‘Chromium Style’ provided by default).


iOpener: a new package for Sublime Text 3

sublime-text-2I recently wrote a new plugin (or ‘package’ in Sublime Text parlance) for Sublime Text 3 called ‘iOpener’ which tries to emulate the behaviour of the emacs ‘find file’; which I find to be much, much faster than navigating through directory trees using a UI. Feel free to install it yourself from my github: or Package Control:

In particular iOpener provides tab completion in directories, and on ‘double pressing’ tab, will give directory listings. It also stores history (in a settings file for now, since this seems to be unavailable in the API), so pressing ‘up’ will navigate to previous paths entered. Finally, it opens directories sensibly (adding them to the side bar of a new window). I personally find that opening files is now a lot quicker than before, and I hope others will agree.

A few words about Sublime Text: Sublime Text has gradually become my editor of choice, however whilst it is very close to everything that I could possibly want in a text editor (cross platform, intuitive, with platform-consistent shortcuts, easily extendible with Python…), it falls short on one big hurdle: it is proprietary and non-free. I currently use it without a licence, (and am duly prompted every dozen-or-so ‘saves’ to consider purchasing one), and I am not quite ready to part with my 50€. So, here’s the deal

I will pay for Sublime Text in a heartbeat, the day it becomes free* software.

It seems like a coy statement, but I do of course mean free as in speech. When the software is so clearly aimed at the connoisseurs of the coding world, it seems rather a shame to deny these users the opportunity to meddle, tinker and hack. I don’t deny that it would be more difficult to monatise… but I can’t be the only one that feels this way, and I hope there are others out there that would be prepared to pay for free software.

Aside from that, I found the python API to be pleasurable to use. However there seemed to be a couple of ‘gotchas’: for example, you can’t currently show an output pane at the same as an input pane, nor can you dynamically update the content of a quick pane (unless I have missed something). Both of these functionalities would seem quite basic and very useful so their omission is vexing. It would be nice to see these in a future version.

A Lemma on Intersecting Circles

I was playing with some geometric constructions recently and came across this rather nice little geometric identity. It gives the length of the line intersecting two intersecting circles in terms of the distance between them and the angle to their shared chord. Notably there is no need to know the radius of either circle. It seems nice enough that it should be known, has anyone seen it before?

circle_lemmaThe proof makes an interesting exercise. The first time I did it was with pure algebra, but then I found the following argument which is a little nicer. I’m still convinced there is a better way, however. Open to you!


Using basic trigonometric relations, we have

    \begin{align*} A+B &= \frac{h}{\tan \alpha} + \frac{h}{\tan \beta} \\[2mm] e &= 2 r_1\sin \alpha \;= \;2 r_2 \sin \beta \\[2mm] \delta &= r_1 \cos \alpha + r_2 \cos \beta \\[2mm] \end{align*}

Constructing a further right-triangle gives us h = e \cdot \sin \theta. And finally

    \begin{align*} A+B &= \frac{h}{\tan \alpha} + \frac{h}{\tan \beta} \\[1mm] &= \sin \theta \left[ \frac{e}{\tan \alpha} + \frac{e}{\tan \beta} \right] \\[1mm] &= 2 \sin \theta \left[ \frac{r_1 \sin \alpha}{\tan \alpha}+\frac{r_2 \sin \beta}{\tan \beta} \ \right] \\ &= 2 \delta \sin \theta \end{align*}

I will leave proving the second case as an exercise.

Re-implementing the Boost Filtered Iterator using Boost Iterator Adaptor

Update: I noticed that I stored a pointer to the functor type in this code for some unfathomable and un-remembered reason… Perhaps it was a dirty hack to allow default construction of the filter when the functors had no default constructors… Better to pass by value (or even better: ‘universal reference’!) if possible though, or to use boost::optional.


Recently I wanted to write my own iterator filter so that I could apply a functor directly to an iterator, before dereferencing. It took me a bit of searching around to figure out how to do it so I thought I’d share my ‘solution’ (I think there are not enough examples of Boost code on the internet).

Note that when constructing the iterator, to construct the begin() iterator, we also need the end() iterator, since the filter may go all the way to the end.

template <typename Functor, typename Iterator>
class my_filter_iterator
  : public boost::iterator_adaptor<
        my_filter_iterator<Functor, Iterator>,  // Derived
        Iterator,                               // Base
        boost::use_default,                     // Traversal
        boost::use_default,                     // Reference
        boost::use_default                      // Difference

    struct enabler {};

    Iterator    end;
    Functor*    f;


        : my_filter_iterator::iterator_adaptor_()
            { f=nullptr; }

// Construct the filter iterator.

    explicit my_filter_iterator(
        Iterator  begin,
        Iterator  end,
        Functor*  f
    ) : my_filter_iterator::iterator_adaptor_(begin), end(end), f(f) {}

    // Only to be used for creating the 'end' iterator.
    explicit my_filter_iterator( Iterator  end, Functor*  f )
         : my_filter_iterator::iterator_adaptor_(end), end(end), f(f) {}


    friend class boost::iterator_core_access;


    // This is called when the iterator is incremented.
    //We skip elements  that do not conform, stopping
    //if we hit end.
    void increment() {
        Iterator i = this->base_reference();
        // The filter.
        while( i != end && (! (*f)(i) ) )
        this->base_reference() = i;


}; // my_filter_iterator

Not-so Calypso

For reasons that are still not quite clear, I said I would re-do the Calypso alarm sound for Blackberry as a rock song.

Well, here it is as promised:

Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.

For those that don’t know the original:

China: Tian Xing 2012 follow up

Well, it’s now been quite a while since I returned from China. I had originally hoped to maintain a blog on the whole trip, however it ultimately turned out that we had no access to the internet in the village where we stayed. However, all is not lost! I wrote up the log book of all the trips (in LaTeX, of course). So for those interested in the antics we got up to, you can find all of the trip reports here: China logbook.

I uploaded a small selection of the many (read: thousands) of photos taken. These can be found on my flickr account here. Note that I didn’t take all of these photos! Many of the cave photos were taken by both me and Mandy Fu.

I also stuck together a few of the videos taken on the trip. Hopefully they provide a bit of an insight into exploration caving (link to follow).

Finally, I should thank all of those that organised the expedition and came along. We all had a great time!
Main Chamber, Da Keng Wan
(Photo by me and Mandy Fu: the large chamber we found at the bottom of Da Keng Wan)