Grok all the things

grok (v): to understand (something) intuitively.

C++

🙄  Cynics & grumps

Ah, C++, the language we all love to loathe. It's a language that, for better or worse, has stood the test of time. But what is it, really? It's more than just an object-oriented Frankenstein's monster grafted onto the body of the good old C. It’s a whirlwind of complexity and syntactic quirks that will make even the most seasoned programmer shed a tear now and then.

Let's start with the language's history, as it's always fascinating to understand the origins of our torment. It began in 1979 as a humble side project of Bjarne Stroustrup, who decided that what C really needed was some object-oriented features. Over the years, C++ has evolved into a colossus that dominates many industries with its convoluted syntax and clever tricks, despite the constant attempt of other languages to dethrone it.

Now, let's move on to one of the many charming aspects of C++: its syntax. With an extensive feature set consisting of classes, templates, and more, who wouldn't revel in the challenge of tackling C++'s syntax? For example, let's take a look at a simple "Hello, World!" program:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Behold, the vast number of characters needed just to print a string to the console! One might wonder: why not use Python or JavaScript for such simple tasks? But never mind that; surely the power of C++ lies in its extensive capabilities—so let's not get bogged down by such petty concerns.

C++ offers a smorgasbord of features and paradigms, from procedural to object-oriented programming (OOP). OOP? Surely you remember that high school Computer Science class where the teacher never stopped talking about inheritance, polymorphism, and encapsulation. Yes, OOP is indeed wonderful—if a tad overrated—so thank C++ for being one of the pioneers in bringing us its many joys.

class Animal {
public:
    virtual void make_sound() = 0;
};

class Dog : public Animal {
public:
    void make_sound() override {
        std::cout << "Woof!" << std::endl;
    }
};

class Cat : public Animal {
public:
    void make_sound() override {
        std::cout << "Meow!" << std::endl;
    }
};

See how simple it is to create base classes and inherit them? And isn't it delightful to watch your executable size balloon as you fill it with virtual functions?

But wait, there's more! Enter templates, C++'s gift to the world of generic programming. The nightmares of writing separate functions for each data type are over, thanks to C++'s obscure angle-bracket syntax:

template <typename T>
T add(const T& a, const T& b) {
    return a + b;
}

An elegant solution to a seemingly insignificant problem, indeed. Many a programmer will spend countless hours deciphering cryptic template errors or debugging template specialization gone awry. A small price to pay for such power, no?

But fear not! C++ doesn't stop at merely providing convoluted syntax and mind-bending concepts. With great power comes great responsibility, and that responsibility takes the form of manual memory management. Grab your trusty pointers, and delve into the arcane world of dynamic allocation and deallocation with new and delete, as you inevitably chase elusive memory leaks in your code.

int* create_array(int size) {
    int* array = new int[size];
    return array;
}

void destroy_array(int* array) {
    delete[] array;
}

Finally, let's touch upon C++'s impressive collection of libraries, from the venerable C standard library to the behemoth that is the C++ Standard Library. And, of course, the Boost libraries—because who wouldn't want to include an entire family of libraries in their project just to use a few lines of code? And if you like threading, good luck with the various platform-specific libraries and APIs!

In conclusion, C++ is a language that will test your wits and patience with its labyrinthine syntax and unending list of features. Some might argue that its power and flexibility are worth the learning curve, but we all know that deep down, every C++ developer dreams of a simpler life with Python or JavaScript. Until that day comes, let us smile through gritted teeth and rejoice in the eternal love-hate relationship that is C++.

Grok.foo is a collection of articles on a variety of technology and programming articles assembled by James Padolsey. Enjoy! And please share! And if you feel like you can donate here so I can create more free content for you.