之前博客《谷歌C++编程规范笔记》整理了一些关于C++ Style方面的东西,看的是中文版本的。
但是今天翻阅英文版本的,在最后,发现了 Google C++ Style 关于windows的。
Windows programmers have developed their own set of coding conventions, mainly derived from the conventions in Windows headers and other Microsoft code.
We want to make it easy for anyone to understand your code, so we have a single set of guidelines for everyone writing C++ on any platform.
It is worth reiterating a few of the guidelines that you might forget if you are used to the prevalent Windows style:
Do not use Hungarian notation (for example, naming an integer iNum).
不要使用匈牙利命名法
Use the Google naming conventions, including the .cc extension for source files.
使用Google的命名规则,包括使用.cc后缀命名源文件
Windows defines many of its own synonyms for primitive types, such as DWORD, HANDLE, etc.
It is perfectly acceptable, and encouraged, that you use these types when calling Windows API functions.
可以使用windows定义的符号,如DWORD HANDLE等
Even so, keep as close as you can to the underlying C++ types. For example, use const TCHAR * instead of LPCTSTR.
使用const TCHAR*代替LPCTSTR
When compiling with Microsoft Visual C++, set the compiler to warning level 3 or higher, and treat all warnings as errors.
把警告当做错误来处理
Do not use #pragma once; instead use the standard Google include guards. The path in the include guards should be relative to the top of your project tree.
In fact, do not use any nonstandard extensions, like #pragma and __declspec, unless you absolutely must.
Using __declspec(dllimport) and __declspec(dllexport) is allowed;
however, you must use them through macros such as DLLIMPORT and DLLEXPORT, so that someone can easily disable the extensions if they share the code.
不要使用#pragma;可以使用__declspec(dllimport)和__declspec(dllexport),但是必须通过定义成宏来使用DLLIMPORT and DLLEXPORT
However, there are just a few rules that we occasionally need to break on Windows:
Normally we forbid the use of multiple implementation inheritance; however, it is required when using COM and some ATL/WTL classes.
正常情况下我们是禁止多重继承的,但是对于COM和一些ATL/WTL类,如果需要可以采用多重继承。
You may use multiple implementation inheritance to implement COM or ATL/WTL classes and interfaces.
Although you should not use exceptions in your own code, they are used extensively in the ATL and some STLs, including the one that comes with Visual C++.
When using the ATL, you should define _ATL_NO_EXCEPTIONS to disable exceptions.
You should investigate whether you can also disable exceptions in your STL, but if not, it is OK to turn on exceptions in the compiler.
(Note that this is only to get the STL to compile. You should still not write exception handling code yourself.)
The usual way of working with precompiled headers is to include a header file at the top of each source file, typically with a name like StdAfx.h or precompile.h.
使用预编译的话,包含的头文件要放在头文件的最前头。
To make your code easier to share with other projects, avoid including this file explicitly (except in precompile.cc), and use the /FI compiler option to include the file automatically.
Resource headers, which are usually named resource.h and contain only macros, do not need to conform to these style guidelines.
资源文件可以命名为resource.h,并且只能定义一些宏。