[c++]decltype
2020/04
05
14:04
class student
{
private:
int age;
public:
void setAge(int age)
{
this->age = age;
}
};
student& CreateStudent()
{
static student stdobj;
return stdobj;
}
student& CreateStudent2()
{
static student stdobj;
return stdobj;
}
int main()
{
decltype(CreateStudent()) xiaoming1 = CreateStudent(); // xiaoming为 student& 类型
decltype(CreateStudent2()) xiaoming2 = CreateStudent2(); // xiaoming为 student& 类型
auto zhangsan = CreateStudent(); // zhangsan 为 student 类型
auto lisi = CreateStudent2(); // lisi 为 student 类型
}
decltype 为编译器推断关键字,其代码只推断不执行
auto 为编译器帮程序员手工填写关键字,其代码完整执行
decltype 推断其完整类型
auto 会剥离掉&(引用)操作符,即若 a为 int& 型,则用auto会获得int
所以可以写出类似于
auto p = new decltype(CreateStudent3()); 的语法
CopyRights: The Post by BY-NC-SA For Authorization,Original If Not Noted,Reprint Please Indicate From 老刘@开发笔记
Post Link: [c++]decltype
Post Link: [c++]decltype