64
loading...
This website collects cookies to deliver better user experience
malloc
and free
. They are hard to work with, lead to many error conditions that need to manually be checked for, and is overall a nightmare. When people hear that C++ also has great performance, people assume that it is by dabbling into all the specifics of memory allocation much like in C so they conclude that it would also be a nightmare. This is categorically false.std::shared_ptr
works by wrapping a regular object in a copy-able and movable object with a reference counting mechanism. Thus, when no code is referencing a shared_ptr
, it is safely destructed and freed like in most languages. The simplest way to construct a shared pointer is as follows:std::shared_ptr cat(new Pet("cat"));
// or
std::shared_ptr cat = std::make_shared<Pet>("cat");
unique_ptr
, but more on that later.auto square = [=](int x) { return x * x; };
const
if they don't modify the class' state. These methods can then also be called on constant instances of the class. Here's an example:class Greeting {
public:
Greeting(std::string greeting) : greeting_(greeting) {}
std::string get_greeting() const {
return greeting_;
}
std::string set_greeting(std::string new_) {
greeting_ = new_;
}
private:
std::string greeting_;
};
const Greeting g("hello");
g.get_greeting(); // returns "hello"
g.set_greeting("hi"); // does not compile
const
pointer to a non-const
object, you may not modify the pointer but can modify the object pointed to by the pointer. However, these problems can typically be avoided by typing the pointer correctly (i.e. making it a const
pointer to a const
object). const
-ness is a big plus but there's several other things that C++ allow you to do that prevent production bugs from occurring in larger projects. It allows you to configure move/copy/delete semantics for the classes you design if you need. It allows you to pass things by value and advanced features like multiple inheritance. All these things make C++ less restrictive. numpy
that they just collectively all decided to import it as np
, typing more than 2 letters does feel verbose. But modern C++ is a lot less verbose than it used to be! For example, type inference like Go is available in C++ with the introduction of the auto
keyword.auto x = 1;
x = 2; // works
x = "abc"; // compilation error
auto
in return types:auto func(int x) { return x * x; }
for (auto& [key, value]: hashmap) {...}
auto
does not mean that the types are dynamic -- they are still inferred at compile time and once assigned, they cannot be changed. This is probably for the best. In practice for large codebases, it arguably also helps readability to specifically type out the full type instead of using auto
. However, the feature is there if you need it.using tf = tensorflow;
// Now you can use tf::Example instead of tensorflow::Example.
CMake
files, although in many ways those are not much different from the gradle
or package.json
files in other languages. However, again Google's open source build tool Bazel makes it extremely easy to work with even with cross-language builds. It takes some getting used to, but provides really quick builds and in general has a very good developer experience.defer
functionality in Go.shared_ptr
, you can also have a unique_ptr
that ensures that only one object can own a resource. Having one owner for data makes it easy to organize and reason about large projects. In fact, while shared_ptr
most closely mimics Java and other OOP languages, you're generally better off using a unique_ptr
. This also adds to the safety of the language.const A&
) which does not lead to a copy and keeps your object safe from being mutated unintentionally. Those are strong guarantees to have and that makes your code just so much easier to reason about. In typescript, for example, you cannot reassign a const
object but you can mutate it. Why even -- just uggh.pandas
for that). There are some things that C++ is great for and there are other things that it's just not suitable for. Ultimately, you still have to choose the right tool for whatever job you're trying to accomplish. Having said that, hopefully this article made C++ a bit more attractive in your eyes than you're used to seeing it as.