zoukankan      html  css  js  c++  java
  • boost实用工具:typeof库 BOOST_TYPE BOOST_AUTO

      boost::typeof库中使用宏BOOST_TYPEBOOST_AUTO来模拟C++11关键字typeof和auto

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
     
    /* boost_typeof.cpp
        boost中typeof库学习使用
    */


    #include <iostream>
    #include <vector>
    #include <string>
    #include <boost/typeof/typeof.hpp>
    #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
    #define auto_t BOOST_AUTO

    using namespace std;
    using namespace boost;

    //C++是一种静态强类型语言,所有变量在使用前都必须声明其类型
    //但有时会带来麻烦,尤其是C++引入命名空间后,会导致繁琐的类型声明
    //如 std::map<std::string, std::string>::iterator pos = ...;
    //仅仅声明一个遍历map的迭代器,就使用了一大行
    //使用using关键字打开命名空间后可以部分减少类型声明的长度,但是没有从根本是解决这样的问题
    //随着编程技术的进步,将来的程序员也不可能写出冗长复杂的类型声明
    //C++静态强类型的优点在这时已经成为阻碍程序员生产力的“缺陷”
    //于是,C++11新增typeof和auto关键字解决上述问题

    //头文件<boost/typeof/typeof.hpp> 定义的宏BOOST_TYPEOF  BOOST_AUTO
    //完全模拟了C++11中typeof和auto的用法
    //boost::typeof虽然支持C++和STL的大部分数据类型,但是它并没有智能到支持任何数据类型
    //如果用户自定义类型想应用于type库,则需要宏注册后才能使用
    vector<string> vecFunc()
    {
        vector<string> vecString(
    10);
        
    return vecString;
    }


    namespace ex
    {
        
    struct demo_class
        {
            
    int na;
            
    int nb;
        };
    }
    BOOST_TYPEOF_REGISTER_TYPE(ex::demo_class)          
    //向typeof注册类


    int main(void)
    {

        BOOST_TYPEOF(
    3.0 * 2) x = 3.0 * 2;              //推导类型为double
        BOOST_AUTO(y, 5 + 3);                           //推导类型为int
        BOOST_AUTO(p, make_pair(23"Michael Jordan")); //推导类型为pair<int, const char*>
        BOOST_AUTO(vec, vecFunc());                     //推导类型为vector<string>
        cout << typeid(x).name() << endl << endl;
        cout << 
    typeid(y).name() << endl << endl;;
        cout << 
    typeid(p).name() << endl << endl;;
        cout << 
    typeid(vec).name() << endl << endl;;

        BOOST_AUTO(xx, make_pair(
    "Demo", ex::demo_class()));
        cout << 
    typeid(xx).name() << endl << endl;;

        auto_t(pd, 
    new double[10]);                     //推导类型为double*
        cout << typeid(pd).name() << endl << endl;;

        cin.get();
        
    return 0;
    }

  • 相关阅读:
    oracle增加表空间大小
    oracle日常查看
    oracle报错ORA-01653 dba_free_space中没有该表空间
    大数据hadoop生态圈
    1104报表背景知识
    db2和oracle字段类型对比
    weblogic 内存配置
    java内存配置举例
    java内存和linux关系
    PHP连接Redis操作函数
  • 原文地址:https://www.cnblogs.com/MakeView660/p/7025956.html
Copyright © 2011-2022 走看看