Updated on 2019/07/03 (long overdue) after this thread on reddit. My post from last week contained a HUGE bug: optionals don’t move their content lose their value on move, so destructor on moved handles is still invoked (thus causing multiple destructions). In the brief discussion on reddit, I proposed to create a optional with move …
Continue reading Moveable optional, really?In the last few days, I needed to create a thin wrapper class that apply RAII patterns to handles coming from a C library. I did this several times in the past, but this time I think I come up with some new solutions, at least for me. In this post, I’ll consider this toy …
Continue reading Wrapping C APIs, and why I wanted a base class to be conditionally copyableOn my recent question on twitter I asked how this piece of code would behave in case of built-in arithmetic types (which I inaccurately referred to as basic numeric types): template<typename T> T f() { T t{}; while (t < t+1) { t++; } return t; } The question is more complex than it seems. …
Continue reading Can you count to infinity?I’m going to be at GDC talking about… Deep Learning applications! No C++ at all! Incredible, isn’t it?
Continue reading GDC 2017Despite the name, perfect forwarding is not so perfect. In the following example, a class B is constructed by passing a : struct A { int x, y; }; struct B { B(A a) { /*…*/ } }; int main() { B b1({1, 2}); } Everything’s ok: {1, 2} is deduced to be the …
Continue reading Not-so-perfect forwardingLast Saturday I’ve been giving two presentations about C++11&14 I originally wrote for NVIDIA internal tech talk at C++ User Group in Udine meetup, invited by Nicola Gigante (@gignico). It has been a very nice meetup, with a lot of nice people, and I’m willing to join them again in the future, if possible. During the first one, …
Continue reading LSIL,TTNG (Last Saturday I Learned, Thanks to Nicola Gigante…)Note to self: Never try to modify your examples in realtime during a presentation, to show an idea that just came through your mind: You might succeed. The problem We have an old pre-C++11 class: class C { string _s; public: C(const string &s) : _s(s) {} const string &get() const { return _s; } …
Continue reading Optimizing return valuesAs stated in a previous post, final keyword enables the sealing of classes and methods. This is important because it allows interesting compile-time checks, but also enables quite a powerful optimization: the devirtualization. Devirtualization happens when the compiler can statically decide, at compile time, which function should be called, so it can produce a direct call to that function, or …
Continue reading The power of devirtualizationI’m not posting anything since GDC, and the reason is that I’m not working on C++ very much lately, because I’m quite busy in trying to make some sense out of the shamanic art they call deep learning. Luckily I’ve been invited to talk at the Italian C++ Day in Firenze on October 29th (http://www.italiancpp.org/event/cppday16/, …
Continue reading A digression…NOTE (2016/03/26 01:30PM): The content of this post has has been fixed. I must thank Lawrence Crowl for pointing out my mistake at SG14 GDC 2016 meeting. It is well known that using ordering comparison operators (<, <=, >, >=) with mixed signed/unsigned types result in warnings and, ultimately, in undefinedunexpected behaviour. I never loved this, and I …
Continue reading Signed/Unsigned operations and undefined behaviour