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"><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-NS Stylesheets V1.78.1" /><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.
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>
7 Identifiers that conflict and should be avoided.
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 />
56 [Note that this list is out of date. It applies to the old
<br />
57 name-mangling; in G++
3.0 and higher a different name-mangling is
<br />
58 used. In addition, many of the bugs relating to G++ interpreting
<br />
59 these names as operators have been fixed.]
<br />
61 The full set of __* identifiers (combined from gcc/cp/lex.c and
<br />
62 gcc/cplus-dem.c) that are either old or new, but are definitely
<br />
63 recognized by the demangler, is:
<br />
129 __postdecrement
<br />
130 __postincrement
<br />
147 __builtin_alloca
<br />
148 __builtin_fsqrt
<br />
152 __builtin_cast_f2i
<br />
153 __builtin_cast_i2f
<br />
154 __builtin_cast_d2ll
<br />
155 __builtin_cast_ll2d
<br />
156 __builtin_copy_dhi2i
<br />
157 __builtin_copy_i2dhi
<br />
158 __builtin_copy_dlo2i
<br />
159 __builtin_copy_i2dlo
<br />
160 __add_and_fetch
<br />
161 __sub_and_fetch
<br />
163 __xor_and_fetch
<br />
164 __and_and_fetch
<br />
165 __nand_and_fetch
<br />
166 __mpy_and_fetch
<br />
167 __min_and_fetch
<br />
168 __max_and_fetch
<br />
169 __fetch_and_add
<br />
170 __fetch_and_sub
<br />
172 __fetch_and_xor
<br />
173 __fetch_and_and
<br />
174 __fetch_and_nand
<br />
175 __fetch_and_mpy
<br />
176 __fetch_and_min
<br />
177 __fetch_and_max
<br />
178 __lock_test_and_set
<br />
181 __compare_and_swap
<br />
183 __high_multiply
<br />
190 __embedded_cplusplus
<br />
191 // long double conversion members mangled as __opr
<br />
192 // http://gcc.gnu.org/ml/libstdc++/
1999-q4/msg00060.html
<br />
194 </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 />
195 This library is written to appropriate C++ coding standards. As such,
<br />
196 it is intended to precede the recommendations of the GNU Coding
<br />
197 Standard, which can be referenced in full here:
<br />
199 <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 />
201 The rest of this is also interesting reading, but skip the
"Design<br />
204 The GCC coding conventions are here, and are also useful:
<br />
205 <a class=
"link" href=
"http://gcc.gnu.org/codingconventions.html" target=
"_top">http://gcc.gnu.org/codingconventions.html
</a><br />
207 In addition, because it doesn't seem to be stated explicitly anywhere
<br />
208 else, there is an
80 column source limit.
<br />
210 <code class=
"filename">ChangeLog
</code> entries for member functions should use the
<br />
211 classname::member function name syntax as follows:
<br />
213 <code class=
"code"><br />
214 1999-
04-
15 Dennis Ritchie
<dr@att.com
><br />
216 * src/basic_file.cc (__basic_file::open): Fix thinko in
<br />
217 _G_HAVE_IO_FILE_OPEN bits.
<br />
220 Notable areas of divergence from what may be previous local practice
<br />
221 (particularly for GNU C) include:
<br />
223 01. Pointers and references
<br />
224 <code class=
"code"><br />
225 char* p =
"flop";
<br />
226 char
& c = *p;
<br />
228 char *p =
"flop"; // wrong
<br />
229 char
&c = *p; // wrong
<br />
232 Reason: In C++, definitions are mixed with executable code. Here,
<br />
233 <code class=
"code">p
</code> is being initialized, not
<code class=
"code">*p
</code>. This is near-universal
<br />
234 practice among C++ programmers; it is normal for C hackers
<br />
235 to switch spontaneously as they gain experience.
<br />
237 02. Operator names and parentheses
<br />
238 <code class=
"code"><br />
239 operator==(type)
<br />
241 operator == (type) // wrong
<br />
244 Reason: The
<code class=
"code">==
</code> is part of the function name. Separating
<br />
245 it makes the declaration look like an expression.
<br />
247 03. Function names and parentheses
<br />
248 <code class=
"code"><br />
251 void mangle () // wrong
<br />
254 Reason: no space before parentheses (except after a control-flow
<br />
255 keyword) is near-universal practice for C++. It identifies the
<br />
256 parentheses as the function-call operator or declarator, as
<br />
257 opposed to an expression or other overloaded use of parentheses.
<br />
259 04. Template function indentation
<br />
260 <code class=
"code"><br />
261 template
<typename T
><br />
263 template_function(args)
<br />
266 template
<class T
><br />
267 void template_function(args) {};
<br />
270 Reason: In class definitions, without indentation whitespace is
<br />
271 needed both above and below the declaration to distinguish
<br />
272 it visually from other members. (Also, re:
"typename"<br />
273 rather than
"class".)
<code class=
"code">T
</code> often could be
<code class=
"code">int
</code>, which is
<br />
274 not a class. (
"class", here, is an anachronism.)
<br />
276 05. Template class indentation
<br />
277 <code class=
"code"><br />
278 template
<typename _CharT, typename _Traits
><br />
279 class basic_ios : public ios_base
<br />
285 template
<class _CharT, class _Traits
><br />
286 class basic_ios : public ios_base
<br />
292 template
<class _CharT, class _Traits
><br />
293 class basic_ios : public ios_base
<br />
300 06. Enumerators
<br />
301 <code class=
"code"><br />
304 space = _ISspace,
<br />
305 print = _ISprint,
<br />
306 cntrl = _IScntrl
<br />
309 enum { space = _ISspace, print = _ISprint, cntrl = _IScntrl };
<br />
312 07. Member initialization lists
<br />
313 All one line, separate from class name.
<br />
315 <code class=
"code"><br />
316 gribble::gribble()
<br />
317 : _M_private_data(
0), _M_more_stuff(
0), _M_helper(
0)
<br />
320 gribble::gribble() : _M_private_data(
0), _M_more_stuff(
0), _M_helper(
0)
<br />
324 08. Try/Catch blocks
<br />
325 <code class=
"code"><br />
342 09. Member functions declarations and definitions
<br />
343 Keywords such as extern, static, export, explicit, inline, etc
<br />
344 go on the line above the function name. Thus
<br />
346 <code class=
"code"><br />
350 virtual int foo()
<br />
353 Reason: GNU coding conventions dictate return types for functions
<br />
354 are on a separate line than the function name and parameter list
<br />
355 for definitions. For C++, where we have member functions that can
<br />
356 be either inline definitions or declarations, keeping to this
<br />
357 standard allows all member function names for a given class to be
<br />
358 aligned to the same margin, increasing readability.
<br />
361 10. Invocation of member functions with
"this->"<br />
362 For non-uglified names, use
<code class=
"code">this-
>name
</code> to call the function.
<br />
364 <code class=
"code"><br />
365 this-
>sync()
<br />
370 Reason: Koenig lookup.
<br />
373 <code class=
"code"><br />
376 blah blah blah;
<br />
377 } // namespace std
<br />
381 namespace std {
<br />
382 blah blah blah;
<br />
383 } // namespace std
<br />
386 12. Spacing under protected and private in class declarations:
<br />
387 space above, none below
<br />
390 <code class=
"code"><br />
400 13. Spacing WRT return statements.
<br />
401 no extra spacing before returns, no parenthesis
<br />
404 <code class=
"code"><br />
416 return (__ret);
<br />
420 14. Location of global variables.
<br />
421 All global variables of class type, whether in the
"user visible"<br />
422 space (e.g.,
<code class=
"code">cin
</code>) or the implementation namespace, must be defined
<br />
423 as a character array with the appropriate alignment and then later
<br />
424 re-initialized to the correct value.
<br />
426 This is due to startup issues on certain platforms, such as AIX.
<br />
427 For more explanation and examples, see
<code class=
"filename">src/globals.cc
</code>. All such
<br />
428 variables should be contained in that file, for simplicity.
<br />
430 15. Exception abstractions
<br />
431 Use the exception abstractions found in
<code class=
"filename">functexcept.h
</code>, which allow
<br />
432 C++ programmers to use this library with
<code class=
"literal">-fno-exceptions
</code>. (Even if
<br />
433 that is rarely advisable, it's a necessary evil for backwards
<br />
434 compatibility.)
<br />
436 16. Exception error messages
<br />
437 All start with the name of the function where the exception is
<br />
438 thrown, and then (optional) descriptive text is added. Example:
<br />
440 <code class=
"code"><br />
441 __throw_logic_error(__N(
"basic_string::_S_construct NULL not valid"));
<br />
444 Reason: The verbose terminate handler prints out
<code class=
"code">exception::what()
</code>,
<br />
445 as well as the typeinfo for the thrown exception. As this is the
<br />
446 default terminate handler, by putting location info into the
<br />
447 exception string, a very useful error message is printed out for
<br />
448 uncaught exceptions. So useful, in fact, that non-programmers can
<br />
449 give useful error messages, and programmers can intelligently
<br />
450 speculate what went wrong without even using a debugger.
<br />
452 17. The doxygen style guide to comments is a separate document,
<br />
455 The library currently has a mixture of GNU-C and modern C++ coding
<br />
456 styles. The GNU C usages will be combed out gradually.
<br />
460 For nonstandard names appearing in Standard headers, we are constrained
<br />
461 to use names that begin with underscores. This is called
"uglification".
<br />
462 The convention is:
<br />
464 Local and argument names:
<code class=
"literal">__[a-z].*
</code><br />
466 Examples:
<code class=
"code">__count __ix __s1
</code><br />
468 Type names and template formal-argument names:
<code class=
"literal">_[A-Z][^_].*
</code><br />
470 Examples:
<code class=
"code">_Helper _CharT _N
</code><br />
472 Member data and function names:
<code class=
"literal">_M_.*
</code><br />
474 Examples:
<code class=
"code">_M_num_elements _M_initialize ()
</code><br />
476 Static data members, constants, and enumerations:
<code class=
"literal">_S_.*
</code><br />
478 Examples:
<code class=
"code">_S_max_elements _S_default_value
</code><br />
480 Don't use names in the same scope that differ only in the prefix,
<br />
481 e.g. _S_top and _M_top. See BADNAMES for a list of forbidden names.
<br />
482 (The most tempting of these seem to be and
"_T" and
"__sz".)
<br />
484 Names must never have
"__" internally; it would confuse name
<br />
485 unmanglers on some targets. Also, never use
"__[0-9]", same reason.
<br />
487 --------------------------
<br />
490 <code class=
"code"><br />
492 #ifndef _HEADER_
<br />
493 #define _HEADER_
1<br />
500 gribble() throw();
<br />
502 gribble(const gribble
&);
<br />
505 gribble(int __howmany);
<br />
508 operator=(const gribble
&);
<br />
511 ~gribble() throw ();
<br />
513 // Start with a capital letter, end with a period.
<br />
515 public_member(const char* __arg) const;
<br />
517 // In-class function definitions should be restricted to one-liners.
<br />
519 one_line() { return
0 }
<br />
522 two_lines(const char* arg)
<br />
523 { return strchr(arg, 'a'); }
<br />
526 three_lines(); // inline, but defined below.
<br />
528 // Note indentation.
<br />
529 template
<typename _Formal_argument
><br />
531 public_template() const throw();
<br />
533 template
<typename _Iterator
><br />
535 other_template();
<br />
540 int _M_private_data;
<br />
541 int _M_more_stuff;
<br />
542 _Helper* _M_helper;
<br />
543 int _M_private_function();
<br />
552 _S_initialize_library();
<br />
555 // More-or-less-standard language features described by lack, not presence.
<br />
556 # ifndef _G_NO_LONGLONG
<br />
557 extern long long _G_global_with_a_good_long_name; // avoid globals!
<br />
560 // Avoid in-class inline definitions, define separately;
<br />
561 // likewise for member class definitions:
<br />
563 gribble::public_member() const
<br />
564 { int __local =
0; return __local; }
<br />
566 class gribble::_Helper
<br />
570 friend class gribble;
<br />
574 // Names beginning with
"__": only for arguments and
<br />
575 // local variables; never use
"__" in a type name, or
<br />
576 // within any name; never use
"__[0-9]".
<br />
578 #endif /* _HEADER_ */
<br />
583 template
<typename T
> // notice:
"typename", not
"class", no space
<br />
584 long_return_value_type
<with_many, args
><br />
585 function_name(char* pointer, //
"char *pointer" is wrong.
<br />
586 char* argument,
<br />
587 const Reference
& ref)
<br />
589 // int a_local; /* wrong; see below. */
<br />
595 int a_local =
0; // declare variable at first use.
<br />
597 // char a, b, *p; /* wrong */
<br />
599 char b = a +
1;
<br />
600 char* c =
"abc"; // each variable goes on its own line, always.
<br />
602 // except maybe here...
<br />
603 for (unsigned i =
0, mask =
1; mask; ++i, mask
<<=
1) {
<br />
608 gribble::gribble()
<br />
609 : _M_private_data(
0), _M_more_stuff(
0), _M_helper(
0)
<br />
613 gribble::three_lines()
<br />
615 // doesn't fit in one line.
<br />
617 } // namespace std
<br />
619 </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>