`
灵动的水
  • 浏览: 195846 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

GNU的C++代码书写规范,C语言之父Dennis Ritchie亲自修订

阅读更多
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 Coding
Standard, which can be referenced here:

http://www.gnu.ai.mit.edu/prep/standards_toc.html

ChangeLog entries for member functions should use the
classname::member function name syntax as follows:

1999-04-15     Dennis Ritchie     <dr@att.com>

* 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 they gain experience.

02. Operator names and parentheses
     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<typename T>
       void
       template_function(args)
       { }
         -NOT-
     template<class T>
     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<typename _CharT, typename _Traits>
       class basic_ios : public ios_base
       {
       public:
         // Types:
       };
     -NOT-
     template<class _CharT, class _Traits>
     class basic_ios : public ios_base
       {
       public:
         // Types:
       };
     -NOT-
     template<class _CharT, class _Traits>
       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 be
aligned to the same margin, increasing readibility.

10. Invocation of member functions with "this->"
      For non-uglified names, use this->name to call the function.

     this->sync()
     -NOT-
     sync()

The library currently has a mixture of GNU-C and modern C++ coding
styles.     The GNU C usages will be combed out gradually.

Name patterns:

For nonstandard names appearing in Standard headers, we are constrained
to use names that begin with underscores.     This is called "uglification".
The convention is:

     Local and argument names:     __[a-z].*

       Examples:     __count     __ix     __s1    

     Type names and template formal-argument names: _[A-Z][^_].*

       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_value

Don't use names in the same scope that differ only in the prefix,
e.g. _S_top and _M_top.     See BADNAMES for a list of forbidden names.
(The most tempting of these seem to be and "_T" and "__sz".)

Names must never have "__" internally; it would confuse name
unmanglers on some targets.     Also, never use "__[0-9]", same reason.

--------------------------

[BY EXAMPLE]
      
#ifndef     _HEADER_
#define     _HEADER_ 1

namespace 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 }

       int
       two_lines(const char* arg)
         { return strchr(arg, 'a'); }

       inline int
       three_lines();     // inline, but defined below.

       // note indentation
       template<typename _Formal_argument>
         void
         public_template() const throw();

       template<typename _Iterator>
         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 use "__" in a type name, or
//      within any name; never use "__[0-9]".

#endif /* _HEADER_ */

namespace std {

     template<typename T>     // notice: "typename", not "class", no space
       long_return_value_type<with_many, args>    
       function_name(char* pointer,                  // "char *pointer" is wrong.
       char* argument,
       const Reference& ref)
       {
         // int a_local;     /* wrong; see below. */
         if (test)
         {
      nested code
         }
      
         int a_local = 0;     // declare variable at first use.

         //     char a, b, *p;      /* wrong */
         char a = 'a';
         char b = a + 1;
         char* c = "abc";     // each variable goes on its own line, always.

         // except maybe here...
         for (unsigned i = 0, mask = 1; mask; ++i, mask <<= 1) {
      // ...
         }
       }
    
     gribble::gribble()
     : _M_private_data(0), _M_more_stuff(0), _M_helper(0);
     { }

     inline int
     gribble::three_lines()
     {
       // doesn't fit in one line.
     }

}
分享到:
评论

相关推荐

    C++程序书写规范标准.doc

    C++程序书写规范标准是软件开发中不可或缺的一部分,好的编码风格可以提高代码的可读性、可维护性和可靠性。本文档旨在总结和统一C++程序书写规范,以便提高编程效率和代码质量。 一、前言 在商品化软件开发中,...

    GNU c++

    GNU C++ 是一个基于GCC(GNU Compiler Collection)的C++编译器,它是Linux操作系统中的核心组件之一。在Linux环境下进行C++编程,不仅涉及语言本身,还涵盖了许多与操作系统、开发工具和库相关的知识。 ### 1. C++...

    GNU编码规范-C语言

    GNU编码规范是软件开发中的一套重要指导原则,特别是针对C语言编程的项目。这套规范旨在提高代码的可读性、可维护性和可移植性,确保软件的质量和一致性。以下是对GNU编码规范主要方面的一些详细解释: 1. 引用私有...

    GNU C++ for Linux 编程技术

    GNU C++ for Linux 编程技术分三部分

    GNU编码标准.doc

    对于使用C语言的项目,GNU编码标准强调清晰、简洁的代码风格,避免复杂的表达式和不必要的嵌套,优先选择易于理解和维护的代码结构。 #### 命名变量和函数 变量和函数的命名应该直观且具有描述性,避免使用过于...

    GNU C++ for Linux

    **GNU C++ for Linux** GNU C++ 是一个广泛使用的C++编译器,它是GNU Compiler Collection (GCC) 的一部分,专为Linux操作系统设计。GCC是一个开源的编译器套件,支持多种编程语言,包括C、C++、Objective-C、...

    C++编译器 C语言 专业版本

    C语言是C++的基础,由Dennis Ritchie于1972年在贝尔实验室创造,它以其高效、灵活和接近机器特性而闻名。C++则是在C语言的基础上发展起来的,由Bjarne Stroustrup于1983年引入类和面向对象编程概念,从而成为一种更...

    The GNU C++ Library - GNU.epub

    GNU C++ Library手册 (2019版).

    C语言源代码规范格式化工具(绿色免费版)

    C语言源代码规范格式化工具是一款专为C语言开发者设计的实用软件,旨在帮助程序员自动整理和格式化源代码,使其符合特定的编程规范和风格。这款工具特别强调其绿色免费的特点,意味着用户无需安装即可直接运行,同时...

    C语言代码自动整理工具

    2. **代码格式化**:对代码进行重新排列,如函数定义、变量声明等,使其符合特定的编码规范,如K&R、Allman或GNU样式。 3. **括号匹配**:确保括号正确对齐,消除不必要的嵌套,提升代码的视觉效果。 4. **命名...

    GNU Linux C++编程

    《GNU Linux C++编程》是一本专注于在Linux环境下进行C++程序开发的电子书,它涵盖了C++语言的基础以及在Linux系统上的高级应用。Linux作为一款开源操作系统,为C++开发者提供了丰富的工具和库,使其成为开发高效、...

    The GNU C++ Library Manual

    《GNU C++ Library Manual》是C++程序员的重要参考资料,它详细介绍了GNU环境下,特别是G++编译器下C++标准库的实现与接口。这本书涵盖了从C++98到C++17等多个版本的标准,是开发者理解和使用GNU C++库的关键工具。 ...

    GNU编码规范

    在编程语言选择方面,GNU编码规范倾向于推荐使用C语言进行程序设计,同时也讨论了与其它实现的兼容性问题、非标准特性的使用、标准C与C语言标准发布前的差异以及条件编译等方面的内容。 关于程序行为,规范涵盖了非...

    深入学习:GNU C++ for Linux 编程技术.part1

    深入学习:GNU C++ for Linux 编程技术 part1  非常好的书,自己转换的,独一无二! 文件比较大,分为四份上传。

    Tom_Swans_GNU_C++_for_Linux.rar

    6. **C++模板**:模板是C++的特色之一,它允许创建泛型代码,提供了一种方式来编写不依赖特定类型的代码。Tom Swans可能会详细介绍模板类和模板函数的使用。 7. **异常处理**:C++的异常处理机制允许程序在运行时...

    GNU手册(C语言)

    ### GNU手册(C语言)知识点概述 #### 一、引言 GNU C Compiler (GCC) 是一个开源编译器套件,支持多种编程语言。它最初由Richard Stallman于1987年发起,旨在为GNU项目提供一个高质量的编译器。随着时间的发展,...

    深入学习:GNU C++ for Linux 编程技术.part2

    深入学习:GNU C++ for Linux 编程技术 part2  非常好的书,自己转换的,独一无二! 文件比较大,分为四份上传。

    深入学习:GNU C++ for Linux 编程技术.part3

    深入学习:GNU C++ for Linux 编程技术.part3 非常好的书,自己转换的,独一无二! 文件比较大,分为四部分上传。

Global site tag (gtag.js) - Google Analytics