Even when the new C++ standard was a work in progress most major compilers have been supporting the upcoming C++17 features already, now the heavy lifting of the job has been completed and just some tweaks here and there are separating us from the “official” publication of the ISO Standard, it’s a great time to publish […]
C++
C++17: Variants!

For compatibility reason to C the C++ language include the union construct which allows to manipulate several different types in a uniform manner. The reason I’m writing that this feature exist for compatibility reasons is that is almost completely useless when you’re doing object oriented programming, the only allowed types to be used with union are POD’s, non […]
About C++

When someone tell me that he know’s C++ and that he actually is a C++ programmer I believe him, but, this statement need some further explanation about which version of C++ the fellas is talking about!. Most C++ programmers knows C++03, eventually a little of C++11 and that’s all. Nowadays the tag C++ is used by […]
C++17: Folding expression.

That is one of those new additions to the language that for the most part is not understood, but that’s not a surprise since most of the people do not understand as well for what reason on heart someone included something diabolic as the variadics in the C++ language. The folding expressions was added to the […]
C++17: map splicing

Std::list have a very useful operation which allows you to move elements from one list to another, it’s called splicing. Splicing come’s almost for free, no elements are really moved but only the pointers internally used by std::list are copied from one container to another. Here’s how splicing work: The first invocation of the splice function […]
Rendering text, not that simple at all. (part 2)

Finally I’m back! September was a long and busy month, as you probably know I changed my job and had a lot of paperwork to do in relation with this event! But now it’s done! I’m on-board of a great project with great people, its gonna be cool I think. Before sharing details about my duties […]
Rendering text, not that simple at all. (part 1)

It was a kind of surprise to discover that there’s no straightforward way to render text using OpenGL, for people new to graphics programming like me drawing a text looks like a very primitive functionality which I expect to be available by default as functions like glDrawText(some context,const char*). But that’s not the case, and rendering text […]
C++17: let’s have a look at the constexpr if

There was a long discussion in the C++ community about when and how to implement the static if, the original proposal from Bright, Sutter and Alexandrescu which was aiming to define a possible implementation of this feature was resurrected after a quite long silence, just to be included in the upcoming C++17. The actual implementation of the […]
Disjoint-set or Union-find?..

I wasn’t planning any post this week, I don’t have that much free time to write posts. But I was forced to write down my own implementation of a disjoint set data structure for the purpose of one of the projects I’m working on, so I thought why not share this piece of code with […]
Unexpected stuff!

Let’s have a look at this code:
1 2 3 4 5 6 7 |
int main() { for(int i{0};i<500;i++) { std::cout<<"This is a big number: "<<i*20000000<<std::endl; } } |
And let’s compile it with g++ using the flag -O3, what’s going to happen? Before answering, let’s have a look at the assembly, shall we?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<strong><span style="color:#ff0000;">L16:</span></strong> movsbl 39(%ebx), %eax <strong><span style="color:#ff0000;">L6:</span></strong> movl %esi, %ecx movl %eax, (%esp) addl $20000000, %edi call __ZNSo3putEc subl $4, %esp movl %eax, %ecx call __ZNSo5flushEv L4: movl $22, 8(%esp) movl $LC2, 4(%esp) movl $__ZSt4cout, (%esp) call __ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i movl %edi, (%esp) movl $__ZSt4cout, %ecx call __ZNSolsEi movl %eax, %esi movl (%eax), %eax subl $4, %esp movl -12(%eax), %eax movl 124(%esi,%eax), %ebx testl %ebx, %ebx <span style="color:#ff0000;"><strong>je L15</strong></span> cmpb $0, 28(%ebx) <span style="color:#ff0000;"><strong>jne L16</strong></span> movl %ebx, %ecx call __ZNKSt5ctypeIcE13_M_widen_initEv movl (%ebx), %eax movl 24(%eax), %edx movl $10, %eax cmpl $__ZNKSt5ctypeIcE8do_widenEc, %edx <strong><span style="color:#ff0000;">je L6</span></strong> movl $10, (%esp) movl %ebx, %ecx call *%edx subl $4, %esp movsbl %al, %eax <strong><span style="color:#ff0000;">jmp L6 </span></strong> <span style="color:#ff0000;"><strong>L15:</strong></span> call __ZSt16__throw_bad_castv .cfi_endproc |
This is actually only a portion of the whole assembly, what I care to show you is the main loop and what’s […]