boost--BOOST_AUTO、typeof、result_of

时间:2023-03-09 23:38:33
boost--BOOST_AUTO、typeof、result_of

1、BOOST_AUTO

BOOST_AUTO的功能类似于auto和any,可以用来定义任意类型数据,且可以在编译时自动推导出表达式的类型。BOOST_AUTO属于boost中的typeof库,使用需要包含"boost/typeof/typeof.hpp"。

当使用BOOST_AUTO用来定义引用类型的时候需要加&。

#include <vector>
#include <boost/typeof/typeof.hpp>
int main()
{
BOOST_AUTO(x, ); //x为int类型,初始值为5
int n = ;
BOOST_AUTO(&i, n);//加了&表示引用,所以i为int&类型,初始值为10
BOOST_AUTO(p, new char[]); //p为char*类型
BOOST_AUTO(v, vector<int>(, )); //v为vector<int>类型,初始为包含10个值为0的元素
BOOST_AUTO(ptr, &n); //ptr为int*类型,指向n
BOOST_AUTO(y, n * ); //y为int类型,初始值为n * 5 return ;
}

2、typeid

typeid是C++中的运算符关键字,使用它可以获得变量或表达式的类型,它返回的是type_info类型的对象,type_info类的成员函数name()用来获得具体类型的字符串,还支持==、!=判断。

如果测试的对象是类类型且至少包含有一个虚函数,则typeid是在运行时获得对象的类型,属于运行时类型识别RTTI。否则,typeid在编译时就可以计算出对象的类型。

int n = ;
cout << typeid(n).name() << endl;
cout << typeid(&n).name() << endl; cout << typeid(double).name() << endl; Base base;
Base * pBase = new Derived;
cout << typeid(base).name() << endl; //输出class Base
cout << typeid(pBase).name() << endl; //输出class Base*
cout << typeid(*pBase).name() << endl; //输出class Derived

3、BOOST_TYPEOF、typeof、decltype

我们一般使用BOOST_AUTO、auto、any来直接定义变量,变量的数据类型在编译的时候通过保存的数据来导出;使用BOOST_TYPEOF、typeof、decltype也用来直接定义变量,但变量的数据类型根据指定变量或表达式的数据类型来导出。

①、BOOST_TYPEOF属于boost中的typeof库,使用需要包含"boost/typeof/typeof.hpp"。

#include <vector>
#include <boost/typeof/typeof.hpp>
int main()
{
BOOST_TYPEOF() x = ; //x为int类型,初始值为10
int n = ;
BOOST_TYPEOF(n) y = ; //y为int类型,初始值为0
BOOST_TYPEOF(&n) p = NULL; //p为int*类型,初始值为NULL
double d = 2.0;
BOOST_TYPEOF(n + d) pi = 3.14; //pi为double类型,初始值为3.14 return ;
}

②、typeof是GUN C中的关键字。

    int n = ;
typeof(n) var = ; //var为int类型,初始值为666 int var = ;
typeof(&var) pVar = NULL; //pVar为int*类型,初始值为NULL typeof(var * n) value = ; //value为int类型,初始值为0

③、decltype是c++11中的关键字,如果表达式是左值类型的,则返回的是对应的引用类型。

int main()
{
int n = ;
decltype(n) var = ; //var为int类型,初始值为0 int* pVar = NULL;
decltype(pVar) pNum = NULL; //pVar为int*类型,初始值为NULL int* pV = NULL;
decltype(*pV) Num; //error:*pV是左值类型,所以Num是一个引用,必须初始化 int FuncName();
decltype(FuncName()) value = ; //value为int类型,初始值为666 decltype(n * 3.0) pi = 3.14; //pi为double类型,初始值为3.14 return ;
}

4、result_of

result_of<>可以获得一个调用表达式的返回类型,调用表达式是一个含有operator()的表达式,它可以是一个函数指针、函数引用、函数对象。

可以通过result_of<>的内部类型定义type来获得具体类型,假设类型Func可被调用(具有operator()),func是Func类型的一个左值,那么typeid(boost::result_of<Func(T1, T2, ...)>::type) 必然等于typeid(func(t1, t2, ...))。

c++11现在已经支持result_of。

①、result_of<>可以获得函数指针的类型,而typeof只能获得函数表达式的类型:

#include "boost/utility/result_of.hpp"

int main()
{
typedef double(*Func)(double d);
//Func func = sqrt;
boost::result_of<Func(double)>::type x = sqrt(5.0); return ;
}

②、result_of<>获得函数调用表达式的类型:

#include "boost/utility/result_of.hpp"

typedef int(*FuncPtr)(int d);
typename boost::result_of<FuncPtr(int)>::type Func(FuncPtr t, int t1) //这里在result_of<>前必须加typename,否则编译出错
{
return t(t1);
} int main()
{
typedef int(*FuncPtr)(int d);
FuncPtr func = abs;
Func(func, -); return ;
}

③、result_of<>还可以获得泛型函数调用表达式的类型:

#include "boost/utility/result_of.hpp"

template<typename T, typename T1>
typename boost::result_of<T(T1)>::type Func(T t, T1 t1) //这里在result_of<>前必须加typename,否则编译出错
{
return t(t1);
} int main()
{
typedef int(*FuncPtr)(int d);
FuncPtr func = abs;
Func(func, -); return ;
}