You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -136,8 +129,6 @@ See [C++ Programmer's Toolbox](#c-programmers-toolbox) for details.
136
129
1.**Copy Constructor and Move Constructor**
137
130
* If you provide copy constructor and/or move constructor,
138
131
provide the corresponding `operator=` overload as well.
139
-
* Class should be disable copy/move by default.
140
-
141
132
2.**Inheritance**
142
133
* All inheritance should be **public**. If you want to do
143
134
private inheritance, you should be including an instance of
@@ -170,17 +161,14 @@ See [C++ Programmer's Toolbox](#c-programmers-toolbox) for details.
170
161
171
162
1.**Parameters**
172
163
* 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.
176
167
2.**Default Argument**
177
-
* Not recommended for readability/ABI issue. **Do not use** unless
168
+
* Not recommended for readability issue. **Do not use** unless
178
169
you have to.
179
-
180
170
3.**Trailing Return Type Syntax**
181
171
* When in lambda. Period.
182
-
* When using template, and the output type should be deducted by input types.
183
-
184
172
4.**Return Value of Complicated Type**
185
173
186
174
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.
201
189
}
202
190
```
203
191
204
-
Also we could return a smart pointer for complex type, such as unique_ptr<std::vector<int>>.
205
-
206
192
I think this pattern should be discouraged in favor
207
193
of
208
194
[return value optimization](cases/return_value_optimization.md),
209
195
because the latter is not only less error-prone, but also **much
210
196
more readable**.
211
-
197
+
212
198
## Exceptions
213
199
214
200
1.**DO NOT** throw exceptions. Returns error code, and let the upper
@@ -273,26 +259,6 @@ I think for naming we should stick
273
259
to
274
260
[Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html#Naming).
275
261
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
-
296
262
## C++ Programmer's Toolbox
297
263
298
264
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.
311
277
implementation (e.g. boost) if you have a choice. This helps
312
278
reduce both runtime dependencies and compile-time dependencies,
313
279
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`
315
282
1.`<stdint.h>` defines types like `int16_t`, `uint32_t`, `int64_t`,
316
283
etc. You should always use those in preference to short, unsigned
317
284
long long and the like, when you need a guarantee on the size of
318
285
an integer.
319
-
320
286
1. Macros damage readability. **Do not use** them unless the benefit
321
287
is huge enough to compensate the loss.
322
-
323
288
1.`auto` is permitted when it promotes readability.
324
-
325
289
1.`switch` cases may have scopes:
326
290
327
291
```c++
@@ -337,30 +301,5 @@ format and focus on more important stuff.
337
301
default: {
338
302
assert(false);
339
303
}
340
-
}
304
+
}
341
305
```
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.
0 commit comments