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