c++抛出异常

try
{
std::cout << "finish" << std::endl;
throw std::out_of_range("error");
return -1;
}
catch (std::exception const& ex)
{
std::cerr << ex.what()<<std::endl;
return EXIT_FAILURE;
}
throw std::out_of_range("error2");//单独会报错

==类模板的特化==

deque管理stack,删除元素会释放内存,重新分配元素不需要移动,对string不起作用。

==类模板局部特化==

template <typename T1, typename T2>
class MyClass
{};

//参数类型相同
template <typename T>
class MyClass<T, T>
{};

//int
template <typename T>
class MyClass<T, int>
{};

//指针类型

template <typename T1, typename T2>
class MyClass<T1*,T2*>
{};

//有时候二义性,多指定类型

template <typename T>
class MyClass<T*, T*>
{};

可以缺省参数

==非类型的模板参数==

template <typename T1, int T2>
class MyClass
{};

使用MyClass<int,20> a;

//使用下面的缺省也可,不够灵活

template <typename T1, int T2=100>
class MyClass
{};

重载函数的集合不能用于模板参数的演绎,函数模板的实参强制类型转换为具体的类型(int(*)(int const&))add<int,5>

4.3非类型模板参数

浮点数类对象不允许

依赖于模板参数的对象,模板内部使用 ==.template==

posted @ 2022-09-19 21:26  风飞侠  阅读(209)  评论(0)    收藏  举报