some cpp conventions

Name and Type

1.    The scope of a derived class is treated as nested in its base class when it comes to name lookup. (help understand hiding) 
2.    Decltype yields the corresponding type for a VARIABLE and a reference for assignable EXPRESSION, and a value for non-assignable EXPRESSION. e.g. 
decltype(ref_int) //int & 
decltype(ref_int + 0) //int  
decltype(val_int) // int  
decltype ((val_int)) // int & 
3.    In implicit type conversion, char is promoted to int, not short. 
  
Pointer and Reference 
4.    A past-the-end pointer is not an invalid pointer. Though it also should not be dereferenced, it can be used to compare with the other pointers pointing to the same array. In contrast, comparing two pointers pointing to two unrelated object is UB. 
5.    A reference is always treated as the object it refers, such as, sizeof(ref), typeid(ref), etc, all info is about the underlying object, with only one exception: decltype(ref) gives the reference type.  
6.    A pointer got from new Type[0] is not a nullptr but a pass-the-end pointer, it serves just like vec.end() when the vector is empty. 
7.    Use std::less<T> ()(a, b) instead of ‘<’ when writing a template. Because if T are pointers and they are not pointing to the same array, using ‘<’ on them is UB. 
  
Ctor/Dtor 
8.    A ctor can serve in static_cast.   
9.    Inherited ctors inherits explicit/constexpr properties. It does not inherit default arguments. Instead it gets multiple ctors in which each parameter with a default argument is successively omitted. 
10.    If a class defines a dtor, even if it uses =default, the compiler will not synthesize a move operation for that class. 
11.    The reason why the member initialization order depends on the definition order rather than the order in which they appear in the initialize list is, dtor must reverse the order and there is no way for a dtor to know the order in which you initialized the members. 
12.    When you use ‘=default’ to force a compiler to generate a default move ctor for your class, but the compiler cannot (e.g. not all members are movable), the compiler will turn it into ‘=deleted’. 
  
  
Move 
13.    Compiler uses the copy ctor for move if you do not provide one. But, if you refuse to provide any copy-control members (assignment operator, copy ctor, etc) and all non-static data members are movable, you will get a real synthesized move ctor from compiler. 
14.    IO stream object can be moved. 
15.    After move operation, do not assume any state of the moved obj. e.g. People may assume  a moved vector has size() as 0 and the call of empty() on it return true. But it is not necessarily the case: perhaps the underlying whole pimpl object was moved away, the call of empty() or size() are forwarded to a null pointer. 
16.    The std::containers only move its elements when the elements have a nonexcept move ctor. Otherwise it cannot guarantee exception safety when moving the container. (Is this part of the reason that VC11 does not support nonexcept keyword yet?) 
17.    Classes that define a move ctor/assignment operator must also define their own copy operations; otherwise, those copy operations are defined as $deleted$ by default. 
18.    You can use a move_iterator when you want to move all elements from a container to another. 
  
  
Functions 
19.    Using directives create an overlord set of functions from different namespaces: 
using namespace sp1; //has print(int) 
using namespace sp2;//has print(double) 
… 
print(1); //sp1::print 
print(1.2);//sp2::print 
… 
20.    When looking for a best match for overloaded functions, the non-member (normal) functions and member functions race together. 
21.    Each function default parameter can have its default specified only once. Any subsequent declaration can add a default only for a parameter that has not previously had a default specified. 
int f(int , int = 2); int f (int =1, int);// OK, incremental specification  
22.    Static member can be used as a default argument before definition and even declaration (but, ofc, the declaration should exist). 
  
Member functions 
23.    When using = default, (as with other member fuctions), if it appears in class, the default ctor will be inlined; if it appears outside the class, it is not by default. 
24.    Besides const, you can place a reference qualifier on a member function to indicate this pointer is pointing to a rvalue or lvalue. E.g. 
void mem_func(int ) const && 
means, mem_func(const && this_obj, int); The && here has nothing to do with move semantic. It avoids some silly statement like : str0 + str1 = “abc”. Operator=() should certainly behave differently  when *this is a rvalue or a lvalue. 
Btw, it is nice but not supported by many compilers yet. 
25.    =default is used when defining a function while =deleted is used when declaring a function. 
26.    We can provide a definition for pure virtual function, but we can only write it outside the class. 
27.    Virtual function can have default argument, but the one used is determined by its static type. No RTTI for this kind of work. 
28.    Member functions can also be defined as volatile, only volatile member functions can be called on volatile objects. 
  
Lambda 
29.    Lambdas are function objects with deleted default ctor, deleted assignment operators and default dtor and it has a member ‘operator()’ which is a const member by default. Whether it has a defaulted or deleted copy/move ct depends on the captured data members. 
30.    The value (as opposed to reference) captured by a lambda is const, you cannot change its value. This is because by default lambda is a const object and the captured values are its data members. You can override its constness by adding a mutable keyword following lambda. 
31.    In a lambda function, when the return type is not specified, the function body is just a return statement, the return type is inferred from it, or it is void. 
32.    We can omit parameter list and return type of lambda. So a simplest form and fully spelled formed should be: 
auto f = []{}; 
auto f = [captures](parameters) mutable -> returntype {} 
  
Template 
33.    Template member function cannot be virtual. 
34.    We can make a template type parameter a friend.  
template <typename Type> class Bar{friend Type;} 
It is OK when it is instantiated with a built-in type. 
35.    Instantiation also happens (if not yet) when assigning the template function to a function pointer / reference. 
  
  
Access control 
36.    Other than narrowing down the access control during inheritance (by public, protected, private derivation), we have a way to enlarge it: 
class Derived: private Base{ 
public: 
     using Base::protected_member; 

Now, protected_member is public. 
37.    The protected/public member can be accessed from derived class, even if it uses private derivation. The access control of private derivation has effect on how to use the derived class, not the base class. 
38.    Normal access controls apply to pointers to members. 
  
  
STL: 
39.    The STL containers now have a cbegin() and cend, which are specifically designed for auto: 
auto citer = vec.cbegin()//always const_iter regardless the container type. 
40.    Std::swap() only invalidates the iterators on std::string. It does not invalidate iterators on other types of containers. They are pointing to the same elements in the swapped container. 
41.    Forward_list does not have size(). (I do not know why exactly) 
42.    Forward_list is different, it inserts_after, etc, and uses an off-the-beginning iterator. 
43.    String library now have string-to-numeric converting functions. 
44.    Reverse_iterator.base() does not yield the adjacent position rather than the same one. 
45.    Use make_pair, make_tuple to let compiler deduce types for you. 
46.    Iterators for std::set, either cons_iterator or (non-const) iterator are both const_iterators. 
47.    Bitset subscription operator [] counts from right to left. 
48.    We can use std::function to store a function directly but cannot when it has overloaded versions: 
  int func(int, int); 
int func(double, double); 
std::function<int(int, int)> f = func; //which? 
Use a pointer or reference to deambiguate: 
int (&funcii )(int , int) = func; 
std::function<int(int, int)> f =  funcii; 
49.    The seekg, seekp, tellg, tellp uses the same marker on fstream and stringstream. 
  
  
Other 
50.    Sizeof (*a_null_pointer) is valid. Sizeof (char) is guaranteed to be 1, always. 
51.    An enumerator value need not to be unique: enum {A = 1, B = 1, C = 1}; //it’s OK 
52.    A definition of a nested class can be outside the enclosing class. 
53.    A local class is legal but must be completely defined in the class body. 
54.    A constexpr is not required to return a const expression always 
55.    A constexpr is not required to be defined when the literal value is used. But it still needs a definition when the run time properties (like address operator &) are required. 
56.    Friendship is not inherited. But, a friend of Base class can access the members of Base part of a Derived object.  
57.    Virtual base classes are always constructed prior to non-virtual base classes regardless of where they appear in the inheritance hierarchy. 
58.    noexcept  specifier should not appear in a typedef or type alias(I am not sure why either.) 
59.    The compiler will generate (when needed) the nonexcept ctor / copy-control members / dtor if the corresponding operations for all of its members promise not to throw. 
60.     A function pointer that not specifying noexcept can point to noexcept functions, but NOT otherwise.

时间: 2024-10-12 15:55:29

some cpp conventions的相关文章

函数模版和主函数分别在.h .cpp中(要包含.cpp)

Complex.h #pragma once #include<iostream> using namespace std;//这句还必须加,要不然致错,不懂为啥呢 template <typename T> class Complex { public: Complex( T a); ~Complex(); Complex operator + (Complex & c1); public: friend ostream & operator << &

QThread的源码(直接搜索&quot;thread.cpp&quot;即可,或者在github里搜)

void QThread::run() { (void) exec(); } int QThread::exec() { Q_D(QThread); QMutexLocker locker(&d->mutex); d->data->quitNow = false; if (d->exited) { d->exited = false; return d->returnCode; } locker.unlock(); QEventLoop eventLoop; i

Caffe 源碼閱讀(三) caffe.cpp

從main函數說起: 1.gflags庫中爲main函數設置usage信息 是google的一個開源的處理命令行的參數的庫.在使用命令行參數的文件夾文件中(源文件或頭文件),首先使用以下定義語句進行變量的定義. DEFINE_int32, DEFINE_int64, DEFINE_bool等, 語法爲:DEFINE_int32(name,default_value,"description").接着你就可以使用FLAGS_name變量了,這些變量的值則是由命令行參數傳遞,無則爲默認值,

[hge] distort.h distort.cpp

荡漾   --写在开头 在两个文件组合起来要实现的功能就是使得一个画片图水波般荡漾 首先来看头文件中的东西 Chapter I: distort.h Part Ⅰ:attributes private: hgeDistortionMesh(); // 构造函数,话说放在 private 是为了屏蔽这个接口,使得初始化过程中必须有参数 static HGE *hge; // 引擎指针 hgeVertex *disp_array; // 顶点数组 int nRows, nCols; // 行数.列数

map——映射(message.cpp)

信息交换 (message.cpp) [题目描述] Byteland战火又起,农夫John派他的奶牛潜入敌国获取情报信息. Cow历尽千辛万苦终于将敌国的编码规则总结如下: 1 编码是由大写字母组成的字符串. 2 设定了密字.加密的过程就是把原信息的字母替换成对应密字. 3 一个字母有且仅有一个对应密字,不同字母对应不同密字. 如今,Cow终于获取了敌国发送的一条加密信息和对应的原信息.Cow如下破解密码:扫描原信息,对于原信息中的字母x,找到它在加密信息中的对应大写字母y,且认为y是x的密字.

Identify Memory Leaks in Visual CPP Applications(VLD内存泄漏检测工具)

原文地址:http://www.codeproject.com/Articles/1045847/Identify-Memory-Leaks-in-Visual-CPP-Applications 基于CPOL License Identify Memory Leaks in Visual CPP Applications Visual Leak Detector (VLD) is an easy to use memory leak detection system. The installat

[C/CPP系列知识] C++中extern “C” name mangling -- Name Mangling and extern “C” in C++

http://www.geeksforgeeks.org/extern-c-in-c/ C++函数重载(function overloading),但是C++编译器是如何区分不同的函数的呢?----是通过在函数名是加些信息来区不同的函数,即所谓的Name Mangling.C++标准并没有对name mangling技术,各个编译器可以添加不同的信息. 考虑下面的函数 int f (void) { return 1; } int f (int) { return 0; } void g (voi

如何在安卓环境下自动编译所有cpp文件

正常情况下,需要在Android.mk文件下面一个一个手动添加cpp文件,如果文件较多,这样就太麻烦了. 解决办法如下: 把Android.mk文件里面的这段代码: LOCAL_SRC_FILES := hellocpp/main.cpp ../../Classes/AppDelegate.cpp 改为: FILE_LIST := hellocpp/main.cpp FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp) LOCAL_

c/cpp中怎样切割字符串,相似于split的功能

在python中,假设要求当前时间的unix时间戳,我特别喜欢这么用: import time timestr = time.time() timestamp = int(timestr.split('.')[0]) 这里的split函数,我非常喜欢,在java.c#和python中都有,非常方便,不用操心踩地雷,可是C/CPP中,就没有了,这点比較遗憾. 假设要处理一个字符串型的"192.168.1.254",想把每一个字段都分开,怎么办呢,C标准库中有函数strtok()的实现,能