1 <html><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.76.1"><meta name=
"keywords" content=
"
5 "><meta name=
"keywords" content=
"
11 "><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 bgcolor=
"white" text=
"black" link=
"#0000FF" vlink=
"#840084" alink=
"#0000FF"><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.
14 </th><td width=
"20%" align=
"right"> <a accesskey=
"n" href=
"source_design_notes.html">Next
</a></td></tr></table><hr></div><div class=
"section" title=
"Coding Style"><div class=
"titlepage"><div><div><h2 class=
"title" style=
"clear: both"><a name=
"contrib.coding_style"></a>Coding Style
</h2></div></div></div><p>
15 </p><div class=
"section" title=
"Bad Identifiers"><div class=
"titlepage"><div><div><h3 class=
"title"><a name=
"coding_style.bad_identifiers"></a>Bad Identifiers
</h3></div></div></div><p>
16 Identifiers that conflict and should be avoided.
17 </p><div class=
"literallayout"><p><br>
18 This is the list of names
<span class=
"quote">“
<span class=
"quote">reserved to the
<br>
19 implementation
</span>”
</span> that have been claimed by certain
<br>
20 compilers and system headers of interest, and should not be used
<br>
21 in the library. It will grow, of course. We generally are
<br>
22 interested in names that are not all-caps, except for those like
<br>
65 [Note that this list is out of date. It applies to the old
<br>
66 name-mangling; in G++
3.0 and higher a different name-mangling is
<br>
67 used. In addition, many of the bugs relating to G++ interpreting
<br>
68 these names as operators have been fixed.]
<br>
70 The full set of __* identifiers (combined from gcc/cp/lex.c and
<br>
71 gcc/cplus-dem.c) that are either old or new, but are definitely
<br>
72 recognized by the demangler, is:
<br>
161 __builtin_cast_f2i
<br>
162 __builtin_cast_i2f
<br>
163 __builtin_cast_d2ll
<br>
164 __builtin_cast_ll2d
<br>
165 __builtin_copy_dhi2i
<br>
166 __builtin_copy_i2dhi
<br>
167 __builtin_copy_dlo2i
<br>
168 __builtin_copy_i2dlo
<br>
187 __lock_test_and_set
<br>
190 __compare_and_swap
<br>
199 __embedded_cplusplus
<br>
200 // long double conversion members mangled as __opr
<br>
201 // http://gcc.gnu.org/ml/libstdc++/
1999-q4/msg00060.html
<br>
203 </p></div></div><div class=
"section" title=
"By Example"><div class=
"titlepage"><div><div><h3 class=
"title"><a name=
"coding_style.example"></a>By Example
</h3></div></div></div><div class=
"literallayout"><p><br>
204 This library is written to appropriate C++ coding standards. As such,
<br>
205 it is intended to precede the recommendations of the GNU Coding
<br>
206 Standard, which can be referenced in full here:
<br>
208 <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>
210 The rest of this is also interesting reading, but skip the
"Design<br>
213 The GCC coding conventions are here, and are also useful:
<br>
214 <a class=
"link" href=
"http://gcc.gnu.org/codingconventions.html" target=
"_top">http://gcc.gnu.org/codingconventions.html
</a><br>
216 In addition, because it doesn't seem to be stated explicitly anywhere
<br>
217 else, there is an
80 column source limit.
<br>
219 <code class=
"filename">ChangeLog
</code> entries for member functions should use the
<br>
220 classname::member function name syntax as follows:
<br>
222 <code class=
"code"><br>
223 1999-
04-
15 Dennis Ritchie
<dr@att.com
><br>
225 * src/basic_file.cc (__basic_file::open): Fix thinko in
<br>
226 _G_HAVE_IO_FILE_OPEN bits.
<br>
229 Notable areas of divergence from what may be previous local practice
<br>
230 (particularly for GNU C) include:
<br>
232 01. Pointers and references
<br>
233 <code class=
"code"><br>
234 char* p =
"flop";
<br>
235 char
& c = *p;
<br>
237 char *p =
"flop"; // wrong
<br>
238 char
&c = *p; // wrong
<br>
241 Reason: In C++, definitions are mixed with executable code. Here,
<br>
242 <code class=
"code">p
</code> is being initialized, not
<code class=
"code">*p
</code>. This is near-universal
<br>
243 practice among C++ programmers; it is normal for C hackers
<br>
244 to switch spontaneously as they gain experience.
<br>
246 02. Operator names and parentheses
<br>
247 <code class=
"code"><br>
250 operator == (type) // wrong
<br>
253 Reason: The
<code class=
"code">==
</code> is part of the function name. Separating
<br>
254 it makes the declaration look like an expression.
<br>
256 03. Function names and parentheses
<br>
257 <code class=
"code"><br>
260 void mangle () // wrong
<br>
263 Reason: no space before parentheses (except after a control-flow
<br>
264 keyword) is near-universal practice for C++. It identifies the
<br>
265 parentheses as the function-call operator or declarator, as
<br>
266 opposed to an expression or other overloaded use of parentheses.
<br>
268 04. Template function indentation
<br>
269 <code class=
"code"><br>
270 template
<typename T
><br>
272 template_function(args)
<br>
275 template
<class T
><br>
276 void template_function(args) {};
<br>
279 Reason: In class definitions, without indentation whitespace is
<br>
280 needed both above and below the declaration to distinguish
<br>
281 it visually from other members. (Also, re:
"typename"<br>
282 rather than
"class".)
<code class=
"code">T
</code> often could be
<code class=
"code">int
</code>, which is
<br>
283 not a class. (
"class", here, is an anachronism.)
<br>
285 05. Template class indentation
<br>
286 <code class=
"code"><br>
287 template
<typename _CharT, typename _Traits
><br>
288 class basic_ios : public ios_base
<br>
294 template
<class _CharT, class _Traits
><br>
295 class basic_ios : public ios_base
<br>
301 template
<class _CharT, class _Traits
><br>
302 class basic_ios : public ios_base
<br>
310 <code class=
"code"><br>
313 space = _ISspace,
<br>
314 print = _ISprint,
<br>
318 enum { space = _ISspace, print = _ISprint, cntrl = _IScntrl };
<br>
321 07. Member initialization lists
<br>
322 All one line, separate from class name.
<br>
324 <code class=
"code"><br>
325 gribble::gribble()
<br>
326 : _M_private_data(
0), _M_more_stuff(
0), _M_helper(
0)
<br>
329 gribble::gribble() : _M_private_data(
0), _M_more_stuff(
0), _M_helper(
0)
<br>
333 08. Try/Catch blocks
<br>
334 <code class=
"code"><br>
351 09. Member functions declarations and definitions
<br>
352 Keywords such as extern, static, export, explicit, inline, etc
<br>
353 go on the line above the function name. Thus
<br>
355 <code class=
"code"><br>
359 virtual int foo()
<br>
362 Reason: GNU coding conventions dictate return types for functions
<br>
363 are on a separate line than the function name and parameter list
<br>
364 for definitions. For C++, where we have member functions that can
<br>
365 be either inline definitions or declarations, keeping to this
<br>
366 standard allows all member function names for a given class to be
<br>
367 aligned to the same margin, increasing readability.
<br>
370 10. Invocation of member functions with
"this->"<br>
371 For non-uglified names, use
<code class=
"code">this-
>name
</code> to call the function.
<br>
373 <code class=
"code"><br>
379 Reason: Koenig lookup.
<br>
382 <code class=
"code"><br>
386 } // namespace std
<br>
392 } // namespace std
<br>
395 12. Spacing under protected and private in class declarations:
<br>
396 space above, none below
<br>
399 <code class=
"code"><br>
409 13. Spacing WRT return statements.
<br>
410 no extra spacing before returns, no parenthesis
<br>
413 <code class=
"code"><br>
429 14. Location of global variables.
<br>
430 All global variables of class type, whether in the
"user visible"<br>
431 space (e.g.,
<code class=
"code">cin
</code>) or the implementation namespace, must be defined
<br>
432 as a character array with the appropriate alignment and then later
<br>
433 re-initialized to the correct value.
<br>
435 This is due to startup issues on certain platforms, such as AIX.
<br>
436 For more explanation and examples, see
<code class=
"filename">src/globals.cc
</code>. All such
<br>
437 variables should be contained in that file, for simplicity.
<br>
439 15. Exception abstractions
<br>
440 Use the exception abstractions found in
<code class=
"filename">functexcept.h
</code>, which allow
<br>
441 C++ programmers to use this library with
<code class=
"literal">-fno-exceptions
</code>. (Even if
<br>
442 that is rarely advisable, it's a necessary evil for backwards
<br>
445 16. Exception error messages
<br>
446 All start with the name of the function where the exception is
<br>
447 thrown, and then (optional) descriptive text is added. Example:
<br>
449 <code class=
"code"><br>
450 __throw_logic_error(__N(
"basic_string::_S_construct NULL not valid"));
<br>
453 Reason: The verbose terminate handler prints out
<code class=
"code">exception::what()
</code>,
<br>
454 as well as the typeinfo for the thrown exception. As this is the
<br>
455 default terminate handler, by putting location info into the
<br>
456 exception string, a very useful error message is printed out for
<br>
457 uncaught exceptions. So useful, in fact, that non-programmers can
<br>
458 give useful error messages, and programmers can intelligently
<br>
459 speculate what went wrong without even using a debugger.
<br>
461 17. The doxygen style guide to comments is a separate document,
<br>
464 The library currently has a mixture of GNU-C and modern C++ coding
<br>
465 styles. The GNU C usages will be combed out gradually.
<br>
469 For nonstandard names appearing in Standard headers, we are constrained
<br>
470 to use names that begin with underscores. This is called
"uglification".
<br>
471 The convention is:
<br>
473 Local and argument names:
<code class=
"literal">__[a-z].*
</code><br>
475 Examples:
<code class=
"code">__count __ix __s1
</code><br>
477 Type names and template formal-argument names:
<code class=
"literal">_[A-Z][^_].*
</code><br>
479 Examples:
<code class=
"code">_Helper _CharT _N
</code><br>
481 Member data and function names:
<code class=
"literal">_M_.*
</code><br>
483 Examples:
<code class=
"code">_M_num_elements _M_initialize ()
</code><br>
485 Static data members, constants, and enumerations:
<code class=
"literal">_S_.*
</code><br>
487 Examples:
<code class=
"code">_S_max_elements _S_default_value
</code><br>
489 Don't use names in the same scope that differ only in the prefix,
<br>
490 e.g. _S_top and _M_top. See BADNAMES for a list of forbidden names.
<br>
491 (The most tempting of these seem to be and
"_T" and
"__sz".)
<br>
493 Names must never have
"__" internally; it would confuse name
<br>
494 unmanglers on some targets. Also, never use
"__[0-9]", same reason.
<br>
496 --------------------------
<br>
499 <code class=
"code"><br>
502 #define _HEADER_
1<br>
509 gribble() throw();
<br>
511 gribble(const gribble
&);
<br>
514 gribble(int __howmany);
<br>
517 operator=(const gribble
&);
<br>
520 ~gribble() throw ();
<br>
522 // Start with a capital letter, end with a period.
<br>
524 public_member(const char* __arg) const;
<br>
526 // In-class function definitions should be restricted to one-liners.
<br>
528 one_line() { return
0 }
<br>
531 two_lines(const char* arg)
<br>
532 { return strchr(arg, 'a'); }
<br>
535 three_lines(); // inline, but defined below.
<br>
537 // Note indentation.
<br>
538 template
<typename _Formal_argument
><br>
540 public_template() const throw();
<br>
542 template
<typename _Iterator
><br>
544 other_template();
<br>
549 int _M_private_data;
<br>
550 int _M_more_stuff;
<br>
551 _Helper* _M_helper;
<br>
552 int _M_private_function();
<br>
561 _S_initialize_library();
<br>
564 // More-or-less-standard language features described by lack, not presence.
<br>
565 # ifndef _G_NO_LONGLONG
<br>
566 extern long long _G_global_with_a_good_long_name; // avoid globals!
<br>
569 // Avoid in-class inline definitions, define separately;
<br>
570 // likewise for member class definitions:
<br>
572 gribble::public_member() const
<br>
573 { int __local =
0; return __local; }
<br>
575 class gribble::_Helper
<br>
579 friend class gribble;
<br>
583 // Names beginning with
"__": only for arguments and
<br>
584 // local variables; never use
"__" in a type name, or
<br>
585 // within any name; never use
"__[0-9]".
<br>
587 #endif /* _HEADER_ */
<br>
592 template
<typename T
> // notice:
"typename", not
"class", no space
<br>
593 long_return_value_type
<with_many, args
><br>
594 function_name(char* pointer, //
"char *pointer" is wrong.
<br>
596 const Reference
& ref)
<br>
598 // int a_local; /* wrong; see below. */
<br>
604 int a_local =
0; // declare variable at first use.
<br>
606 // char a, b, *p; /* wrong */
<br>
609 char* c =
"abc"; // each variable goes on its own line, always.
<br>
611 // except maybe here...
<br>
612 for (unsigned i =
0, mask =
1; mask; ++i, mask
<<=
1) {
<br>
617 gribble::gribble()
<br>
618 : _M_private_data(
0), _M_more_stuff(
0), _M_helper(
0)
<br>
622 gribble::three_lines()
<br>
624 // doesn't fit in one line.
<br>
626 } // namespace std
<br>
628 </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>