C++’s “pointer-to-member” operator

Lessons learned today: There are still operators in C++ I did not know 🙂

I was suprised when I saw these strange operators ->* and .*.

You can use those operators when you have a pointer to a member function. In order to call this function, you need an explicit instance. ->* and .* combines function pointer and instance. .* is a built-in operator and cannot be overloaded. ->* may be overloaded.

class A
{
public:
   void f() {}
   void g() {}
};


...

void (A::*ptr)(); // This is a pointer to a member function of A

// Let the pointer point to f
ptr = &A::f;
// We cannot use this pointer without an object
A a;
(a.*ptr)() // Call f on instance a
// Now we have a pointer to an instance
A *b = new A;
(b->*ptr)() // Call f on instance b

More on this

Pigz – Parallel gzip

This nice tool really is amazing:

Pigz is a parallel implementation of gzip and can be used like gzip.
As it is able to spawn mutiple threads it is much faster than the single-threaded gzip.

Usage:

pigz -p 8 file # compresses file to file.gz using 8 threads
pigz -p 8 -d file.gz # decompresses file.gz to file using 8 threads

pv file.gz | pigz -d -p 8 > file # decompresses file.gz to file using 8 threads and a nice progress bar using pipe view :-)

dd over ssh / netcat

Nice trick to run dd over ethernet:

Source:
dd bs=16M if=/dev/sdX | gzip | nc server 1000

Target:
nc -l -p 1000 | gunzip | pv | dd bs=16M of=/dev/sdX

or via ssh:

dd bs=16M if=/dev/sdX | gzip | ssh user@server "gunzip | dd bs=16M of=/dev/sdb"

Getting Date and Time in C++11

You remember the days when it was necessary to use strftime to convert time to human readable strings? Even in C++ it actually was a pain in the ass.

This is the way how you can do in in C++11:

#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
    auto t = std::time(nullptr);
    auto tm = *std::localtime(&t);
    std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
}

Hell yeah, I love C++11!