C++11 -- Type Alias, noexcept, override, final

Type Alias

Similar to typedef, as like as two peas, it is a kind of aliasing. Type Alias needs to use keyword using. There are three usage scenarios:

Scenario 1: aliasing function pointers

//typedef void (*func)(int,int);// There is a way to write in C language. Here, a function pointer type - func is defined. The function type pointed to has two int type parameters, and the return value is void. It's just obvious that this syntax can't highlight that func is a type
using func=void(*)(int,int);//Using such a typed alias can clearly indicate that func is a pointer function type
......

//Use as follows
void example(int a,int b){}
func fun=example;//Use func type to create an object that points to a function

Scenario 2: alias template parameters

template<typeneme T>
class Container{
using velue_type=T;//Equivalent to typedef T value_type;
};
......

//Use as follows
template<typename Container>
void fun(const Container& c)
{
	typename Container::value_type n;//Get the element type in the Container container and create a variable of this type
}

This method is very common in the C + + standard library, and is widely used in containers, iterators and imitation functions.

Scenario 3: aliasing template types

Alias Template (Alias Template) is used to hide template parameters and simplify writing.

template<typename Char>
using mystring=std::basic_string<Char,std::char_traits<Char>>;
......

//Use as follows
mystring<char> str;

Typedef basic is available in the standard library_ string string.

expand

Several forms of using:
1. Namespace usage directive

using namespace std;

2. Declaration of use of namespace members

using std::cout;

3. Use declaration of class members

class HH {
protected://Protect members
	int hh;
};
class XX:public HH {//inherit
protected://Protect members
	using HH::hh;//Members of the parent class are used here
public:
	void test() {
		hh = 1;//The scope qualification of the parent class is no longer required here
	}
};

4. After C++2.0, Type Alias and Alias Template use keywords

noexcept

exception is a university question, which is worthy of careful study by programmers.

//The general form is as follows:
//void fun()noexcept(expression->bool);// Whether the function throws an exception depends on the return value of the expression. If it is true, no exception will be thrown, and if it is false, an exception will be thrown

void fun()noexcept;//Ensure that the function does not throw exceptions, which is equivalent to void fun()noexcept(true);

void swap(Type& a,Type& b)noexcept(noexcept(a.swap(b)))//This means that when the expression a.swap(b) has no exception, no exception will be thrown
{
	a.swap(b);
}

If the exception is thrown and not handled in this function, it will be passed upward to the function calling the function for processing. If it is still not handled, it will be passed upward until there is no function call relationship.
If the exception is not handled until the end, the program will trigger a call to std::terminate(), and in std::terminate(), it will call std::abort() to terminate the program.
Note: the move constructor and the move assignment function are generally declared as noexcept, which often only means the transfer of memory ownership and does not involve memory allocation, so there are generally no exceptions. Using noexcept declaration helps programmers infer program logic on the one hand, and the compiler can better optimize code on the other hand.

class MyString{
private:
	char* _data;
	size_t _len;
	...
public:
	//Mobile copy
	MyString(MyString&& str)noexcept:_data(str._data),_len(str._len){...}
	//Move assignment
	MyString& operator=(MyString&& str)noexcept{
		...
		return *this;
	}
};

override

override is used to modify the virtual function of the parent class overridden by the subclass. It is used to display and tell the compiler that the modified function is the virtual function of overriding the parent class, so as to prevent a clerical error from essentially redefining a function in the subclass.

class Base{
public:
	virtual void func(int a){}
};
class Derived:public Base{
public:
	void fun(int a){};//I originally wanted to rewrite the virtual function of the parent class, but the clerical error essentially redefines a function, and the compiler will not report an error
	//void fun(int a)override {};//error: it indicates that this function is a virtual function that overrides the parent class. Through inspection, it is found that the parent class does not have a function with the same name, so an error is reported
	void func(int a){};
};

final

Used to decorate classes with inheritance system and virtual functions in classes.
**Used to modify a class: * * indicates that this class (modified by final) is the lowest class in the inheritance system, and no other class will inherit this class

class Base1 {};
class Base2 final :public Base1 {};
class Base3:public Base2{};//error: a class declared final cannot be used as a base class

Used to modify virtual function: indicates that all classes that inherit from the class of the virtual function (modified by final) no longer have the right to override the virtual function

class Base1 {
public:
	virtual void fun(){}
};
class Base2:public Base1 {
public:
	virtual void fun()final{}
};
class Base3:public Base2{
public:
	virtual void fun(){}//error: a function declared final cannot be overridden by its subclass
};

Keywords: C++

Added by kenshejoe on Wed, 05 Jan 2022 08:15:05 +0200