8be487b3fae50b57e1d233a5f8205ddba9d643cd
[kill9.git] / harmful / software / c++ / index.md
1 <style>
2 pre {
3 overflow:auto;
4 table-layout:fixed;
5 width: 75%;
6 max-width:75%;
7 }
8 </style>
9 # C++ sucks
10
11 >C makes it easy to shoot yourself in the foot; C++ makes it harder,
12 >but when you do, it blows away your whole leg.
13
14 --- Bjarne Stroustrup
15
16 >If you like C++, you don’t know C++. There’s a mutual exclusion going
17 >on here, and I’ve yet to see a counter-example other than possibly a
18 >few of the members of the standards committee.
19
20 --- ssylvan
21
22 >I think maybe the guy who invented C++ doesn’t know the difference
23 >between increment and excrement.
24
25 --- smcameron
26
27 ## Object oriented
28
29 So automatically sucks.
30
31 ## Ugly syntax
32
33 The only non-ugly part of C++ is the headers' name: iostream, cmath,
34 ctime, thread, etc. Unlike in C, where they are ugly as shit: stdio.h,
35 unistd.h, stdlib.h, pthreads.h...
36
37 But the rest of the language has the ugliest syntax that I've ever seen:
38
39 Hello world in C++
40
41 ~~~
42 #include <iostream>
43
44 int main(void)
45 {
46 std::cout << "Hello world!" << std:endl;
47 }
48 ~~~
49
50 ### What the FUCK was Bjarne smoking when he did this:
51
52 In C:
53
54 `printf("0x%04x\n", 0x424);`
55
56 In C++:
57
58 `std::cout << "0x" << std::hex << std::setfill('0') << std::setw(4) << 0x424 << std::endl;`
59
60 Now what the fuck is std:: and why do I have to write << just to print some shit.
61
62 Note: you can skip the std:: part with ```using namespace std``` but this is a [bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)
63
64 ## Error messages
65
66 in the following code:
67
68
69
70 ~~~
71 #include <iostream>
72
73 int main(void)
74 {
75 std::cout < "Hello world!" < std:endl;
76 }
77 ~~~
78
79 You get this error message:
80
81 <p class="code">
82
83 ~~~
84 a.cc: In function ‘int main()’:
85 a.cc:5:13: error: no match for ‘operator<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const char [13]’)
86 5 | std::cout < "Hello world!" < std:endl;
87 | ~~~~~~~~~ ^ ~~~~~~~~~~~~~~
88 | | |
89 | | const char [13]
90 | std::ostream {aka std::basic_ostream<char>}
91 In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:64,
92 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
93 from /usr/local/include/c++/10.0.1/ios:40,
94 from /usr/local/include/c++/10.0.1/ostream:38,
95 from /usr/local/include/c++/10.0.1/iostream:39,
96 from a.cc:1:
97 /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>&)’
98 489 | operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
99 | ^~~~~~~~
100 /usr/local/include/c++/10.0.1/bits/stl_pair.h:489:5: note: template argument deduction/substitution failed:
101 a.cc:5:15: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::pair<_T1, _T2>’
102 5 | std::cout < "Hello world!" < std:endl;
103 | ^~~~~~~~~~~~~~
104 In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:67,
105 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
106 from /usr/local/include/c++/10.0.1/ios:40,
107 from /usr/local/include/c++/10.0.1/ostream:38,
108 from /usr/local/include/c++/10.0.1/iostream:39,
109 from a.cc:1:
110 /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>&)’
111 366 | operator<(const reverse_iterator<_Iterator>& __x,
112 | ^~~~~~~~
113 /usr/local/include/c++/10.0.1/bits/stl_iterator.h:366:5: note: template argument deduction/substitution failed:
114 a.cc:5:15: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::reverse_iterator<_Iterator>’
115 5 | std::cout < "Hello world!" < std:endl;
116 | ^~~~~~~~~~~~~~
117 In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:67,
118 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
119 from /usr/local/include/c++/10.0.1/ios:40,
120 from /usr/local/include/c++/10.0.1/ostream:38,
121 from /usr/local/include/c++/10.0.1/iostream:39,
122 from a.cc:1:
123 /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>&)’
124 404 | operator<(const reverse_iterator<_IteratorL>& __x,
125 | ^~~~~~~~
126 /usr/local/include/c++/10.0.1/bits/stl_iterator.h:404:5: note: template argument deduction/substitution failed:
127 a.cc:5:15: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::reverse_iterator<_Iterator>’
128 5 | std::cout < "Hello world!" < std:endl;
129 | ^~~~~~~~~~~~~~
130 In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:67,
131 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
132 from /usr/local/include/c++/10.0.1/ios:40,
133 from /usr/local/include/c++/10.0.1/ostream:38,
134 from /usr/local/include/c++/10.0.1/iostream:39,
135 from a.cc:1:
136 /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>&)’
137 1451 | operator<(const move_iterator<_IteratorL>& __x,
138 | ^~~~~~~~
139 /usr/local/include/c++/10.0.1/bits/stl_iterator.h:1451:5: note: template argument deduction/substitution failed:
140 a.cc:5:15: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::move_iterator<_IteratorL>’
141 5 | std::cout < "Hello world!" < std:endl;
142 | ^~~~~~~~~~~~~~
143 In file included from /usr/local/include/c++/10.0.1/bits/stl_algobase.h:67,
144 from /usr/local/include/c++/10.0.1/bits/char_traits.h:39,
145 from /usr/local/include/c++/10.0.1/ios:40,
146 from /usr/local/include/c++/10.0.1/ostream:38,
147 from /usr/local/include/c++/10.0.1/iostream:39,
148 from a.cc:1:
149 /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>&)’
150 1507 | operator<(const move_iterator<_Iterator>& __x,
151 | ^~~~~~~~
152 /usr/local/include/c++/10.0.1/bits/stl_iterator.h:1507:5: note: template argument deduction/substitution failed:
153 a.cc:5:15: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::move_iterator<_IteratorL>’
154 5 | std::cout < "Hello world!" < std:endl;
155 | ^~~~~~~~~~~~~~
156 In file included from /usr/local/include/c++/10.0.1/string:55,
157 from /usr/local/include/c++/10.0.1/bits/locale_classes.h:40,
158 from /usr/local/include/c++/10.0.1/bits/ios_base.h:41,
159 from /usr/local/include/c++/10.0.1/ios:42,
160 from /usr/local/include/c++/10.0.1/ostream:38,
161 from /usr/local/include/c++/10.0.1/iostream:39,
162 from a.cc:1:
163 /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>&)’
164 6267 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
165 | ^~~~~~~~
166 /usr/local/include/c++/10.0.1/bits/basic_string.h:6267:5: note: template argument deduction/substitution failed:
167 a.cc:5:15: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
168 5 | std::cout < "Hello world!" < std:endl;
169 | ^~~~~~~~~~~~~~
170 In file included from /usr/local/include/c++/10.0.1/string:55,
171 from /usr/local/include/c++/10.0.1/bits/locale_classes.h:40,
172 from /usr/local/include/c++/10.0.1/bits/ios_base.h:41,
173 from /usr/local/include/c++/10.0.1/ios:42,
174 from /usr/local/include/c++/10.0.1/ostream:38,
175 from /usr/local/include/c++/10.0.1/iostream:39,
176 from a.cc:1:
177 /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*)’
178 6280 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
179 | ^~~~~~~~
180 /usr/local/include/c++/10.0.1/bits/basic_string.h:6280:5: note: template argument deduction/substitution failed:
181 a.cc:5:15: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
182 5 | std::cout < "Hello world!" < std:endl;
183 | ^~~~~~~~~~~~~~
184 In file included from /usr/local/include/c++/10.0.1/string:55,
185 from /usr/local/include/c++/10.0.1/bits/locale_classes.h:40,
186 from /usr/local/include/c++/10.0.1/bits/ios_base.h:41,
187 from /usr/local/include/c++/10.0.1/ios:42,
188 from /usr/local/include/c++/10.0.1/ostream:38,
189 from /usr/local/include/c++/10.0.1/iostream:39,
190 from a.cc:1:
191 /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>&)’
192 6292 | operator<(const _CharT* __lhs,
193 | ^~~~~~~~
194 /usr/local/include/c++/10.0.1/bits/basic_string.h:6292:5: note: template argument deduction/substitution failed:
195 a.cc:5:15: note: mismatched types ‘const _CharT*’ and ‘std::basic_ostream<char>’
196 5 | std::cout < "Hello world!" < std:endl;
197 | ^~~~~~~~~~~~~~
198 In file included from /usr/local/include/c++/10.0.1/bits/ios_base.h:46,
199 from /usr/local/include/c++/10.0.1/ios:42,
200 from /usr/local/include/c++/10.0.1/ostream:38,
201 from /usr/local/include/c++/10.0.1/iostream:39,
202 from a.cc:1:
203 /usr/local/include/c++/10.0.1/system_error:252:3: note: candidate: ‘bool std::operator<(const std::error_code&, const std::error_code&)’
204 252 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept
205 | ^~~~~~~~
206 /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&’
207 252 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept
208 | ~~~~~~~~~~~~~~~~~~^~~~~
209 /usr/local/include/c++/10.0.1/system_error:379:3: note: candidate: ‘bool std::operator<(const std::error_condition&, const std::error_condition&)’
210 379 | operator<(const error_condition& __lhs,
211 | ^~~~~~~~
212 /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&’
213 379 | operator<(const error_condition& __lhs,
214 | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
215 a.cc:5:35: error: found ‘:’ in nested-name-specifier, expected ‘::’
216 5 | std::cout < "Hello world!" < std:endl;
217 | ^
218 | ::
219 ~~~
220 </p>
221 (In GCC)
222
223 ## Compatibilty
224
225 C++ is "compatible" with C
226
227 But this can break most C programs