Top 5 favorite modern C++ features

I must say, when I first started dabbling in computer programming, C++ was not at all my first choice of language. The general perception of C++ that I got from my friends and from forums were like…

C++ is a language for really, really smart people.
Each line of code is about 30-40 characters long on average, if not longer.
Everything is needlessly difficult and fraught with pitfalls.
There are so many *, #, &, <>, (), {}, /, [ ], <<, ::, and ! symbols everywhere that it looks like comic book characters swearing.
Before you even think of attempting C++, you should first know C well.
It is impossible to know the whole language. Even people who have been learning it for 15 years have barely even scratched the surface of what is possible.
Template metaprogramming is the devil.

Looking back on these statements today, as someone who now programs mostly in C++ for fun, I see a grain of truth in each of them. C++ is certainly not perfect. Far from it; it is a flawed, verbose, error-prone, needlessly complicated, sometimes rage-inducing language.

But why do I write in it, then, if it’s so terrible? And why for fun? Well, I probably would have never picked up C++ in the first place if the ISO C++11 standard hadn’t come out.

At the time, I was playing around with straight C for a while since I moved to Linux, though with time I found it to be a little too spartan for my taste. I was interested in learning C++ to make games since many well-known game frameworks use the language natively, and I wanted to carry over my C knowledge.

Originally dubbed “C++0x” before delayed to 2011, the C++11 standard added a ton of new and attractive features which attracted people like me, who were previously used to C#, Java, Go, Ruby, Python, etc. Due to extremely positive reception in the C++ community, the ISO committee has been hard at work creating the C++14 standard, and the up-and-coming C++17 standard. These last few iterations have been made such radical changes to the core language that they are colloquially referred to as “modern C++.”

C++11 feels like a new language. I write code differently now than I did in C++98. The C++11 code is shorter, simpler, and usually more efficient than what I used to write.

— Bjarne Stroustrup, creator of C++, https://isocpp.org/tour

These features made me comfortable enough to dive into C++, though they are not necessarily the reasons why I decided to stay. I stayed because I could now work almost as efficiently as I could in C# or Java, but at the same time I had the gory insides of the machine exposed to me. That’s right, I actually found it refreshing to see why the computer behaved the way it did, and not only that, I loved the fact that I could drop the fancy object-oriented stuff and fall back to glorious old C if I ever needed to. C++ also blew my mind several times over in regards to what I thought was possible to get computer programs to do (I’m looking at you, template metaprogramming).

So grab a beverage and some popcorn, because here come my top 5 favorite “modern C++” features.

Read more

Want to write an operating system?

There are people who live to make things. Some make cars, some make weapons, some make awesome Tesla coils, but have you ever heard of people who make full-blown operating systems for fun? Chances are, you probably haven’t, but there are indeed many programmers who are itching to take on such an extreme hobby.

Introduction

Operating system placement.svg
An OS is a low-level interface between the hardware and you, making the computer simple to use and program for.

Firstly, what is an operating system in very simple English? An operating system is, in theory, a relatively simple concept to understand. Naturally, it all begins with the computer itself: monitor, tower, mouse, and keyboard in all their glory, which is the hardware. But akin to any human being, no physical body can function on its own; it requires a conscious mind to operate it. This is where the operating system comes in.

In the simplest of terms, the computer hardware is a digital device, meaning each transistor on the motherboard can only process two values: 1 and 0 (otherwise known as “true” and “false”). Programs at their lowest level are written in binary “machine code”, or strings of 1’s and 0’s. When you execute a program, a stream of machine instructions are sent to the CPU which then proceeds to read them and loads up the program into RAM.

[The operating system] acts as a layer between the hardware and you, translating clicks of the mouse and taps of the keyboard into binary so that the computer can understand, as well as making sure that all the programs running are coexisting peacefully and not stepping on each others’ toes all the time.

When system developers write components for the operating system, they make use of assembly language, a low-level CPU-specific language which uses short human-readable 3- or 4-letter commands that directly translate to specific bits of machine code. Naturally, it’s a lifesaver for the purposes of writing the OS, but it is not robust, portable, and intuitive enough to use as a generalized programming language.

In order to make life easier for both users and programmers who do not want to deal with machine code directly, there is usually an operating system installed on the computer (such as Windows, Mac OS X, Linux, BSD, etc) which acts as a layer between the hardware and you, translating clicks of the mouse and taps of the keyboard into binary so that the computer can understand, as well as making sure that all the programs running are coexisting peacefully and not stepping on each others’ toes all the time.

Read more