zoukankan      html  css  js  c++  java
  • c++11之类型萃取type_traits

    • 不知道怎么评价这个特性。。。
    • 代码
        1. #include<iostream>
        2. #include<type_traits>
        3. //继承integral_constant可以方便写静态变量
        4. structGetSize: std::integral_constant<int,1>
        5. {
        6. };
        7. template<class T,T v>
        8. struct integral_my
        9. {
        10. staticconst T value = v;
        11. typedef T value_type;
        12. typedef integral_my<T, v>type;
        13. operator value_type(){return value;};//重载类型转换符
        14. };
        15. class A {};
        16. class B :A {};
        17. class C {};
        18. int main()
        19. {
        20. int a =GetSize::value;
        21. typedefint(*func)();
        22. //类型判断
        23. std::cout << std::is_function<int>::value << std::endl;
        24. std::cout << std::is_function<func>::value << std::endl;
        25. std::cout << std::is_const<constint&>::value << std::endl;
        26. std::cout << std::is_const<constint>::value << std::endl;
        27. std::cout << std::endl;
        28. //类型是否相同
        29. std::cout << std::is_same<int,float>::value << std::endl;
        30. std::cout << std::is_same<int,signedint>::value << std::endl;
        31. std::cout << std::endl;
        32. //判断是否未基类
        33. std::cout << std::is_base_of<B, A>::value << std::endl;
        34. std::cout << std::is_base_of<A, B>::value << std::endl;
        35. std::cout << std::is_base_of<C, A>::value << std::endl;
        36. std::cout << std::endl;
        37. bool b = std::is_base_of<C, A>::value;
        38. std::cout << std::boolalpha;
        39. std::cout << b << std::endl;
        40. return0;
        41. }
         





  • 相关阅读:
    在仅有的一次生命里活出自己最大的可能
    每个人都渴望赞美
    历练领导力的八字要诀
    爱情语录
    Ps
    别跟我要钱,我是教授
    改变人生的五个问题
    纪晓岚妙用口才
    智慧和智商
    经典
  • 原文地址:https://www.cnblogs.com/dongdongweiwu/p/4743658.html
Copyright © 2011-2022 走看看