014decltype含义和举例_用于推导类型
#include<iostream>
#include<cstdlib>
#include <functional>
using namespace std;
class CT
{
public:
int i;
int j;
};
int testFunction()
{
return 10;
}
const int&&myfunctionTest(void)
{
return 0;
}
int main(void)
{
const int i = 0;
const int&reference01 = i;
auto number01 = i;
decltype(i) number02 = 23;
decltype(reference01) number03 = number02;
decltype(CT::i) number04;
CT temCT;
decltype(temCT) temCT02;
decltype(temCT02.j)number05;
auto &&number06 = i;
decltype(number06) && number07 = i;
decltype(34)number08 = 5;
int j = 0;
int*p1 = &j;
int&reference02 = j;
decltype(reference02 + 1)number09 = 454;
decltype(p1)p2=&j;
*p2 = 43;
decltype(*p2) number10 = j;
decltype((i))reference03 = j;
decltype(testFunction())temv = 14;
decltype(testFunction)temv2;
function<decltype(testFunction)> func01 = testFunction;
cout << func01() << endl;
decltype(myfunctionTest()) myreturn = 0;
system("pause");
return 0;
}
015decltype的主要用途_函数模板中
#include<iostream>
#include<cstdlib>
#include <vector>
using namespace std;
template<typename T>
class CT
{
public:
decltype(T().begin()) iter;
void getBegin(T&tem)
{
iter = tem.begin();
}
public:
CT(){}
};
int main(void)
{
using conttype =const std::vector<int>;
conttype myVector = { 12,23,34 };
CT<conttype>ct;
ct.getBegin(myVector);
const std::vector<int> myvector02 = { 23,345,45 };
vector<int>::const_iterator iter = myvector02.begin();
system("pause");
return 0;
}
016使用括号()构造类的临时对象调用类中的成员函数
#include<iostream>
#include<cstdlib>
using namespace std;
class A
{
public:
A()
{
cout << "执行A的构造函数" << endl;
}
~A()
{
cout << "执行A的析构函数" << endl;
}
int myFunction()
{
cout << "指向A的myFunction函数" << endl;
return 0;
}
};
int main(void)
{
A().myFunction();
(const A()).myFunction();
decltype(A().myFunction()) number01;
system("pause");
return 0;
}
017decltype的主要用途_通过变量表达式抽取变量类型
#include<iostream>
#include<cstdlib>
#include <vector>
using namespace std;
int main(void)
{
vector<int> myV;
myV.push_back(1);
myV.push_back(2);
myV.push_back(3);
vector<int>::size_type mySize = myV.size();
cout << mySize << endl;
decltype(myV)::size_type mysize02 = mySize;
cout << mysize02 << endl;
typedef decltype(sizeof(0)) size__t;
system("pause");
return 0;
}
018decltype的主要用途_auto结合decltype构成返回类型后置语法
#include<iostream>
#include<cstdlib>
#include <vector>
using namespace std;
auto func(int a,int b)->int
{
return 0;
}
auto add(int i,int k)->decltype(i+k)
{
return i + k;
}
int&myfunction01(int&i)
{
return i;
}
double myfunction01(double&i)
{
return i;
}
template<typename T>
auto myFunTemplate(T&tv)->decltype(myfunction01(tv))
{
return myfunction01(tv);
}
int main(void)
{
int i = 12;
double d = 23.34;
cout << myFunTemplate(i) << endl;
cout << myFunTemplate(d) << endl;
system("pause");
return 0;
}
019decltype的主要用途_decltype(auto)用法_用于函数的返回类型
#include<iostream>
#include<cstdlib>
#include <vector>
using namespace std;
template<typename T>
T& DoubleMyself(T&value)
{
value *= 2;
return value;
}
template<typename T>
decltype(auto) DoubleMyself02(T&value)
{
value *= 2;
return value;
}
int main(void)
{
int number01 = 100;
DoubleMyself(number01) = 20;
cout << "number01=" << number01 << endl;
DoubleMyself02(number01) = 43;
cout << number01 << endl;
decltype(DoubleMyself02(number01))number02 = number01;
cout << "number02=" << number02 << endl;
system("pause");
return 0;
}
020decltype的主要用途_decltype(auto)用法_用于变量的声明中_保存const和引用属性
#include<iostream>
#include<cstdlib>
using namespace std;
decltype(auto) myFunction()
{
int i = 1;
return i;
}
decltype(auto)myFunciton02()
{
int i = 1;
return(i);
}
int main(void)
{
int number01 = 12;
const int &reference = 23;
auto number02 = reference;
decltype(auto)number03 = reference;
int number04 = 234;
decltype((number04))rere = number04;
decltype(myFunction()) number05 = 23;
int a = 1;
decltype(myFunciton02())number06 = a;
myFunciton02() = 234;
system("pause");
return 0;
}