auto a; // 错误,没有初始化表达式,无法推断出a的类型auto int a = 10; // 错误,auto临时变量的语义在C++ 11中已不存在auto a = 10;auto c = ‘A‘;auto s("hello");vector<int> vctTemp;auto it = vctTemp.begin();auto ptr = [](){ cout << "hello world" << endl; };
template <class T, class U>auto Multiply(T t, U u)->decltype(t*u){typedef decltype(t*u) NewType;NewType *pResult = new NewType(t*u);return *pResult;}
1、表达式根据其值的类型可分为以下三类:
decltype(fun()) sum = x;//sum的类型就是函数fun的返回值类型const int ci = 0, &cj = ci;decltype(ci) x = 0; //x的类型是const int型decltype(cj) y = x; //y的类型是const int&,y绑定到变量xdecltype(cj) z;//错误,引用类型必须被初始化int i = 42, *p = &i, &r = i;decltype(r + 0) b; //r+0的结果是int型//通过解引用可以给指针指向的对象赋值,说明解引用操作返回的是引用类型的decltype(*p) c;//错误,c是int&,必须被初始化//decltype结果类型与表达式形式密切相关decltype((i)) d;//错误,d是int&类型,必须初始化decltype(i) e;//正确,e是一个int类型
原文:http://www.cnblogs.com/fengkang1008/p/4652221.html