Skip to content

Commit 4c924de

Browse files
authored
Merge pull request #2 from PaddlePaddle/revert-1-master
Revert "Add more rules based on my thinking."
2 parents de72ba3 + deaa38e commit 4c924de

File tree

3 files changed

+12
-150
lines changed

3 files changed

+12
-150
lines changed

README.md

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ See [C++ Programmer's Toolbox](#c-programmers-toolbox) for details.
5656
2. **Forward Declarations**
5757

5858
See [Google Style Guide](https://google.github.io/styleguide/cppguide.html#Forward_Declarations).
59-
60-
An exception for this rule is:
61-
62-
* Use a Private Data Class for exposed API, especially for shared library.
63-
* look more details in [Expose API/Shared Library].
64-
6559
3. **Inline Functions**
6660

6761
**Rule of thumb**: do not inline a function if it is more than 10
@@ -91,11 +85,10 @@ See [C++ Programmer's Toolbox](#c-programmers-toolbox) for details.
9185
## Namespaces
9286

9387
1. **Unnamed Namespace**
94-
* ** Do not** Use them in `.cpp`/`.h` files. Using `static` for hidding
95-
symbols in source files. The anonymous namespace will generate a namespace
96-
when compiling, and could generate a different namespace name each time.
97-
       It will breaks the ABIs, make dubug harder.
98-
   *   Sometimes you may want to expose internal functions or
88+
* Use them in `.cpp` file to hide functions or variables you do
89+
not want expose.
90+
* **Do not** use them in `.h` files.
91+
* Sometimes you may want to expose internal functions or
9992
variables just for testing purpose. In this case it is better
10093
to declare them in
10194
[internal-only namespaces](cases/internal_only_namespaces.md).
@@ -136,8 +129,6 @@ See [C++ Programmer's Toolbox](#c-programmers-toolbox) for details.
136129
1. **Copy Constructor and Move Constructor**
137130
* If you provide copy constructor and/or move constructor,
138131
provide the corresponding `operator=` overload as well.
139-
* Class should be disable copy/move by default.
140-
141132
2. **Inheritance**
142133
* All inheritance should be **public**. If you want to do
143134
private inheritance, you should be including an instance of
@@ -170,17 +161,14 @@ See [C++ Programmer's Toolbox](#c-programmers-toolbox) for details.
170161

171162
1. **Parameters**
172163
* Ordering: Input and then Output.
173-
* Either use const references or pointer for non-fundamental types. The stl types are not fundamental types.
174-
* Use **const reference** for read only parameter. Use **pointer** for writable parameter.
175-
164+
* All **references** must be **const**, and this is the recommended
165+
way to pass parameters.
166+
* For output parameters, pass pointers.
176167
2. **Default Argument**
177-
* Not recommended for readability/ABI issue. **Do not use** unless
168+
* Not recommended for readability issue. **Do not use** unless
178169
you have to.
179-
180170
3. **Trailing Return Type Syntax**
181171
* When in lambda. Period.
182-
* When using template, and the output type should be deducted by input types.
183-
184172
4. **Return Value of Complicated Type**
185173

186174
Returning values of simple types such as `int`, `int64_t`, `bool`
@@ -201,14 +189,12 @@ See [C++ Programmer's Toolbox](#c-programmers-toolbox) for details.
201189
}
202190
```
203191

204-
Also we could return a smart pointer for complex type, such as unique_ptr<std::vector<int>>.
205-
206192
I think this pattern should be discouraged in favor
207193
of
208194
[return value optimization](cases/return_value_optimization.md),
209195
because the latter is not only less error-prone, but also **much
210196
more readable**.
211-
197+
212198
## Exceptions
213199

214200
1. **DO NOT** throw exceptions. Returns error code, and let the upper
@@ -273,26 +259,6 @@ I think for naming we should stick
273259
to
274260
[Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html#Naming).
275261

276-
277-
## Shared Library/API
278-
279-
* Please use C-API as the exposed API for most library.
280-
* Because C-API has a standard ABI in many operating system. All standard C compiler
281-
will compile same C function/variable to same symbol in asm.
282-
283-
* Try to use void\* for C++ object instance. Try to use function pointer simulate
284-
member function.
285-
286-
* [Example](cases/c_api_for_cpp.md)
287-
288-
* If you really want to expose CPP API, the following rules should be followed.
289-
* Do not use virtual method.
290-
* The virtual method will change the method name to an offset in vtable. And the offset is
291-
easily changed if the header could be changed. Then break the ABI compatibility.
292-
* Another guide for virtual method in API is that always creates new virtual method at the END OF CLASS declaration. And do not change older declarations. It will get method as `create`, `createEx`, `createEx2`, `...`, and it seems the virtual method is not necessary for this situations.
293-
* Use [private data class](https://en.wikipedia.org/wiki/Private_class_data_pattern), instead of directly declare member field in private.
294-
295-
296262
## C++ Programmer's Toolbox
297263

298264
There are many available tools for C++, and it is always good to have
@@ -311,17 +277,15 @@ format and focus on more important stuff.
311277
implementation (e.g. boost) if you have a choice. This helps
312278
reduce both runtime dependencies and compile-time dependencies,
313279
and it promotes readability.
314-
280+
1. Use `const` whenever it makes sense. With C++11, `constexpr` is a
281+
better choice for some uses of `const`
315282
1. `<stdint.h>` defines types like `int16_t`, `uint32_t`, `int64_t`,
316283
etc. You should always use those in preference to short, unsigned
317284
long long and the like, when you need a guarantee on the size of
318285
an integer.
319-
320286
1. Macros damage readability. **Do not use** them unless the benefit
321287
is huge enough to compensate the loss.
322-
323288
1. `auto` is permitted when it promotes readability.
324-
325289
1. `switch` cases may have scopes:
326290

327291
```c++
@@ -337,30 +301,5 @@ format and focus on more important stuff.
337301
default: {
338302
assert(false);
339303
}
340-
}
304+
}
341305
```
342-
343-
1. Use smart_pointer as much as you can. Try unique_ptr rather than shared_ptr.
344-
345-
* Smart Pointer is a better way to handle memory alloc/free then manually new/delete.
346-
* especially in multi-thread situation.
347-
* unique_ptr is much lighter than shared_ptr.
348-
* shared_ptr maintain a mutex, a reference count. Create and destory a shared_ptr could be a little bit slower than unique_ptr.
349-
* unique_ptr is better for multi-thread condition. It ensures that there are no other thread using this data right now.
350-
* Use unique_ptr<int[] > rather than vector<int> for array that will not grow.
351-
* unique_ptr is easier to return.
352-
* Use weak_ptr to avoid circle reference in shared_ptr.
353-
354-
1. Use **const** as much as you can. With C++11, `constexpr` is a
355-
better choice for some uses of `const`
356-
357-
358-
* It will let compiler to check whether program is correct or not, rather than die in runtime.
359-
* Not just for parameter, but also for member function.
360-
* **mutalbe** key word may be used in this situation.
361-
362-
1. Use standard library as much as you can.
363-
364-
* **Do not** write a sort algorithm except you know what you are doing. For most situation, **std::algorithm** is your friend. And the implementation in **std** is good in general.
365-
* Use **std::thread** rather than pthread. Because C++ std library should be avaliable for most platform.
366-

cases/c_api_for_cpp.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

cases/some_examples_will_break_abi.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)