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