孟岩ID:myan
[修改头像]
1552969次访问,排名6好友1人,关注者32
总是在思考存在的问题
myan的文章
原创 146 篇
翻译 0 篇
转载 3 篇
评论 5196 篇
最近评论
lschou520:怎么会忘记印度、日本和欧洲呢?
daijunhua:支持,中华儿女,互相护持地走!
ranzj:我只抱怨自己的努力不够。
ranzj:我毫不怀疑 SilverLight 是个“钱”途无量的玩意儿。
winvc:还有 之前已经看到过一篇署名孟岩的文章了 也是自称学计算机的 在MOP发的文章 题目是《不知名的程序员写给想学编程的朋友》(最后署名前还特别声明了下自己是初中文化全靠自学的 大哥 这样的人全国有几千万 没几个比你这种货色差的)

那文章是看的我想吐 不知道是你还是重名了 不过咋跟你这篇文章风格这么像呢 都是不懂 逻辑混乱 瞎喷
你是自己想不明白问题 但认为自己想……
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes
文章分类
收藏
    相册
    测试
    友情链接
    老赵的博客
    存档

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

    新一篇: C++大师Herb Sutter加盟微软,面对Codeproject侃侃而谈。

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

    }

    发表于 @ 2002年02月01日 16:58:00|评论(loading...)|编辑

    旧一篇: Codeproject对现任VC++.NET首席软件设计师Stan Lippman的采访

    评论

    #Soundboy 发表于2004-09-28 13:40:00  IP: 211.94.131.*
    命名规范实在是个不好把握的事情,曾经没有规范,到有了严格的规范。

    到现在又迷茫了,因为有很多规范。
    #Patrick He 发表于2004-10-06 11:38:00  IP: 219.238.229.*
    这是C++ Standard Library的style。

    这里有一份C++的programming style,可以进一步参考。
    http://geosoft.no/development/cppstyle.html
    #施工队 发表于2004-10-09 09:36:00  IP: 218.0.234.*
    命名规范应该是很个性的东西,谁都可以有自己的规范
    #duyanning 发表于2005-01-19 17:23:00  IP: 61.150.11.*
    # 回复:GNU的C++代码书写规范,C语言之父Dennis Ritchie亲自修订 2004-09-28 1:40 PM Soundboy
    命名规范实在是个不好把握的事情,曾经没有规范,到有了严格的规范。
    到现在又迷茫了,因为有很多规范。
    ====================================
    同感,这些规范竟然彼此矛盾到这边强烈推荐的那边却被点名禁止!
    #heyuqi 发表于2005-04-18 19:43:00  IP: 202.198.169.*
    Geotechnical Software Services有这么一条对于Guidelines的观点的:

    General Recommendations
    1. Any violation to the guide is allowed if it enhances readability.
    2. The rules can be violated if there are strong personal objections against them.

    很多时候规范没有强制性的,只要能让别人一看就清楚你所定义的变量和函数,这条规范就是我们要遵循的规范!
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © 孟岩