C++ sucks

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.

— Bjarne Stroustrup

If you like C++, you don’t know C++. There’s a mutual exclusion going on here, and I’ve yet to see a counter-example other than possibly a few of the members of the standards committee.

— ssylvan

I think maybe the guy who invented C++ doesn’t know the difference between increment and excrement.

— smcameron

I invented the term object oriented, and I can tell you that C++ wasn’t what I had in mind.

— Alan Kay

A friend said that C++ is a well designed language because it is designed by “A thousand of experts in Sillicon Valley”. She said this as a reason to use C++, but we all know that’s a reason not to use it.

Object oriented

So automatically sucks.

Ugly syntax

The only non-ugly part of C++ is the headers’ name: iostream, cmath, ctime, thread, etc. Unlike in C, where they are ugly as shit: stdio.h, unistd.h, stdlib.h, pthreads.h…

But the rest of the language has the ugliest syntax that I’ve ever seen:

Hello world in C++

#include <iostream>

int main(void)
{
    std::cout << "Hello world!" << std:endl;
}

What the FUCK was Bjarne smoking when he did this:

In C:

printf("0x%04x\n", 0x424);

In C++:

std::cout << "0x" << std::hex << std::setfill('0') << std::setw(4) << 0x424 << std::endl;

Now what the fuck is std:: and why do I have to write << just to print some shit.

Note: you can skip the std:: part with using namespace std but this is a bad practice

Error messages

in the following code:

#include <iostream>

int main(void)
{
    std::cout < "Hello world!" < std:endl;
}

You get this error message:

a.cc: In function ‘int main()’:
a.cc:5:13: error: no match for ‘operator<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const char [13]’)
    5 |   std::cout < "Hello world!" < std:endl;
      |   ~~~~~~~~~ ^ ~~~~~~~~~~~~~~
      |        |      |
      |        |      const char [13]
      |        std::ostream {aka std::basic_ostream<char>}
In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:64,
                 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
                 from /usr/local/include/c++/10.0.1/ios:40,
                 from /usr/local/include/c++/10.0.1/ostream:38,
                 from /usr/local/include/c++/10.0.1/iostream:39,
                 from a.cc:1:
/usr/local/include/c++/10.0.1/bits/stl_pair.h:489:5: note: candidate: ‘template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)’
  489 |     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/local/include/c++/10.0.1/bits/stl_pair.h:489:5: note:   template argument deduction/substitution failed:
a.cc:5:15: note:   ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::pair<_T1, _T2>’
    5 |   std::cout < "Hello world!" < std:endl;
      |               ^~~~~~~~~~~~~~
In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:67,
                 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
                 from /usr/local/include/c++/10.0.1/ios:40,
                 from /usr/local/include/c++/10.0.1/ostream:38,
                 from /usr/local/include/c++/10.0.1/iostream:39,
                 from a.cc:1:
/usr/local/include/c++/10.0.1/bits/stl_iterator.h:366:5: note: candidate: ‘template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)’
  366 |     operator<(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/local/include/c++/10.0.1/bits/stl_iterator.h:366:5: note:   template argument deduction/substitution failed:
a.cc:5:15: note:   ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::reverse_iterator<_Iterator>’
    5 |   std::cout < "Hello world!" < std:endl;
      |               ^~~~~~~~~~~~~~
In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:67,
                 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
                 from /usr/local/include/c++/10.0.1/ios:40,
                 from /usr/local/include/c++/10.0.1/ostream:38,
                 from /usr/local/include/c++/10.0.1/iostream:39,
                 from a.cc:1:
/usr/local/include/c++/10.0.1/bits/stl_iterator.h:404:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)’
  404 |     operator<(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/local/include/c++/10.0.1/bits/stl_iterator.h:404:5: note:   template argument deduction/substitution failed:
a.cc:5:15: note:   ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::reverse_iterator<_Iterator>’
    5 |   std::cout < "Hello world!" < std:endl;
      |               ^~~~~~~~~~~~~~
In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:67,
                 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
                 from /usr/local/include/c++/10.0.1/ios:40,
                 from /usr/local/include/c++/10.0.1/ostream:38,
                 from /usr/local/include/c++/10.0.1/iostream:39,
                 from a.cc:1:
/usr/local/include/c++/10.0.1/bits/stl_iterator.h:1451:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)’
 1451 |     operator<(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/local/include/c++/10.0.1/bits/stl_iterator.h:1451:5: note:   template argument deduction/substitution failed:
a.cc:5:15: note:   ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::move_iterator<_IteratorL>’
    5 |   std::cout < "Hello world!" < std:endl;
      |               ^~~~~~~~~~~~~~
In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:67,
                 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
                 from /usr/local/include/c++/10.0.1/ios:40,
                 from /usr/local/include/c++/10.0.1/ostream:38,
                 from /usr/local/include/c++/10.0.1/iostream:39,
                 from a.cc:1:
/usr/local/include/c++/10.0.1/bits/stl_iterator.h:1507:5: note: candidate: ‘template<class _Iterator> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)’
 1507 |     operator<(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/local/include/c++/10.0.1/bits/stl_iterator.h:1507:5: note:   template argument deduction/substitution failed:
a.cc:5:15: note:   ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::move_iterator<_IteratorL>’
    5 |   std::cout < "Hello world!" < std:endl;
      |               ^~~~~~~~~~~~~~
In file included from /usr/local/include/c++/10.0.1/string:55,
                 from /usr/local/include/c++/10.0.1/bits/locale_classes.h:40,
                 from /usr/local/include/c++/10.0.1/bits/ios_base.h:41,
                 from /usr/local/include/c++/10.0.1/ios:42,
                 from /usr/local/include/c++/10.0.1/ostream:38,
                 from /usr/local/include/c++/10.0.1/iostream:39,
                 from a.cc:1:
/usr/local/include/c++/10.0.1/bits/basic_string.h:6267:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
 6267 |     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/local/include/c++/10.0.1/bits/basic_string.h:6267:5: note:   template argument deduction/substitution failed:
a.cc:5:15: note:   ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
    5 |   std::cout < "Hello world!" < std:endl;
      |               ^~~~~~~~~~~~~~
In file included from /usr/local/include/c++/10.0.1/string:55,
                 from /usr/local/include/c++/10.0.1/bits/locale_classes.h:40,
                 from /usr/local/include/c++/10.0.1/bits/ios_base.h:41,
                 from /usr/local/include/c++/10.0.1/ios:42,
                 from /usr/local/include/c++/10.0.1/ostream:38,
                 from /usr/local/include/c++/10.0.1/iostream:39,
                 from a.cc:1:
/usr/local/include/c++/10.0.1/bits/basic_string.h:6280:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)’
 6280 |     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/local/include/c++/10.0.1/bits/basic_string.h:6280:5: note:   template argument deduction/substitution failed:
a.cc:5:15: note:   ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
    5 |   std::cout < "Hello world!" < std:endl;
      |               ^~~~~~~~~~~~~~
In file included from /usr/local/include/c++/10.0.1/string:55,
                 from /usr/local/include/c++/10.0.1/bits/locale_classes.h:40,
                 from /usr/local/include/c++/10.0.1/bits/ios_base.h:41,
                 from /usr/local/include/c++/10.0.1/ios:42,
                 from /usr/local/include/c++/10.0.1/ostream:38,
                 from /usr/local/include/c++/10.0.1/iostream:39,
                 from a.cc:1:
/usr/local/include/c++/10.0.1/bits/basic_string.h:6292:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
 6292 |     operator<(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/local/include/c++/10.0.1/bits/basic_string.h:6292:5: note:   template argument deduction/substitution failed:
a.cc:5:15: note:   mismatched types ‘const _CharT*’ and ‘std::basic_ostream<char>’
    5 |   std::cout < "Hello world!" < std:endl;
      |               ^~~~~~~~~~~~~~
In file included from /usr/local/include/c++/10.0.1/bits/ios_base.h:46,
                 from /usr/local/include/c++/10.0.1/ios:42,
                 from /usr/local/include/c++/10.0.1/ostream:38,
                 from /usr/local/include/c++/10.0.1/iostream:39,
                 from a.cc:1:
/usr/local/include/c++/10.0.1/system_error:252:3: note: candidate: ‘bool std::operator<(const std::error_code&, const std::error_code&)’
  252 |   operator<(const error_code& __lhs, const error_code& __rhs) noexcept
      |   ^~~~~~~~
/usr/local/include/c++/10.0.1/system_error:252:31: note:   no known conversion for argument 1 from ‘std::ostream’ {aka ‘std::basic_ostream<char>’} to ‘const std::error_code&’
  252 |   operator<(const error_code& __lhs, const error_code& __rhs) noexcept
      |             ~~~~~~~~~~~~~~~~~~^~~~~
/usr/local/include/c++/10.0.1/system_error:379:3: note: candidate: ‘bool std::operator<(const std::error_condition&, const std::error_condition&)’
  379 |   operator<(const error_condition& __lhs,
      |   ^~~~~~~~
/usr/local/include/c++/10.0.1/system_error:379:36: note:   no known conversion for argument 1 from ‘std::ostream’ {aka ‘std::basic_ostream<char>’} to ‘const std::error_condition&’
  379 |   operator<(const error_condition& __lhs,
      |             ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:5:35: error: found ‘:’ in nested-name-specifier, expected ‘::’
    5 |   std::cout < "Hello world!" < std:endl;
      |                                   ^
      |                                   ::

(In GCC)

Objects are stupid

std::string spurdo = "sparde"

where spurdo is an object

spurdo.c_str() // converts the string to a C '\0' terminated string.

In C, strings are an array of chars, in C++, they’re an object, with methods.

Compatibilty

C++ is “compatible” with C

But this can break most C programs

A sad story about c++ executables

<lucy>  try to strip the executable
<lucy>  Did it change anything?
<qorg11>  strip?
<lucy>  "strip a.out"
<lucy>  It removes unused stuff
<lucy>  like debug symbols and similar
<qorg11>  qorg@satania ~> md5sum a.out       
 cd1cc93ac2ea0c8129042e96ae7d65e7  a.out
 qorg@satania ~> strip a.out
 qorg@satania ~> md5sum a.out
 e6e6e1ea132edb67c6005e703ebe7c81  a.out
<qorg11>  yeah?
<lucy>  and now compare the assembly code
<qorg11>  how
<qorg11>  do i disassembly it or what
<lucy>  how did you do it before?
<qorg11>  g++ -S
<qorg11>  lol
<lucy>  oof
<lucy>  maybe just compare the filesize
<qorg11>  qorg@satania ~> gcc mtx.c -lcurses -lpthread 
 qorg@satania ~> du -h a.out                                                       
 24K    a.out
<qorg11>  qorg@satania ~> g++ mtx.cc -lcurses -pthread 
 qorg@satania ~> du -h a.out
 40K    a.out
<qorg11>  now with -Os
<qorg11>  qorg@satania ~> gcc mtx.c -lcurses -lpthread -Os
 qorg@satania ~> du -h a.out
 24K    a.out
<qorg11>  lol
<qorg11>  qorg@satania ~> g++ mtx.cc -lcurses -pthread -Os
 qorg@satania ~> du -h a.out
 24K    a.out
<qorg11>  congrats c++!
<qorg11>  exact filesize: 23488 (according to stat)
<lucy>  now the stripped versions
<qorg11>  stat'd c version: 21520
<qorg11>  stripped c++ 14680
<qorg11>  stripped c: 14472
<lucy>  a considerably smaller difference
<lucy>  So c++ is bloated up with debug symbols, awesome
<qorg11>  lol
<qorg11>  can i take a log of this and put it in kill-9.xyz/harmful/software/c++?