Marco Foco

Discussing with another C++ programmer, we came out with this piece of code: #include <iostream> template <class… Args> void foo(int, Args…) {     std::cout << “1”; } template <class…Args> void foo(Args…, int) {     std::cout << “2”; } int main() {     foo(11); // A     foo<>(11); // B     foo(11, 33);                    // C     foo<int>(11, 33);               // D     foo(11, 22, 33);                // E     foo<int, int>(11, 22, 33);      // F } I was pretty convinced that all the calls to foo were ambiguous, so I decided to test it with different …

Continue reading Curiosity #20160225

A nullptr is a representation of the null pointer that implicitly converts into any pointer type, but not into an integral type. nullptr, and its type nullptr_t, have been standardised in C++11, but its introduction predates the standard by at least 15 years, since the idea is contained in Scott Meyer‘s book Effective C++, 2nd edition, published in 1996. Despite …

Continue reading Use nullptr if you can, 0 if you need, but please don’t use NULL

I’m preparing some slides to explain how to write better code with C++11/14, and I wanted to start from the last thing I wrote about here, the use of keywords final and override. The scope of my previous post, albeit a bit cryptic, was to show how those keywords are context-sensitive, so you can also use  final and override as type names, as …

Continue reading Final & Override again

namespace override { class override { }; class override_final { ::override::override override; public: virtual ::override::override & final() { return override; } }; } namespace final { class final final : public override::override { }; class final_override final : public override::override_final { ::final::final override; public: ::final::final & final() override final { return override; } }; } …

Continue reading Please, use final and override!

In these days I’m porting a library from Qt4 to Qt5, and here’s how sticking to C++11 syntax has helped me catching a subtle porting problem. I had a class subclassing QTcpServer, overriding a virtual method with this syntax: void incomingConnection(int descriptor) override; Specifying the method as override  was enough to discover a breaking change in …

Continue reading Override and errors

Last week I was playing with C++, trying to rewrite again the “usual” n-dimension Vector/Point/Matrix library. This is an experiment (someone would call it a kata) I did several times mostly as a test bench for language/compiler features I’m learning (e.g. lazy evaluation with expression templates, SIMD intrinsics, etc…). Objects of this library usually represent these entities: Points – …

Continue reading Reinventing the array – preface