c++几个新特性

template 模板

1、出于通用性考虑,程序库中几乎所有东西都被设计为template形式,不支持template几乎不能使用标准程序库。

2、所谓template,是针对“一个或多个尚未明确的类型”所撰写的函数或类。使用template时,可以显示地(explicitly)或隐式地(implicitly)将类型当作参数传递。

3、目前唯一能让“template的运用”具有可移植的方式,就是在头文件中以inline function实现template function。(编译器会对函数模板进行两次编译:在声明的地方对模板代码本身进行编译、语法检查,在调用的地方对参数替换后的代码进行编译。)

4、Nontype Templates参数(非类型模板参数):type可以作为template的参数,非nontype也可以作为template参数。nontype参数因而被看作整个template类型的一部分 

例如:bitset<32> flags32  //bitset with 32 bits

        bitset<50> flags50   //bitset with 50 bits

5、Default Template Parameters(缺省模板参数):template classes可以有缺省参数。

例如:template <class T, class container = vector<T> >

         class Myclass;

         Myclass<int> x1;  //equivalent to Myclass<int, vector<int> >

6、关键字typename 关键字typename被用来作为类型之前的标识符,如typename T::SubType * ptr 与T::SubType *ptr区别。

基本型别的显式初始化

1、如果采用不含参数的、明确的constructor(构造函数)调用语法,基本类型初始化为0。

int i1;      //undefined value
int i2 = int(); //initialized with zero

此特性能够保证template类型能够在任何类型都有一个确定的初值

异常处理

1、通常异常处理,C++标准程序库可以在不“污染”函数接口(亦即参数和返回值)的情况下处理异常。如果你遇到一个意外情况,可以“抛出一个异常”来停止一般的(正常的)处理流程。

class Error;
void f()
{
      ...
      if(exception-condition)
      {
           throw Error();   //create object of class Error and throw it as exception
      }
}

异常机制能够保证程序在发生异常时候,像return一样优雅的退出,局部对象得到析构

int main()
{
      try{
           .....
           f();
           ..... 
      }
      catch (const Error&){
            .....            //handle exception
      }
}

在这里,try区段中任何选型为Error的异常都将在catch中获得处理

注意区分:异常处理、错误处理

关键字exlplict

1、关键字explicit的作用,可以禁止“单参数构造函数(single argument constructor)”被用于自动类型转换。

例如:集合类型(collection classes),可以将初始长度作为参数传递给构造函数,例如你可以申明一个构造函数,以stact的初始大小为参数:

class Stack
{
       explicit Stack(int size);  //create stack with initial size
       .....
};

//声明了explicit,则
Stack s;
s = 40; //将会曝出编译错误,如没有声明,则会调用Stack(40)

新的类型转换操作符(Type Conversion Operators)

1、static_cast

将一个值以符合逻辑的方式转换类型;可以看作“利用原值重建一个临时对象,并在设立初值时使用类型转换”。唯有当上述的类型转换有所定义,整个转换才会成功。所谓的“有所定义”,可以是语言内建规则,也可以是程序员自定的转换动作。

float x;
...
cout << static_cast<int>(x); //print x as int

f(static_cast<string>("hello")); //call f() for string instead of char*

2、dynamic_cast

将多态类型(ploymorphic type)向下转型(downcast)为其实际静态类型(real static type).这是唯一在执行时期进行校验的转型动作,你可以用它来检验某个多态对象的类型。

class Car; //abstact base class(has at least one virtual function)

class Cabriolet : public Car{
      ....
} ;

void f(Car* cp)
{
      Cabriolet* p = dynamic_cast<Cabriolet*>(cp)
      if(p == NULL)
      {
            //did not refer to an object of type cabriolet
            ...
       }
}

3、const_cast

设定或去除类型的常数型(constness),亦可以去除volatile饰词,除此之外不用于任何转换

4、reinterpret_cast

此操作符与实际编译器定义

常数静态成员的初始化(constant static members)

我们终于能够在class声明中对“整数型常数静态成员”直接赋予初值。

class MyClass
{
      static const int num = 100;
      int elems[num];
      ...
};

//注意,你还必须为class之中声明的常静态成员,定义一个空间
const int MyClass::num;   //no initialization here

main()定义式

其中已经内置了return 0

原文地址:https://www.cnblogs.com/penghuster/p/6287101.html