OSDN Git Service

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