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.73.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="start" 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 />
51 [Note that this list is out of date. It applies to the old<br />
52 name-mangling; in G++ 3.0 and higher a different name-mangling is<br />
53 used. In addition, many of the bugs relating to G++ interpreting<br />
54 these names as operators have been fixed.]<br />
56 The full set of __* identifiers (combined from gcc/cp/lex.c and<br />
57 gcc/cplus-dem.c) that are either old or new, but are definitely <br />
58 recognized by the demangler, is:<br />
124 __postdecrement<br />
125 __postincrement<br />
142 __builtin_alloca<br />
143 __builtin_fsqrt<br />
147 __builtin_cast_f2i<br />
148 __builtin_cast_i2f<br />
149 __builtin_cast_d2ll<br />
150 __builtin_cast_ll2d<br />
151 __builtin_copy_dhi2i<br />
152 __builtin_copy_i2dhi<br />
153 __builtin_copy_dlo2i<br />
154 __builtin_copy_i2dlo<br />
155 __add_and_fetch<br />
156 __sub_and_fetch<br />
158 __xor_and_fetch<br />
159 __and_and_fetch<br />
160 __nand_and_fetch<br />
161 __mpy_and_fetch<br />
162 __min_and_fetch<br />
163 __max_and_fetch<br />
164 __fetch_and_add<br />
165 __fetch_and_sub<br />
167 __fetch_and_xor<br />
168 __fetch_and_and<br />
169 __fetch_and_nand<br />
170 __fetch_and_mpy<br />
171 __fetch_and_min<br />
172 __fetch_and_max<br />
173 __lock_test_and_set<br />
176 __compare_and_swap<br />
178 __high_multiply<br />
185 __embedded_cplusplus<br />
186 // long double conversion members mangled as __opr<br />
187 // http://gcc.gnu.org/ml/libstdc++/1999-q4/msg00060.html<br />
189 </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 />
190 This library is written to appropriate C++ coding standards. As such,<br />
191 it is intended to precede the recommendations of the GNU Coding<br />
192 Standard, which can be referenced in full here:<br />
194 http://www.gnu.org/prep/standards/standards.html#Formatting<br />
196 The rest of this is also interesting reading, but skip the "Design<br />
199 The GCC coding conventions are here, and are also useful:<br />
200 http://gcc.gnu.org/codingconventions.html<br />
202 In addition, because it doesn't seem to be stated explicitly anywhere<br />
203 else, there is an 80 column source limit.<br />
205 ChangeLog entries for member functions should use the<br />
206 classname::member function name syntax as follows:<br />
208 1999-04-15 Dennis Ritchie <dr@att.com><br />
210 * src/basic_file.cc (__basic_file::open): Fix thinko in<br />
211 _G_HAVE_IO_FILE_OPEN bits.<br />
213 Notable areas of divergence from what may be previous local practice<br />
214 (particularly for GNU C) include:<br />
216 01. Pointers and references<br />
217 char* p = "flop";<br />
218 char& c = *p;<br />
220 char *p = "flop"; // wrong<br />
221 char &c = *p; // wrong<br />
223 Reason: In C++, definitions are mixed with executable code. Here, <br />
224 p is being initialized, not *p. This is near-universal<br />
225 practice among C++ programmers; it is normal for C hackers<br />
226 to switch spontaneously as they gain experience.<br />
228 02. Operator names and parentheses<br />
229 operator==(type)<br />
231 operator == (type) // wrong<br />
233 Reason: The == is part of the function name. Separating<br />
234 it makes the declaration look like an expression. <br />
236 03. Function names and parentheses<br />
239 void mangle () // wrong<br />
241 Reason: no space before parentheses (except after a control-flow<br />
242 keyword) is near-universal practice for C++. It identifies the<br />
243 parentheses as the function-call operator or declarator, as <br />
244 opposed to an expression or other overloaded use of parentheses.<br />
246 04. Template function indentation<br />
247 template<typename T><br />
249 template_function(args)<br />
252 template<class T><br />
253 void template_function(args) {};<br />
255 Reason: In class definitions, without indentation whitespace is<br />
256 needed both above and below the declaration to distinguish<br />
257 it visually from other members. (Also, re: "typename"<br />
258 rather than "class".) T often could be int, which is <br />
259 not a class. ("class", here, is an anachronism.)<br />
261 05. Template class indentation<br />
262 template<typename _CharT, typename _Traits><br />
263 class basic_ios : public ios_base<br />
269 template<class _CharT, class _Traits><br />
270 class basic_ios : public ios_base<br />
276 template<class _CharT, class _Traits><br />
277 class basic_ios : public ios_base<br />
283 06. Enumerators<br />
286 space = _ISspace,<br />
287 print = _ISprint,<br />
288 cntrl = _IScntrl<br />
291 enum { space = _ISspace, print = _ISprint, cntrl = _IScntrl };<br />
293 07. Member initialization lists<br />
294 All one line, separate from class name.<br />
296 gribble::gribble() <br />
297 : _M_private_data(0), _M_more_stuff(0), _M_helper(0);<br />
300 gribble::gribble() : _M_private_data(0), _M_more_stuff(0), _M_helper(0);<br />
303 08. Try/Catch blocks<br />
315 } catch(...) { <br />
319 09. Member functions declarations and definitions<br />
320 Keywords such as extern, static, export, explicit, inline, etc<br />
321 go on the line above the function name. Thus<br />
326 virtual int foo()<br />
328 Reason: GNU coding conventions dictate return types for functions<br />
329 are on a separate line than the function name and parameter list<br />
330 for definitions. For C++, where we have member functions that can<br />
331 be either inline definitions or declarations, keeping to this<br />
332 standard allows all member function names for a given class to be<br />
333 aligned to the same margin, increasing readability.<br />
336 10. Invocation of member functions with "this->"<br />
337 For non-uglified names, use this->name to call the function.<br />
339 this->sync()<br />
343 Reason: Koenig lookup.<br />
348 blah blah blah;<br />
349 } // namespace std<br />
353 namespace std {<br />
354 blah blah blah;<br />
355 } // namespace std<br />
357 12. Spacing under protected and private in class declarations:<br />
358 space above, none below<br />
369 13. Spacing WRT return statements.<br />
370 no extra spacing before returns, no parenthesis<br />
384 return (__ret);<br />
387 14. Location of global variables.<br />
388 All global variables of class type, whether in the "user visible"<br />
389 space (e.g., cin) or the implementation namespace, must be defined<br />
390 as a character array with the appropriate alignment and then later<br />
391 re-initialized to the correct value.<br />
393 This is due to startup issues on certain platforms, such as AIX.<br />
394 For more explanation and examples, see src/globals.cc. All such<br />
395 variables should be contained in that file, for simplicity.<br />
397 15. Exception abstractions<br />
398 Use the exception abstractions found in functexcept.h, which allow<br />
399 C++ programmers to use this library with -fno-exceptions. (Even if<br />
400 that is rarely advisable, it's a necessary evil for backwards<br />
401 compatibility.)<br />
403 16. Exception error messages<br />
404 All start with the name of the function where the exception is<br />
405 thrown, and then (optional) descriptive text is added. Example:<br />
407 __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));<br />
409 Reason: The verbose terminate handler prints out exception::what(),<br />
410 as well as the typeinfo for the thrown exception. As this is the<br />
411 default terminate handler, by putting location info into the<br />
412 exception string, a very useful error message is printed out for<br />
413 uncaught exceptions. So useful, in fact, that non-programmers can<br />
414 give useful error messages, and programmers can intelligently<br />
415 speculate what went wrong without even using a debugger.<br />
417 17. The doxygen style guide to comments is a separate document,<br />
420 The library currently has a mixture of GNU-C and modern C++ coding<br />
421 styles. The GNU C usages will be combed out gradually.<br />
425 For nonstandard names appearing in Standard headers, we are constrained <br />
426 to use names that begin with underscores. This is called "uglification".<br />
427 The convention is:<br />
429 Local and argument names: __[a-z].*<br />
431 Examples: __count __ix __s1 <br />
433 Type names and template formal-argument names: _[A-Z][^_].*<br />
435 Examples: _Helper _CharT _N <br />
437 Member data and function names: _M_.*<br />
439 Examples: _M_num_elements _M_initialize ()<br />
441 Static data members, constants, and enumerations: _S_.*<br />
443 Examples: _S_max_elements _S_default_value<br />
445 Don't use names in the same scope that differ only in the prefix, <br />
446 e.g. _S_top and _M_top. See BADNAMES for a list of forbidden names.<br />
447 (The most tempting of these seem to be and "_T" and "__sz".)<br />
449 Names must never have "__" internally; it would confuse name<br />
450 unmanglers on some targets. Also, never use "__[0-9]", same reason.<br />
452 --------------------------<br />
456 #ifndef _HEADER_<br />
457 #define _HEADER_ 1<br />
464 gribble() throw();<br />
466 gribble(const gribble&);<br />
469 gribble(int __howmany);<br />
472 operator=(const gribble&);<br />
475 ~gribble() throw ();<br />
477 // Start with a capital letter, end with a period.<br />
479 public_member(const char* __arg) const;<br />
481 // In-class function definitions should be restricted to one-liners.<br />
483 one_line() { return 0 }<br />
486 two_lines(const char* arg) <br />
487 { return strchr(arg, 'a'); }<br />
490 three_lines(); // inline, but defined below.<br />
492 // Note indentation.<br />
493 template<typename _Formal_argument><br />
495 public_template() const throw();<br />
497 template<typename _Iterator><br />
499 other_template();<br />
504 int _M_private_data;<br />
505 int _M_more_stuff;<br />
506 _Helper* _M_helper;<br />
507 int _M_private_function();<br />
516 _S_initialize_library();<br />
519 // More-or-less-standard language features described by lack, not presence.<br />
520 # ifndef _G_NO_LONGLONG<br />
521 extern long long _G_global_with_a_good_long_name; // avoid globals!<br />
524 // Avoid in-class inline definitions, define separately;<br />
525 // likewise for member class definitions:<br />
527 gribble::public_member() const<br />
528 { int __local = 0; return __local; }<br />
530 class gribble::_Helper<br />
534 friend class gribble;<br />
538 // Names beginning with "__": only for arguments and<br />
539 // local variables; never use "__" in a type name, or<br />
540 // within any name; never use "__[0-9]".<br />
542 #endif /* _HEADER_ */<br />
547 template<typename T> // notice: "typename", not "class", no space<br />
548 long_return_value_type<with_many, args> <br />
549 function_name(char* pointer, // "char *pointer" is wrong.<br />
550 char* argument, <br />
551 const Reference& ref)<br />
553 // int a_local; /* wrong; see below. */<br />
559 int a_local = 0; // declare variable at first use.<br />
561 // char a, b, *p; /* wrong */<br />
563 char b = a + 1;<br />
564 char* c = "abc"; // each variable goes on its own line, always.<br />
566 // except maybe here...<br />
567 for (unsigned i = 0, mask = 1; mask; ++i, mask <<= 1) {<br />
572 gribble::gribble()<br />
573 : _M_private_data(0), _M_more_stuff(0), _M_helper(0);<br />
577 gribble::three_lines()<br />
579 // doesn't fit in one line.<br />
581 } // namespace std<br />
582 </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>