GNU's C ++ code writing specification

xiaoxiao2021-03-06  62

GNU's C code writing specification, C language father Dennis Ritchie personally revised C Standard Library Style Guidelines Draft 1999-02-26 ---------------------------------------------------------------------------------------------------- ------------- This Library IS Written To Appropriate C Coding Standards. As Such, IT IS intended to Precede The Recommendations of The GNU Codingstandard, Which Can Be Reference Here: http: // www. gnu.ai.mit.edu/prep/standards_toc.html ChangeLog entries for member functions should use theclassname :: member function name syntax as follows: 1999-04-15 Dennis Ritchie * src / basic_file.cc (__basic_file :: open): Fix thinko in_G_HAVE_IO_FILE_OPEN bits.Notable areas of divergence from what may be previous local practice (particularly for GNU C) include: 01 Pointers and references char * p = "flop"; char & c = * p;. -Not- char * p = "flop"; // Wrong Char & c = * p; // Wrong Reason: In C , definitions are mixed with executable code. Here, p is being initialized, not * p. This is near- Universal Practice Among C Programmers; IT IS Normal For c hackers to switch spontaneously as the gain experience.02. Operator names and petheses Operator == (TYPE) -Not- Operator == (TYPE) // Wrong Reason: The == IS Part of the function name. Separating it Makes the declaration look like an expression 03. Function names and parentheses void mangle () -NOT- void mangle () // wrong Reason:. no space before parentheses (except after a control-flow keyword) is near-universal practice for C

. It identifies the parentheses as the function-call operator or declarator, as opposed to an expression or other overloaded use of parentheses.04. Template function indentation template void template_function (args) {} -NOT- template void template_function (args) {}; Reason: In class definitions, without indentation whitespace is needed both above and below the declaration to distinguish it visually from other members (Also, re: "typename" rather than "class".) T. often could be int, which is not a class ( "class", here, is an anachronism.) 05 Template class indentation template class basic_ios: public ios_base {public: // Types:}..; -Not- template Class Basic_ios: public ios_base {public: // types:}; -not- template class basic_ios: public ios_base {public: // types:}; 06. ENUMERATORS . Enum {space = _ISspace, print = _ISprint, cntrl = _IScntrl,}; -NOT- enum {space = _ISspace, print = _ISprint, cntrl = _IScntrl}; 07 Member initialization lists All one line, separate from class name gribble:. : gribble (): _M_Private_Data (0), _m_more_stuff (0), _m_helper (0); {} -not- gribble :: gribble (): _m_private_data (0), _m_more_stuff (0), _M_HELPER (0);

{} 08. Try / Catch Blocks Try {//} catch (...) {//} - NOT- Try {//} catch (...) {//} 09. Member Functions Declarations and Defintions Keywords Such . as extern, static, export, explicit, inline, etc go on the line above the function name Thus virtual int foo () -NOT- virtual int foo () Reason: GNU coding conventions dictate return types for functions are on a separate line than the function name and parameter list for definitions. for C , where we have member functions that can. be either inline definitions or declarations, keeping to this standard allows all member function names for a given class to bealigned to the same margin, increasing readibility .10. Invocation of Member Functions with "this->" for non-uglified names, user this-> name to call the function. This-> sync () -not- sync () THE LIBRARY CURRENTLY HAS A MIXTURE OF GNU- C and modern C CodingStyles. The gnu c usages Will be Combed out gradually.name pattern: for nonstardard names Appearing in st andard headers, we are constrained to use names that begin with underscores This is called "uglification" .The convention is:. Local and argument names:. __ [az] * Examples: __count __ix __s1 Type names and template formal-argument names: _ [AZ] [^ _] * Examples:. _Helper _CharT _N Member data and function names: _M _ * Examples:. _M_num_elements _M_initialize () Static data members, constants, and enumerations:. _S _ * Examples: _S_max_elements _S_default_valueDon't use names In The Same Scope That Difer Only In The Prefix, Eg_S_top and _m_top. See Badnames for A List of Forbidden Names. (The Most Tempting of these Seem to BE and "and" __sz "

Names Must Never Have "__" IT Would Confuse Nameunmanglers on Some Targets. Also, Never Use "__ [0-9]", Same Reason. --------------- ----------- [BY EXAMPLE] #ifndef _header_ # define _header_ 1namespace std {class gribble {public: // ctor, op =, dtor gribble () throw (); gribble (const gribble); explicit gribble (int __howmany); gribble & operator = (const gribble &); virtual ~ gribble () throw (); // argument inline void public_member (const char * __arg) const; // in-class function definitions should be restricted to one -Liners. Int one_line () {return 0} {Return Strchr (arg, 'a');} inline int three_lines (); // inline, but defined Below. // Note Indentation Template void public_template () const throw (); template void other_template (); private: class _Helper; int _M_private_data; int _M_more_stuff; _Helper * _M_helper; int _M_private_function (); enum _Enum {_S_one, _S_two}; static void _S_initialize_library ();}; // More-or-less-standard language features described by lack, not presence: # ifndef _G_NO_LONGLONG extern long long _G_global_with_a_good_long_name; // avoid ! globals # endif // avoid in-class inline definitions, define separately; // likewise for member class definitions: inline int gribble :: public_member () const {int __local = 0; return __local;} class gribble :: _ Helper {int _M_stuff; Friend Class Gribble;};} // Names Beginning with "__": Only for arguments and // local variables; never us "__" in a type name, or // within any name;

转载请注明原文地址:https://www.9cbs.com/read-88793.html

New Post(0)