]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/doc/html/manual/bk01apas03.html
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / doc / html / manual / bk01apas03.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Coding Style</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /><meta name="keywords" content="&#10;      ISO C++&#10;    , &#10;      library&#10;    " /><link rel="start" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="appendix_contributing.html" title="Appendix A. Contributing" /><link rel="prev" href="bk01apas02.html" title="Directory Layout and Source Conventions" /><link rel="next" href="bk01apas04.html" title="Documentation Style" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Coding Style</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="bk01apas02.html">Prev</a> </td><th width="60%" align="center">Appendix A. Contributing</th><td width="20%" align="right"> <a accesskey="n" href="bk01apas04.html">Next</a></td></tr></table><hr /></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="contrib.coding_style"></a>Coding Style</h2></div></div></div><p>
4   </p><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="coding_style.bad_identifiers"></a>Bad Identifiers</h3></div></div></div><p>
5       Identifiers that conflict and should be avoided.
6     </p><div class="literallayout"><p><br />
7       This is the list of names “<span class="quote">reserved to the<br />
8       implementation</span>” that have been claimed by certain<br />
9       compilers and system headers of interest, and should not be used<br />
10       in the library. It will grow, of course.  We generally are<br />
11       interested in names that are not all-caps, except for those like<br />
12       "_T"<br />
13 <br />
14       For Solaris:<br />
15       _B<br />
16       _C<br />
17       _L<br />
18       _N<br />
19       _P<br />
20       _S<br />
21       _U<br />
22       _X<br />
23       _E1<br />
24       ..<br />
25       _E24<br />
26 <br />
27       Irix adds:<br />
28       _A<br />
29       _G<br />
30 <br />
31       MS adds:<br />
32       _T<br />
33 <br />
34       BSD adds:<br />
35       __used<br />
36       __unused<br />
37       __inline<br />
38       _Complex<br />
39       __istype<br />
40       __maskrune<br />
41       __tolower<br />
42       __toupper<br />
43       __wchar_t<br />
44       __wint_t<br />
45       _res<br />
46       _res_ext<br />
47       __tg_*<br />
48 <br />
49       For GCC:<br />
50 <br />
51       [Note that this list is out of date. It applies to the old<br />
52       name-mangling; in G++ 3.0 and higher a different name-mangling is<br />
53       used. In addition, many of the bugs relating to G++ interpreting<br />
54       these names as operators have been fixed.]<br />
55 <br />
56       The full set of __* identifiers (combined from gcc/cp/lex.c and<br />
57       gcc/cplus-dem.c) that are either old or new, but are definitely <br />
58       recognized by the demangler, is:<br />
59 <br />
60       __aa<br />
61       __aad<br />
62       __ad<br />
63       __addr<br />
64       __adv<br />
65       __aer<br />
66       __als<br />
67       __alshift<br />
68       __amd<br />
69       __ami<br />
70       __aml<br />
71       __amu<br />
72       __aor<br />
73       __apl<br />
74       __array<br />
75       __ars<br />
76       __arshift<br />
77       __as<br />
78       __bit_and<br />
79       __bit_ior<br />
80       __bit_not<br />
81       __bit_xor<br />
82       __call<br />
83       __cl<br />
84       __cm<br />
85       __cn<br />
86       __co<br />
87       __component<br />
88       __compound<br />
89       __cond<br />
90       __convert<br />
91       __delete<br />
92       __dl<br />
93       __dv<br />
94       __eq<br />
95       __er<br />
96       __ge<br />
97       __gt<br />
98       __indirect<br />
99       __le<br />
100       __ls<br />
101       __lt<br />
102       __max<br />
103       __md<br />
104       __method_call<br />
105       __mi<br />
106       __min<br />
107       __minus<br />
108       __ml<br />
109       __mm<br />
110       __mn<br />
111       __mult<br />
112       __mx<br />
113       __ne<br />
114       __negate<br />
115       __new<br />
116       __nop<br />
117       __nt<br />
118       __nw<br />
119       __oo<br />
120       __op<br />
121       __or<br />
122       __pl<br />
123       __plus<br />
124       __postdecrement<br />
125       __postincrement<br />
126       __pp<br />
127       __pt<br />
128       __rf<br />
129       __rm<br />
130       __rs<br />
131       __sz<br />
132       __trunc_div<br />
133       __trunc_mod<br />
134       __truth_andif<br />
135       __truth_not<br />
136       __truth_orif<br />
137       __vc<br />
138       __vd<br />
139       __vn<br />
140 <br />
141       SGI badnames:<br />
142       __builtin_alloca<br />
143       __builtin_fsqrt<br />
144       __builtin_sqrt<br />
145       __builtin_fabs<br />
146       __builtin_dabs<br />
147       __builtin_cast_f2i<br />
148       __builtin_cast_i2f<br />
149       __builtin_cast_d2ll<br />
150       __builtin_cast_ll2d<br />
151       __builtin_copy_dhi2i<br />
152       __builtin_copy_i2dhi<br />
153       __builtin_copy_dlo2i<br />
154       __builtin_copy_i2dlo<br />
155       __add_and_fetch<br />
156       __sub_and_fetch<br />
157       __or_and_fetch<br />
158       __xor_and_fetch<br />
159       __and_and_fetch<br />
160       __nand_and_fetch<br />
161       __mpy_and_fetch<br />
162       __min_and_fetch<br />
163       __max_and_fetch<br />
164       __fetch_and_add<br />
165       __fetch_and_sub<br />
166       __fetch_and_or<br />
167       __fetch_and_xor<br />
168       __fetch_and_and<br />
169       __fetch_and_nand<br />
170       __fetch_and_mpy<br />
171       __fetch_and_min<br />
172       __fetch_and_max<br />
173       __lock_test_and_set<br />
174       __lock_release<br />
175       __lock_acquire<br />
176       __compare_and_swap<br />
177       __synchronize<br />
178       __high_multiply<br />
179       __unix<br />
180       __sgi<br />
181       __linux__<br />
182       __i386__<br />
183       __i486__<br />
184       __cplusplus<br />
185       __embedded_cplusplus<br />
186       // long double conversion members mangled as __opr<br />
187       // http://gcc.gnu.org/ml/libstdc++/1999-q4/msg00060.html<br />
188       _opr<br />
189     </p></div></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="coding_style.example"></a>By Example</h3></div></div></div><div class="literallayout"><p><br />
190       This library is written to appropriate C++ coding standards. As such,<br />
191       it is intended to precede the recommendations of the GNU Coding<br />
192       Standard, which can be referenced in full here:<br />
193 <br />
194       http://www.gnu.org/prep/standards/standards.html#Formatting<br />
195 <br />
196       The rest of this is also interesting reading, but skip the "Design<br />
197       Advice" part.<br />
198 <br />
199       The GCC coding conventions are here, and are also useful:<br />
200       http://gcc.gnu.org/codingconventions.html<br />
201 <br />
202       In addition, because it doesn't seem to be stated explicitly anywhere<br />
203       else, there is an 80 column source limit.<br />
204 <br />
205       ChangeLog entries for member functions should use the<br />
206       classname::member function name syntax as follows:<br />
207 <br />
208       1999-04-15  Dennis Ritchie  &lt;dr@att.com&gt;<br />
209 <br />
210       * src/basic_file.cc (__basic_file::open): Fix thinko in<br />
211       _G_HAVE_IO_FILE_OPEN bits.<br />
212 <br />
213       Notable areas of divergence from what may be previous local practice<br />
214       (particularly for GNU C) include:<br />
215 <br />
216       01. Pointers and references<br />
217       char* p = "flop";<br />
218       char&amp; c = *p;<br />
219       -NOT-<br />
220       char *p = "flop";  // wrong<br />
221       char &amp;c = *p;      // wrong<br />
222       <br />
223       Reason: In C++, definitions are mixed with executable code. Here,       <br />
224       p is being initialized, not *p. This is near-universal<br />
225       practice among C++ programmers; it is normal for C hackers<br />
226       to switch spontaneously as they gain experience.<br />
227 <br />
228       02. Operator names and parentheses<br />
229       operator==(type)<br />
230       -NOT-<br />
231       operator == (type)  // wrong<br />
232       <br />
233       Reason: The == is part of the function name. Separating<br />
234       it makes the declaration look like an expression. <br />
235 <br />
236       03. Function names and parentheses<br />
237       void mangle()<br />
238       -NOT-<br />
239       void mangle ()  // wrong<br />
240 <br />
241       Reason: no space before parentheses (except after a control-flow<br />
242       keyword) is near-universal practice for C++. It identifies the<br />
243       parentheses as the function-call operator or declarator, as <br />
244       opposed to an expression or other overloaded use of parentheses.<br />
245 <br />
246       04. Template function indentation<br />
247       template&lt;typename T&gt;<br />
248       void <br />
249       template_function(args)<br />
250       { }<br />
251       -NOT-<br />
252       template&lt;class T&gt;<br />
253       void template_function(args) {};<br />
254       <br />
255       Reason: In class definitions, without indentation whitespace is<br />
256       needed both above and below the declaration to distinguish<br />
257       it visually from other members. (Also, re: "typename"<br />
258       rather than "class".)  T often could be int, which is <br />
259       not a class. ("class", here, is an anachronism.)<br />
260 <br />
261       05. Template class indentation<br />
262       template&lt;typename _CharT, typename _Traits&gt;<br />
263       class basic_ios : public ios_base<br />
264       {<br />
265       public:<br />
266       // Types:<br />
267       };<br />
268       -NOT-<br />
269       template&lt;class _CharT, class _Traits&gt;<br />
270       class basic_ios : public ios_base<br />
271       {<br />
272       public:<br />
273       // Types:<br />
274       };<br />
275       -NOT-<br />
276       template&lt;class _CharT, class _Traits&gt;<br />
277       class basic_ios : public ios_base<br />
278       {<br />
279       public:<br />
280       // Types:<br />
281       };<br />
282 <br />
283       06. Enumerators<br />
284       enum<br />
285       {<br />
286       space = _ISspace,<br />
287       print = _ISprint,<br />
288       cntrl = _IScntrl<br />
289       };<br />
290       -NOT-<br />
291       enum { space = _ISspace, print = _ISprint, cntrl = _IScntrl };<br />
292 <br />
293       07. Member initialization lists<br />
294       All one line, separate from class name.<br />
295 <br />
296       gribble::gribble() <br />
297       : _M_private_data(0), _M_more_stuff(0), _M_helper(0);<br />
298       { }<br />
299       -NOT-<br />
300       gribble::gribble() : _M_private_data(0), _M_more_stuff(0), _M_helper(0);<br />
301       { }<br />
302 <br />
303       08. Try/Catch blocks<br />
304       try <br />
305       {<br />
306       //<br />
307       }   <br />
308       catch (...)<br />
309       {<br />
310       //<br />
311       }   <br />
312       -NOT-<br />
313       try {<br />
314       // <br />
315       } catch(...) { <br />
316       //<br />
317       }<br />
318 <br />
319       09. Member functions declarations and definitions<br />
320       Keywords such as extern, static, export, explicit, inline, etc<br />
321       go on the line above the function name. Thus<br />
322 <br />
323       virtual int   <br />
324       foo()<br />
325       -NOT-<br />
326       virtual int foo()<br />
327 <br />
328       Reason: GNU coding conventions dictate return types for functions<br />
329       are on a separate line than the function name and parameter list<br />
330       for definitions. For C++, where we have member functions that can<br />
331       be either inline definitions or declarations, keeping to this<br />
332       standard allows all member function names for a given class to be<br />
333       aligned to the same margin, increasing readability.<br />
334 <br />
335 <br />
336       10. Invocation of member functions with "this-&gt;"<br />
337       For non-uglified names, use this-&gt;name to call the function.<br />
338 <br />
339       this-&gt;sync()<br />
340       -NOT-<br />
341       sync()<br />
342 <br />
343       Reason: Koenig lookup.<br />
344 <br />
345       11. Namespaces<br />
346       namespace std<br />
347       {<br />
348       blah blah blah;<br />
349       } // namespace std<br />
350 <br />
351       -NOT-<br />
352 <br />
353       namespace std {<br />
354       blah blah blah;<br />
355       } // namespace std<br />
356 <br />
357       12. Spacing under protected and private in class declarations:<br />
358       space above, none below<br />
359       i.e.<br />
360 <br />
361       public:<br />
362       int foo;<br />
363 <br />
364       -NOT-<br />
365       public:<br />
366       <br />
367       int foo;<br />
368 <br />
369       13. Spacing WRT return statements.<br />
370       no extra spacing before returns, no parenthesis<br />
371       i.e.<br />
372 <br />
373       }<br />
374       return __ret;<br />
375 <br />
376       -NOT-<br />
377       }<br />
378 <br />
379       return __ret;<br />
380 <br />
381       -NOT-<br />
382 <br />
383       }<br />
384       return (__ret);<br />
385 <br />
386 <br />
387       14. Location of global variables.<br />
388       All global variables of class type, whether in the "user visible"<br />
389       space (e.g., cin) or the implementation namespace, must be defined<br />
390       as a character array with the appropriate alignment and then later<br />
391       re-initialized to the correct value.<br />
392 <br />
393       This is due to startup issues on certain platforms, such as AIX.<br />
394       For more explanation and examples, see src/globals.cc. All such<br />
395       variables should be contained in that file, for simplicity.<br />
396 <br />
397       15. Exception abstractions<br />
398       Use the exception abstractions found in functexcept.h, which allow<br />
399       C++ programmers to use this library with -fno-exceptions. (Even if<br />
400       that is rarely advisable, it's a necessary evil for backwards<br />
401       compatibility.)<br />
402 <br />
403       16. Exception error messages<br />
404       All start with the name of the function where the exception is<br />
405       thrown, and then (optional) descriptive text is added. Example:<br />
406 <br />
407       __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));<br />
408 <br />
409       Reason: The verbose terminate handler prints out exception::what(),<br />
410       as well as the typeinfo for the thrown exception. As this is the<br />
411       default terminate handler, by putting location info into the<br />
412       exception string, a very useful error message is printed out for<br />
413       uncaught exceptions. So useful, in fact, that non-programmers can<br />
414       give useful error messages, and programmers can intelligently<br />
415       speculate what went wrong without even using a debugger.<br />
416 <br />
417       17. The doxygen style guide to comments is a separate document,<br />
418       see index.<br />
419 <br />
420       The library currently has a mixture of GNU-C and modern C++ coding<br />
421       styles. The GNU C usages will be combed out gradually.<br />
422 <br />
423       Name patterns:<br />
424 <br />
425       For nonstandard names appearing in Standard headers, we are constrained <br />
426       to use names that begin with underscores. This is called "uglification".<br />
427       The convention is:<br />
428 <br />
429       Local and argument names:  __[a-z].*<br />
430 <br />
431       Examples:  __count  __ix  __s1  <br />
432 <br />
433       Type names and template formal-argument names: _[A-Z][^_].*<br />
434 <br />
435       Examples:  _Helper  _CharT  _N <br />
436 <br />
437       Member data and function names: _M_.*<br />
438 <br />
439       Examples:  _M_num_elements  _M_initialize ()<br />
440 <br />
441       Static data members, constants, and enumerations: _S_.*<br />
442 <br />
443       Examples: _S_max_elements  _S_default_value<br />
444 <br />
445       Don't use names in the same scope that differ only in the prefix, <br />
446       e.g. _S_top and _M_top. See BADNAMES for a list of forbidden names.<br />
447       (The most tempting of these seem to be and "_T" and "__sz".)<br />
448 <br />
449       Names must never have "__" internally; it would confuse name<br />
450       unmanglers on some targets. Also, never use "__[0-9]", same reason.<br />
451 <br />
452       --------------------------<br />
453 <br />
454       [BY EXAMPLE]<br />
455       <br />
456       #ifndef  _HEADER_<br />
457       #define  _HEADER_ 1<br />
458 <br />
459       namespace std<br />
460       {<br />
461       class gribble<br />
462       {<br />
463       public:<br />
464       gribble() throw();<br />
465 <br />
466       gribble(const gribble&amp;);<br />
467 <br />
468       explicit <br />
469       gribble(int __howmany);<br />
470 <br />
471       gribble&amp; <br />
472       operator=(const gribble&amp;);<br />
473 <br />
474       virtual <br />
475       ~gribble() throw ();<br />
476 <br />
477       // Start with a capital letter, end with a period.<br />
478       inline void  <br />
479       public_member(const char* __arg) const;<br />
480 <br />
481       // In-class function definitions should be restricted to one-liners.<br />
482       int <br />
483       one_line() { return 0 }<br />
484 <br />
485       int <br />
486       two_lines(const char* arg) <br />
487       { return strchr(arg, 'a'); }<br />
488 <br />
489       inline int <br />
490       three_lines();  // inline, but defined below.<br />
491 <br />
492       // Note indentation.<br />
493       template&lt;typename _Formal_argument&gt;<br />
494       void <br />
495       public_template() const throw();<br />
496 <br />
497       template&lt;typename _Iterator&gt;<br />
498       void <br />
499       other_template();<br />
500 <br />
501       private:<br />
502       class _Helper;<br />
503 <br />
504       int _M_private_data;<br />
505       int _M_more_stuff;<br />
506       _Helper* _M_helper;<br />
507       int _M_private_function();<br />
508 <br />
509       enum _Enum <br />
510       { <br />
511       _S_one, <br />
512       _S_two <br />
513       };<br />
514 <br />
515       static void <br />
516       _S_initialize_library();<br />
517       };<br />
518 <br />
519       // More-or-less-standard language features described by lack, not presence.<br />
520       # ifndef _G_NO_LONGLONG<br />
521       extern long long _G_global_with_a_good_long_name;  // avoid globals!<br />
522       # endif<br />
523 <br />
524       // Avoid in-class inline definitions, define separately;<br />
525       // likewise for member class definitions:<br />
526       inline int<br />
527       gribble::public_member() const<br />
528       { int __local = 0; return __local; }<br />
529 <br />
530       class gribble::_Helper<br />
531       {<br />
532       int _M_stuff;<br />
533 <br />
534       friend class gribble;<br />
535       };<br />
536       }<br />
537 <br />
538       // Names beginning with "__": only for arguments and<br />
539       //   local variables; never use "__" in a type name, or<br />
540       //   within any name; never use "__[0-9]".<br />
541 <br />
542       #endif /* _HEADER_ */<br />
543 <br />
544 <br />
545       namespace std <br />
546       {<br />
547       template&lt;typename T&gt;  // notice: "typename", not "class", no space<br />
548       long_return_value_type&lt;with_many, args&gt;  <br />
549       function_name(char* pointer,               // "char *pointer" is wrong.<br />
550       char* argument, <br />
551       const Reference&amp; ref)<br />
552       {<br />
553       // int a_local;  /* wrong; see below. */<br />
554       if (test) <br />
555       { <br />
556       nested code <br />
557       }<br />
558       <br />
559       int a_local = 0;  // declare variable at first use.<br />
560 <br />
561       //  char a, b, *p;   /* wrong */<br />
562       char a = 'a';<br />
563       char b = a + 1;<br />
564       char* c = "abc";  // each variable goes on its own line, always.<br />
565 <br />
566       // except maybe here...<br />
567       for (unsigned i = 0, mask = 1; mask; ++i, mask &lt;&lt;= 1) {<br />
568       // ...<br />
569       }<br />
570       }<br />
571       <br />
572       gribble::gribble()<br />
573       : _M_private_data(0), _M_more_stuff(0), _M_helper(0);<br />
574       { }<br />
575 <br />
576       inline int <br />
577       gribble::three_lines()<br />
578       {<br />
579       // doesn't fit in one line.<br />
580       }<br />
581       } // namespace std<br />
582     </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="bk01apas02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="appendix_contributing.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="bk01apas04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Directory Layout and Source Conventions </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Documentation Style</td></tr></table></div></body></html>