Summary of new features in C ++ 11

New features in C ++ 11

  1. The auto keyword (C ++ 11) range-based for loop (C ++ 11). The pointer nullptr (C ++ 11)
  2. C ++ dynamic memory management
  3. Serial container array forward_list;
  4. Inheritance and polymorphism: final override
  5. delete: Do not generate the default member function
  6. default: Force the compiler to generate default member functions
  7. Smart pointers: unique_ptr, shared_ptr, weak_ptr
  8. Hash structured associative container: unordered series

Unified initialization

The standard in C ++ 98 allows the use of curly braces {} to set a uniform list initial value for array elements

int array1[] = {1,2,3,4,5};
int array2[5] = {0};

This cannot be initialized for vector containers

vector<int> v{1,2,3,4,5};

It can't be compiled, so every time you define a vector, you need to define the vector first, and then use a loop to assign an initial value to it, which is very inconvenient.
C ++ 11 expands the scope of the list enclosed in braces (initialization list), making it available for all built-in types and user-defined types. When using the initialization list, you can add an equal sign (=), also Can not be added .

Built-in type curly braces {} initialization

	// 内置类型变量
	int x1 = { 10 };
	int x2{ 10 };
	int x3 = 1 + 2;
	int x4 = { 1 + 2 };
	int x5{ 1 + 2 };
	int x6{ x1 + x2 };
	// 数组
	int arr1[5] {1, 2, 3, 4, 5};
	int arr2[]{1, 2, 3, 4, 5};
	// 动态数组,在C++98中不支持
	int* arr3 = new int[5]{1, 2, 3, 4, 5};
	// 标准容器
	vector<int> v{ 1, 2, 3, 4, 5 };
	map<int, int> m{ { 1, 1 }, { 2, 2, }, { 3, 3 }, { 4, 4 } };

Single custom type list initialization

class Point
{
public:
	Point(int x = 0, int y = 0) 
		: _x(x), _y(y)
	{}
private:
	int _x;
	int _y;
};
int main()
{
	//两种初始化都可以
	Point p{ 1, 2 };
	Point p2(1, 2);
	return 0;
}

Initialization of multiple custom type lists

If you want to support list initialization for multiple objects, you need to add a constructor with an initializer_list type parameter to the class (template class) . Note: initializer_list is a system-defined class template. There are three main methods in this class template: begin (), end () iterator and size () method to get the number of elements in the interval.

template<class T>
	class vector
	{
	public:
		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{}
		vector(initializer_list<T>l)
			:_start(new T[l.size()])
		{
			_finish = _start;
			for (auto e : l)
			{
				*_finish++ = e;
			}
		}
	private:
		T* _start;
		T* _finish;
		T* _endofstorage;
	};

Variable type deduction

auto derivation

auto summary

decltype type deduction

The prerequisite for the use of auto is: the type declared by auto must be initialized, otherwise the compiler cannot derive the actual type of auto

template<class T1,class T2>
//返回的时候,返回T1和T2都不行,应该返回T1+T2后的值的类型
auto Add(const T1& left, const T2& right)
{
	return left + right;
}

If the actual type of the result after addition can be used as the return type of the function, there will be no error, but this requires the program to run to know the
actual type of the result , that is, RTTI (Run-Time Type Identification).

  • typeidCan only view the type and not define the type with its result class
  • dynamic_castCan only be used in inheritance systems containing virtual functions
So new keywords are needed

decltype is the type used to define variables based on the actual type of the expression , for example: return value type deduction

//此处auto为占位符
//返回值类型追踪
auto  Add( T1& left,  T2& right)->decltype(left+right)
{
	return left + right;
	//ret = left + right;
}

int main()
{
	int a = 1;
	double b = 2.0;
	cout<< Add(a, b);
	return 0;
}

Range for loop

Range for

final和override

Summarized in polymorphism

Smart pointer

Smart pointer summary

New containers-static array array, forward_list and unordered series

Delegate constructor

The so-called delegation constructor: refers to a class construction method in which the delegation function delegates the constructed task to the target constructor.
The constructor that calls the "benchmark version" in the initialization list is called the delegate constructor, and the called "benchmark version" is called the target constructor.
Note: The constructor cannot "delegate" and use the initialization list at the same time.

class Info {
public:
	Info() 
		: _type(0)
		, _name('a')
	{
		InitRSet();
	}
	Info(int type) 
		:Info()
		//不能再在初始化列表中初始化其它的成员变量
		//,_type(type)
	{
		//InitRSet();
		_type = type;
	}
	Info(char type) 
		:Info()
	{
		//InitRSet();
		_type = type;
	}
private:
	void InitRSet() 
	{
		//初始化其他变量
	}
private:
	int _type;
	char _name;
	//...
};
  • The delegate constructor can also be the target constructor
  • Remember: ring delegation can still be compiled but will overflow on the right stack

Rvalue reference

Summary of rvalue references

Default function control

C ++ 11 allows programmers to control whether the compiler needs to generate those functions that are generated by default

Explicit default function

In C ++ 11, you can add = default to the definition or declaration of the default function to explicitly instruct the compiler to generate the default version of the function . The =defaultmodified function is called the explicit default function .

class A
{
public:
	A(int a): _a(a)
	{}
	// 显式缺省构造函数,由编译器生成
	A() = default;
	// 在类中声明,在类外定义时让编译器生成默认赋值运算符重载
	A& operator=(const A& a);
private:
	int _a;
};
	A& A::operator=(const A& a) = default;
int main()
{
	A a1(10);
	A a2;
	a2 = a1;
	return 0;
}

Delete the default function

If you want to limit the generation of certain default functions, in C ++ 98, the function is set to private and not defined, so that if someone else wants to call it, an error will be reported. In C ++ 11, it is simpler. Just add = delete to the function declaration. This syntax instructs the compiler not to generate the default version of the corresponding function. The function modified by = delete is called the delete function .

class A
{
public:
	A(int a): _a(a)
	{}
	// 禁止编译器生成默认的拷贝构造函数以及赋值运算符重载
	A(const A&) = delete;
	A& operator(const A&) = delete;
private:
	int _a;
};
int main()
{
	A a1(10);
	// 编译失败,因为该类没有拷贝构造函数
	//A a2(a1);
	// 编译失败,因为该类没有赋值运算符重载
	A a3(20);
	a3 = a2;
	return 0;
}

Note: Avoid using delete function with explicit

Default parameter

Lambda expression and thread library

Summary of lambda expressions and thread libraries
Published 253 original articles · praised 41 · 40,000+ views

Guess you like

Origin blog.csdn.net/liuyuchen282828/article/details/103954995