Category Archives: C / C++ / C++11

Surpress compiler warnings of third party libraries

I like my compiler to be very verbose and pedantic. For me, -Wall -Wextra -pedantic -Weffc++ is a must. And I don’t want to see any warnings before releasing my software.

Well, but your compiler might still complain about headers of third party libraries which you do not want to touch. How to remove those annoying warnings?

Here’s the simplest solution. Just add

#pragma GCC system_header

to the affected header file and you will not be disturbed by those warnings any more.

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
   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

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!