- Type ALias (与typedef 相似)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
using func=void(*)(int,int);
void example(int,int){} func fn=example; typdef basic_string<char> string;
template<typename T> struct Container{ using value_type=T; }
template<typename Cntr> void fn2(const Cntr& c){ typename Cntr::value_type n; }
template<class CharT> using mystring=std::basic_string<CharT,std::char_traits<Chart>> mystring<char> str;
|
- nonexcep
Type ALias
code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
using func=void(*)(int,int);
void example(int,int){} func fn=example; typdef basic_string<char> string;
template<typename T> struct Container{ using value_type=T; }
template<typename Cntr> void fn2(const Cntr& c){ typename Cntr::value_type n; }
template<class CharT> using mystring=std::basic_string<CharT,std::char_traits<Chart>> mystring<char> str;
|
using 用法总结
- using-directives
1 2 3
| using namespace std;
using std::cout;
|
- using-declareations
1 2
| using _Base::_M_allocate
|
- type alias
1 2 3 4 5 6 7 8 9 10
| using func=void(*)(int,int);
template<typename T> struct Container{ using value_type=T; }
template<class CharT> using mystring=std::basic_string<CharT,std::char_traits<Chart>>
|
noexcept
表示函数不抛出异常
当异常都没有被捕获时,会调用std::terminate()中的std::abort()方法
code
1 2 3 4
| void swap(Type& x,Type& y) noexcept(nonexcept(x.swap(y))){ x.swap(y); }
|
注意点
当使用growable container(会发生 memory reallocation)之后两种
- vector
- deque
如果类定义了移动构造,并且希望被grow 类型的容器调用需要在移动构造时使用noexcept1 2 3 4 5 6 7 8 9 10 11 12
| class Mystring{ private: char* _data; size_t _len; public: Mystring(Mystring&& str)noexcept: :_data(str._data),_len(str.len){}
Mystring& operator=(MyString&& str)noexcept{ return *this; } }
|
override
用于让编译器帮忙检验当前函数是否是复写父类函数
code
1 2 3 4 5 6 7 8 9 10 11 12
| struct Base{ virtual void vfunc(float){} }; struct Dervied1:Base{ virtual void vfunc(int){} }
struct Dervied1:Base{ virtual void vfunc(int)override{} }
|
final
不允许复写
code
类
1 2
| struct Base1 final{} struct Derived1:Base1{};
|
方法
1 2 3 4 5 6 7
| struct Base2{ virtual void f() final; }
struct Derived2:Base2{ void f(); }
|