首页 > 编程语言 >C++ Style Guide

C++ Style Guide

时间:2023-01-02 15:05:00浏览次数:37  
标签:Style Slice style C++ should will code Guide


Common Rules

The following style guidelines will be followed for both Slice and C++ code:

C1. Braces for compound statements will be on their own lines, at the same indentation level as the statement they are part of. Code and declarations in the brace-enclosed block will be indented one level from the braces. The only exception to this rule is for C++ namespace and Slice module blocks; because the nesting of these scopes could change in the future, if indentation was used it would have to be changed across many source files if the nesting changed. In addition, most code is contained in at least three namespaces/modules, if not more, and indenting the code to represent this would waste a great deal of space and make the code harder to read.
C2. Indentation: 4 spaces per level, no tabs.
C3. Interface, class and structure names will be in UpperCamelCase style.
C4. Function, operation and member variable names will follow lowerCamelCase style.
C5. Comments will follow Doxygen (Javadoc form) guidelines. For Slice this will apply to interface, class and struct definitions, and all operations. For C++, this will apply to classes and operations. All other comments will follow C++ standard convention, which is for "//" comments to be used except for lengthy multi-line comments.
C6. Comment the end of namespace scopes (or any code blocks long enough that would likely not be entirely visible on a typical screen) to improve maintainability / readability.
C7. All comments shall be in the English language.

Slice Style Guide

The following additional style guidelines will be followed in Asterisk SCF Slice definitions:

  1. Empty definitions will be shown by opening and closing braces at the end of the line.
  2. Slice file names will end with "If.ice" (where If is for interface) so that developers in IDEs will be able to tell when they were produced generated code. (The Slice translators use the .ice filename as the basis for the generated C++ and C# filenames.)
  3. #pragma once will be used for multiple-include protection. The Slice translators use the MCPP library for C-style preprocessing, which supports #pragma once. It provides a performance benefit over traditional #ifdef-based include guards in addition to being easier to read and maintain.

C++ Style Guide_whitespace

C++ Style Guide

To ensure compatibility with GCC, all C++ source and header files must end with a newline.

  1. In pointer and reference declarations, the & and * modifiers should be next to the type, not the variable (Foo& foo)
  2. When declaring variables, modifiers (such as const and auto) should be to written on the left side of the type (const Foo& foo).
  3. Single argument class and struct constructors should always be marked explicit, to keep the compiler from using one in an unexpected way that keeps an error from being detected at compile time.
C++ Header Files

The following additional style guidelines will be followed in C++ header files:

  1. #pragma once will be used for multiple-include protection. #pragma once has been supported in GCC since version 3.4.0, and in Visual Studio since (at least) version 6. It provides a performance benefit over traditional #ifdef-based include guards in addition to being easier to read and maintain.
  2. Forward declarations should be used when the code does not need the entire type declaration; the most common cases of this are pointer and reference variables, which only need the type name declared, but not its complete declaration.
  3. Header files should #include all other headers required to compile them; source files that #include a header file should not be required to know which other headers are required for it. Organize #include
  4. Avoid using namespace;
  5. Use lower-case "m" prefix (member) for member variables, to avoid hiding member variables with local variables in methods. This is not done for Slice class member variables, as Slice class members are always public, since Slice classes are more analogous to C++ struct than C++ class.

C++ Style Guide_header_02

C++ Source Files

The following additional style guidelines will be followed in C++ source (.cpp) files:

  1. Organize #include
  2. Flatten namespaces (if desired for convenience) here in the source file, not in header files.
  3. Variables should be initialized using constructor syntax, not assignment syntax, even for Plain-Old-Data types. This is for readability and consistency, not for performance, since modern compilers will compile them to the same object code in nearly all cases. Instead of:
int x = 1;
string str = "This is a string.";
ProxyPtr proxy = 0;

use:


int x(1);
string str("This is a string.");
ProxyPtr proxy(0);


  1. Write function calls with no whitespace between the function name and the opening parenthesis, or around the parentheses. The only whitespace should be following any commas that separate arguments, like this:

    doOperation("xyz", 2, fooPtr);

  2. Don't format statements (for, if, switch, while, etc.) as if they were function calls; a single space should appear after the keyword, like this:
if ((x + 1) > 100)
{
return y;
}
for (x = 0; x++; x < z)
{
y *= 3;
}
  1. Compound statements must always be enclosed in braces, even if the block contains only one statement. This rule helps to avoid unexpected execution paths when code is modified for debugging purposes (or any other reason). Instead of this:
if (x > 5)
return y;
while (z > 4)
z /= 2;

use this:


if (x > 5)
{
return y;
}
while (z > 4)
{
z /= 2;
}

C++ Style Guide_variables_03


Editor setup

Emacs

Save a copy of the attached file ​​asterisk-scf-style.el​​ into your site-lisp directory. Make sure you have the following in your .emacs

C++ Style Guide_header_04

(require 'asterisk-scf-style)

;; Treat .h files as C++ files
(add-to-list 'auto-mode-alist '("//.h//'" . c++-mode))

;; You may want to merge this with existing configuration in your custom-set-variables block
(setq indent-tabs-mode nil
require-final-newline t
show-trailing-whitespace t
c-default-style '((c-mode . "asterisk-scf") (c++-mode . "asterisk-scf"))
)


标签:Style,Slice,style,C++,should,will,code,Guide
From: https://blog.51cto.com/u_15747257/5983704

相关文章

  • C++笔记整理
    把自己印象笔记中所记录的一些C++知识点整合了一下,可用于面试前对C++知识的快速回顾。不过并不全,只是自己笔记中的摘要,重要的还是系统和踏实地学习。每个知识点不分顺序。1.......
  • C++ priority_queue使用方法
    以leetcode1081题为例,https://leetcode.cn/problems/number-of-orders-in-the-backlog/classSolution{public:intgetNumberOfBacklogOrders(vector<vector<int......
  • C/C++高级语言程序设计课程设计任务书[2022-01-02]
    C/C++高级语言程序设计课程设计任务书[2022-01-02]高级语言程序设计课程设计任务书课程设计名称 中文:高级语言程序设计课程设计英文:ComputerProgrammingBasicCompreh......
  • C/C++课程设计题目[2023-01-02]
    C/C++课程设计题目[2023-01-02]选题1:考勤信息管理系统某公司对员工的出勤采用计算机管理,为该公司设计一个员工考勤信息管理程序。系统包括三类用户:管理员,考勤员,普通职员......
  • C/C++通讯录管理程序[2023-01-02]
    C/C++通讯录管理程序[2023-01-02]问题描述:编写一个简单的通讯录管理程序。通讯录记录有姓名,地址(省、市(县)、街道),电话号码,邮政编码等四项。基本要求:程序应提供的......
  • C++小型公司工资管理系统[2023-01-02]
    C++小型公司工资管理系统[2023-01-02]题目14“小型公司工资管理系统设计”1、问题描述某公司需要存储雇员的编号、姓名、性别、所在部门,级别,并进行工资的计算。其中,雇......
  • C/C++停车场管理系统[2023-01-02]
    C/C++停车场管理系统[2023-01-02]项目七:停车场管理系统1、教学内容提供停车场地的管理,分为月租车和临时停车两大类。场地分为月租车停放区域和临时车辆停放区域两大块......
  • C++ string 基本用法
    一、C++string的创建方式1、string的头文件#include<iostream>//等价于C语言中的#include<stdio.h>#include<cstring>#include<string.h>//以上两种都是C语言的string头......
  • [C/C++] C++之Lambda表达式
    Lambda表达式也叫匿名函数,有时候也叫闭包(Closure)参考视频:注意视频中捕获变量部分有错误,按本博客为准1.定义[OuterVar](intx,inty)->int{returnOuterVar+......
  • C++实现向上取整
    1.使用库函数doubleceil(doublex)由于传入的参数和返回的参数都是double,所以需要手动转化代码:#include<bits/stdc++.h>usingnamespacestd;intmain(){......