Bai Qiao Original: omnipotent type boost: any

Source: Internet
Author: User

4.6 use third-party LibrariesThe preceding section describes how to assign values to, convert, and convert character codes to Visual C ++. In fact, there are some useful third-party class libraries to help C ++ programmers complete object processing, boost is a famous one. This section briefly introduces boost: any, boost: lexical_cast, and rational boost: rational in the boost library.4.6.1 universal boost: anyThe boost Library provides the any class. boost: any is a class that can save any type value. This is a bit like the variant type. However, because variant uses a huge union, low efficiency. While boost uses the template, it does not change the type of the value during saving, but only provides a method for the user to determine the type and value when necessary. Boost: any can be used to store almost any data type:

 
 
  1. boost::any ai, as;  
  2. ai = 100;  
  3. as = string("hello"); 
When necessary, we can use any_cast to restore the original data:
 
 
  1. int i = boost::any_cast<int>(ai);  
  2. string s = boost::any_cast<string>(as); 
When this type of conversion does not match, bad_any_cast will occur abnormally:
 
 
  1. try 
  2. {  
  3.     int i = boost::any_cast<int>(as);  
  4. }  
  5. catch(boost::bad_any_cast & e)  
  6. {  
In traditional C ++ programs, we have to use the omnipotent Pointer "void *" to support various data types, but unfortunately, the Conversion Based on universal pointers is insecure. The "void *" type check is missing. Therefore, we recommend that you use the any class whenever possible. Now you can write the following program to experience how to use boost: any to convert object types. Program 4-10: Use boost: any to convert Object Types
 
 
  1. 01  #include "stdafx.h" 
  2. 02  #include "boost/any.hpp" 
  3. 03  #include <string>  
  4. 04    
  5. 05  using namespace std;  
  6. 06  using namespace boost;  
  7. 07    
  8. 08  class Cat  
  9. 09  {  
  10. 10  };  
  11. 11    
  12. 12  void print(any it)  
  13. 13  {  
  14. 14      if(it.empty())  
  15. 15      {  
  16. 16          printf("nothing!\r\n");  
  17. 17          return;  
  18. 18      }  
  19. 19    
  20. 20      if(it.type() == typeid(int))  
  21. 21      {  
  22. 22          printf("integer: %d\r\n", any_cast<int>(it));  
  23. 23          return;  
  24. 24      }  
  25. 25    
  26. 26      if(it.type() == typeid(string))  
  27. 27      {  
  28. 28          printf("string: %s\r\n", any_cast<string>(it).c_str());  
  29. 29          return;  
  30. 30      }  
  31. 31    
  32. 32      if(it.type() == typeid(CString))  
  33. 33      {  
  34. 34          _tprintf(_T("CString: %s\r\n"), any_cast<CString>(it));  
  35. 35          return;  
  36. 36      }  
  37. 37    
  38. 38      if(it.type() == typeid(Cat))  
  39. 39      {  
  40. 40          _tprintf(_T("oops! a cat!\r\n"));  
  41. 41          return;  
  42. 42      }  
  43. 43  }  
  44. 44    
  45. 45  int main()  
  46. 46  {  
  47. 47      print(100);  
  48. 48    
  49. 49      any as[] = {any(), 100, string("hello"), CString("world"), Cat()};  
  50. 50      for(int i = 0; i < sizeof(as) / sizeof(as[0]); i++)  
  51. 51      {  
  52. 52          print(as[i]);  
  53. 53      }  
  54. 54    
  55. 55      return 0;56 } 
The result output is 4-18.
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =".../attachment/200908/200908241251094430129 .jpg" border = "0"/>
========================================================== === 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://book.vcer.net/images/vcmap-s.gif "/> the above is from section 4.6.1

This article is from the "Bai Qiao blog" blog, please be sure to keep this source http://bluejoe.blog.51cto.com/807902/192762

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.