Added a rocks thing about c++

This commit is contained in:
qorg11 2021-12-09 17:32:50 +01:00
parent 6dd1e6415b
commit 7cc4619b13
No known key found for this signature in database
GPG Key ID: 343FC20A4ACA62B9
1 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,119 @@
<script src="/run_prettify.js"></script>
<noscript><p>JavaScript disabled! Congrats! But keep in mind that there won't be syntax highligthing</p></noscript>
# C++ has good stuff
As stated in the [C++ harmful section](/harmful/software/c++) C++ is a
mostly bad programming language. But the great things about C++ are
**REALLY** great and worth mentioning:
## Concurrency
### Threads API
Let's be honest, no matter how much you love C, you have to recognize
that the pthreads API are garbage. But C++'s threads API is much
better:
<pre class="prettyprint">
#include <iostream>
#include <thread>
#include <mutex>
int j = 10;
std::mutex j_lock;
int
counter_1()
{
for(int i = 0; i <=10000; i++) {
j_lock.lock(); /* critical section begins here */
j++;
j_lock.unlock(); /* critical sectrion ends here */
}
return 0;
}
int
counter_2()
{
for(int i = 0; i <=10000; i++) {
j_lock.lock(); /* critical section begins here */
j--;
j_lock.unlock(); /* critical section begins here */
}
return 0;
}
int
main(void)
{
std::thread t1(counter_1); /* This starts the thread automatically */
std::thread t2(counter_2);
t1.join(); /* wait for the thing to finish */
t2.join();
std::cout << j << std::endl;
}
</pre>
### Async functions and stuff
As far as I know, C doesn't have this (well, it does but as state
above you'll have to have a lot of patience to work with
pthreads). but C++ does.
<pre class="prettyprint">
#include <iostream>
#include <future>
int
do_stuff()
{
/* this function takes its time to be completed */
return 0; /* Everything's alright */
}
int
main()
{
std::future<int> future = std::async(do_stuff);
std::cout << "do_stuff() is being executed in the background" << std::endl;
std::cout << "The return value of do_stuff is: " << future.get() << std::endl;
return 0;
}
</pre>
## C compatibility
Yes, you can be a C++ wizard and all you want, but saying "C++ has a
good syntax" is like saying "I love my job" -- You're only saying that
to convince yourself to get up from bed every morning. C++ is
backwards compatible with C. Meaning that you can call C functions
from C++. So you can do all your programming in C and only use the
cool C++ features.
## Things C doesn't have
You can write literally anything in C, but implementing a linked list
gets boring at the third time you do it (altough you can use things
like libulz) . So C++ has all the abstraction a normal human could
want. Such as vectors (dynamics arrays, in C you would have to do
really weird shit with `realloc()` or implementing a linked list),
actual strings (C strings are very based, and a literal chad move
though).
## Creating libs is easy
Due to portability, there are things that C++ can't do by itself, such
as creating UNIX sockets, which as far as i know, sockets in C++ must
use the C sockets API. As stated before, calling C functions from C++
is very trivial, and you just have to call them. Converting C++ data
types to C is trivial, because they're either the same datatype or
there's a method for converting it to the C data type (`c_str` for the
case of `std::string`, which will convert it to `char *` datatype). So
you can easily create C++-styles functions from C functions.
Despite that, don't write complex libs in C++, as C users would want
to use it as well, looking at you, qt.