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, 5); //x为int类型,初始值为5
    int n = 10;
    BOOST_AUTO(&i, n);//加了&表示引用,所以i为int&类型,初始值为10
    BOOST_AUTO(p, new char[20]);  //p为char*类型
    BOOST_AUTO(v, vector<int>(10, 0)); //v为vector<int>类型,初始为包含10个值为0的元素
    BOOST_AUTO(ptr, &n); //ptr为int*类型,指向n
    BOOST_AUTO(y, n * 5); //y为int类型,初始值为n * 5

    return 0;
}
View Code

相关文章:

  • 2022-12-23
  • 2021-06-09
  • 2021-06-23
  • 2021-07-31
  • 2021-06-13
  • 2021-10-06
  • 2022-02-16
  • 2021-05-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2022-12-23
相关资源
相似解决方案