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=" ISO C++ , library " /><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="source_organization.html" title="Directory Layout and Source Conventions" /><link rel="next" href="documentation_style.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="source_organization.html">Prev</a> </td><th width="60%" align="center">Appendix A.
6 </th><td width="20%" align="right"> <a accesskey="n" href="documentation_style.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>
7 </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>
8 Identifiers that conflict and should be avoided.
9 </p><div class="literallayout"><p><br />
10 This is the list of names “<span class="quote">reserved to the<br />
11 implementation</span>” that have been claimed by certain<br />
12 compilers and system headers of interest, and should not be used<br />
13 in the library. It will grow, of course. We generally are<br />
14 interested in names that are not all-caps, except for those like<br />
57 [Note that this list is out of date. It applies to the old<br />
58 name-mangling; in G++ 3.0 and higher a different name-mangling is<br />
59 used. In addition, many of the bugs relating to G++ interpreting<br />
60 these names as operators have been fixed.]<br />
62 The full set of __* identifiers (combined from gcc/cp/lex.c and<br />
63 gcc/cplus-dem.c) that are either old or new, but are definitely <br />
64 recognized by the demangler, is:<br />
130 __postdecrement<br />
131 __postincrement<br />
148 __builtin_alloca<br />
149 __builtin_fsqrt<br />
153 __builtin_cast_f2i<br />
154 __builtin_cast_i2f<br />
155 __builtin_cast_d2ll<br />
156 __builtin_cast_ll2d<br />
157 __builtin_copy_dhi2i<br />
158 __builtin_copy_i2dhi<br />
159 __builtin_copy_dlo2i<br />
160 __builtin_copy_i2dlo<br />
161 __add_and_fetch<br />
162 __sub_and_fetch<br />
164 __xor_and_fetch<br />
165 __and_and_fetch<br />
166 __nand_and_fetch<br />
167 __mpy_and_fetch<br />
168 __min_and_fetch<br />
169 __max_and_fetch<br />
170 __fetch_and_add<br />
171 __fetch_and_sub<br />
173 __fetch_and_xor<br />
174 __fetch_and_and<br />
175 __fetch_and_nand<br />
176 __fetch_and_mpy<br />
177 __fetch_and_min<br />
178 __fetch_and_max<br />
179 __lock_test_and_set<br />
182 __compare_and_swap<br />
184 __high_multiply<br />
191 __embedded_cplusplus<br />
192 // long double conversion members mangled as __opr<br />
193 // http://gcc.gnu.org/ml/libstdc++/1999-q4/msg00060.html<br />
195 </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 />
196 This library is written to appropriate C++ coding standards. As such,<br />
197 it is intended to precede the recommendations of the GNU Coding<br />
198 Standard, which can be referenced in full here:<br />
200 http://www.gnu.org/prep/standards/standards.html#Formatting<br />
202 The rest of this is also interesting reading, but skip the "Design<br />
205 The GCC coding conventions are here, and are also useful:<br />
206 http://gcc.gnu.org/codingconventions.html<br />
208 In addition, because it doesn't seem to be stated explicitly anywhere<br />
209 else, there is an 80 column source limit.<br />
211 ChangeLog entries for member functions should use the<br />
212 classname::member function name syntax as follows:<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 />
219 Notable areas of divergence from what may be previous local practice<br />
220 (particularly for GNU C) include:<br />
222 01. Pointers and references<br />
223 char* p = "flop";<br />
224 char& c = *p;<br />
226 char *p = "flop"; // wrong<br />
227 char &c = *p; // wrong<br />
229 Reason: In C++, definitions are mixed with executable code. Here, <br />
230 p is being initialized, not *p. 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 />
234 02. Operator names and parentheses<br />
235 operator==(type)<br />
237 operator == (type) // wrong<br />
239 Reason: The == is part of the function name. Separating<br />
240 it makes the declaration look like an expression. <br />
242 03. Function names and parentheses<br />
245 void mangle () // wrong<br />
247 Reason: no space before parentheses (except after a control-flow<br />
248 keyword) is near-universal practice for C++. It identifies the<br />
249 parentheses as the function-call operator or declarator, as <br />
250 opposed to an expression or other overloaded use of parentheses.<br />
252 04. Template function indentation<br />
253 template<typename T><br />
255 template_function(args)<br />
258 template<class T><br />
259 void template_function(args) {};<br />
261 Reason: In class definitions, without indentation whitespace is<br />
262 needed both above and below the declaration to distinguish<br />
263 it visually from other members. (Also, re: "typename"<br />
264 rather than "class".) T often could be int, which is <br />
265 not a class. ("class", here, is an anachronism.)<br />
267 05. Template class indentation<br />
268 template<typename _CharT, typename _Traits><br />
269 class basic_ios : public ios_base<br />
275 template<class _CharT, class _Traits><br />
276 class basic_ios : public ios_base<br />
282 template<class _CharT, class _Traits><br />
283 class basic_ios : public ios_base<br />
289 06. Enumerators<br />
292 space = _ISspace,<br />
293 print = _ISprint,<br />
294 cntrl = _IScntrl<br />
297 enum { space = _ISspace, print = _ISprint, cntrl = _IScntrl };<br />
299 07. Member initialization lists<br />
300 All one line, separate from class name.<br />
302 gribble::gribble() <br />
303 : _M_private_data(0), _M_more_stuff(0), _M_helper(0);<br />
306 gribble::gribble() : _M_private_data(0), _M_more_stuff(0), _M_helper(0);<br />
309 08. Try/Catch blocks<br />
321 } catch(...) { <br />
325 09. Member functions declarations and definitions<br />
326 Keywords such as extern, static, export, explicit, inline, etc<br />
327 go on the line above the function name. Thus<br />
332 virtual int foo()<br />
334 Reason: GNU coding conventions dictate return types for functions<br />
335 are on a separate line than the function name and parameter list<br />
336 for definitions. For C++, where we have member functions that can<br />
337 be either inline definitions or declarations, keeping to this<br />
338 standard allows all member function names for a given class to be<br />
339 aligned to the same margin, increasing readability.<br />
342 10. Invocation of member functions with "this->"<br />
343 For non-uglified names, use this->name to call the function.<br />
345 this->sync()<br />
349 Reason: Koenig lookup.<br />
354 blah blah blah;<br />
355 } // namespace std<br />
359 namespace std {<br />
360 blah blah blah;<br />
361 } // namespace std<br />
363 12. Spacing under protected and private in class declarations:<br />
364 space above, none below<br />
375 13. Spacing WRT return statements.<br />
376 no extra spacing before returns, no parenthesis<br />
390 return (__ret);<br />
393 14. Location of global variables.<br />
394 All global variables of class type, whether in the "user visible"<br />
395 space (e.g., cin) or the implementation namespace, must be defined<br />
396 as a character array with the appropriate alignment and then later<br />
397 re-initialized to the correct value.<br />
399 This is due to startup issues on certain platforms, such as AIX.<br />
400 For more explanation and examples, see src/globals.cc. All such<br />
401 variables should be contained in that file, for simplicity.<br />
403 15. Exception abstractions<br />
404 Use the exception abstractions found in functexcept.h, which allow<br />
405 C++ programmers to use this library with -fno-exceptions. (Even if<br />
406 that is rarely advisable, it's a necessary evil for backwards<br />
407 compatibility.)<br />
409 16. Exception error messages<br />
410 All start with the name of the function where the exception is<br />
411 thrown, and then (optional) descriptive text is added. Example:<br />
413 __throw_logic_error(__N("basic_string::_S_construct NULL not valid"));<br />
415 Reason: The verbose terminate handler prints out exception::what(),<br />
416 as well as the typeinfo for the thrown exception. As this is the<br />
417 default terminate handler, by putting location info into the<br />
418 exception string, a very useful error message is printed out for<br />
419 uncaught exceptions. So useful, in fact, that non-programmers can<br />
420 give useful error messages, and programmers can intelligently<br />
421 speculate what went wrong without even using a debugger.<br />
423 17. The doxygen style guide to comments is a separate document,<br />
426 The library currently has a mixture of GNU-C and modern C++ coding<br />
427 styles. The GNU C usages will be combed out gradually.<br />
431 For nonstandard names appearing in Standard headers, we are constrained <br />
432 to use names that begin with underscores. This is called "uglification".<br />
433 The convention is:<br />
435 Local and argument names: __[a-z].*<br />
437 Examples: __count __ix __s1 <br />
439 Type names and template formal-argument names: _[A-Z][^_].*<br />
441 Examples: _Helper _CharT _N <br />
443 Member data and function names: _M_.*<br />
445 Examples: _M_num_elements _M_initialize ()<br />
447 Static data members, constants, and enumerations: _S_.*<br />
449 Examples: _S_max_elements _S_default_value<br />
451 Don't use names in the same scope that differ only in the prefix, <br />
452 e.g. _S_top and _M_top. See BADNAMES for a list of forbidden names.<br />
453 (The most tempting of these seem to be and "_T" and "__sz".)<br />
455 Names must never have "__" internally; it would confuse name<br />
456 unmanglers on some targets. Also, never use "__[0-9]", same reason.<br />
458 --------------------------<br />
462 #ifndef _HEADER_<br />
463 #define _HEADER_ 1<br />
470 gribble() throw();<br />
472 gribble(const gribble&);<br />
475 gribble(int __howmany);<br />
478 operator=(const gribble&);<br />
481 ~gribble() throw ();<br />
483 // Start with a capital letter, end with a period.<br />
485 public_member(const char* __arg) const;<br />
487 // In-class function definitions should be restricted to one-liners.<br />
489 one_line() { return 0 }<br />
492 two_lines(const char* arg) <br />
493 { return strchr(arg, 'a'); }<br />
496 three_lines(); // inline, but defined below.<br />
498 // Note indentation.<br />
499 template<typename _Formal_argument><br />
501 public_template() const throw();<br />
503 template<typename _Iterator><br />
505 other_template();<br />
510 int _M_private_data;<br />
511 int _M_more_stuff;<br />
512 _Helper* _M_helper;<br />
513 int _M_private_function();<br />
522 _S_initialize_library();<br />
525 // More-or-less-standard language features described by lack, not presence.<br />
526 # ifndef _G_NO_LONGLONG<br />
527 extern long long _G_global_with_a_good_long_name; // avoid globals!<br />
530 // Avoid in-class inline definitions, define separately;<br />
531 // likewise for member class definitions:<br />
533 gribble::public_member() const<br />
534 { int __local = 0; return __local; }<br />
536 class gribble::_Helper<br />
540 friend class gribble;<br />
544 // Names beginning with "__": only for arguments and<br />
545 // local variables; never use "__" in a type name, or<br />
546 // within any name; never use "__[0-9]".<br />
548 #endif /* _HEADER_ */<br />
553 template<typename T> // notice: "typename", not "class", no space<br />
554 long_return_value_type<with_many, args> <br />
555 function_name(char* pointer, // "char *pointer" is wrong.<br />
556 char* argument, <br />
557 const Reference& ref)<br />
559 // int a_local; /* wrong; see below. */<br />
565 int a_local = 0; // declare variable at first use.<br />
567 // char a, b, *p; /* wrong */<br />
569 char b = a + 1;<br />
570 char* c = "abc"; // each variable goes on its own line, always.<br />
572 // except maybe here...<br />
573 for (unsigned i = 0, mask = 1; mask; ++i, mask <<= 1) {<br />
578 gribble::gribble()<br />
579 : _M_private_data(0), _M_more_stuff(0), _M_helper(0);<br />
583 gribble::three_lines()<br />
585 // doesn't fit in one line.<br />
587 } // namespace std<br />
588 </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="documentation_style.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>