Advertisement
aircampro

understanding C++17 any types

Jun 15th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. // simple example of the type any being used
  2. //
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include <experimental/any>
  9.  
  10. // although uint8_t holds the max age it must be u16
  11. struct Identifier_t {
  12.     std::string name;
  13.     uint16_t age;
  14.     std::string city;
  15.     float floatTag;
  16. };
  17.  
  18. using namespace std::experimental;
  19.  
  20. main()
  21. {
  22.  
  23. // =========== any list (vector) =======================================
  24. std::vector<any> v { 7, 2.26, true, "hi! there" };
  25.  
  26. // test the types first element integer ?
  27. auto& r = v[0].type();  // What is contained in this std:: any?
  28. if (r == typeid(int))
  29.   std::cout << "We have a int" << "\n";
  30. else
  31.   std::cout << "We have a problem!" << "\n";
  32.  
  33. try
  34. {
  35.     std::cout << "integer has a value of " << any_cast<int>(v[0]) << '\n';
  36. }
  37. catch(bad_any_cast& )
  38. {
  39.     std::cout << "error not a integer" << '\n';
  40. }
  41.  
  42. auto& t = v[1].type();  // What is contained in this std:: any?
  43. if (t == typeid(double))
  44.   std::cout << "We have a double" << "\n";
  45. else
  46.   std::cout << "We have a problem!" << "\n";
  47.  
  48. try
  49. {
  50.     std::cout << "double has a value of " << any_cast<double>(v[1]) << '\n';
  51. }
  52. catch(bad_any_cast& )
  53. {
  54.     std::cout << "error not a double" << '\n';
  55. }
  56.  
  57. auto& s = v[2].type();  // What is contained in this std:: any?
  58. if (s == typeid(bool))
  59.   std::cout << "We have a bool" << "\n";
  60. else
  61.   std::cout << "We have a problem!" << "\n";
  62.  
  63. try
  64. {
  65.     std::cout << "bool has a value of " << any_cast<bool>(v[2]) << '\n';
  66. }
  67. catch(bad_any_cast& )
  68. {
  69.     std::cout << "error not a bool" << '\n';
  70. }
  71.  
  72. try
  73. {
  74.     std::cout << "bool to int has a value of " << any_cast<int>(v[2]) << '\n';
  75. }
  76. catch(bad_any_cast& )
  77. {
  78.     std::cout << "error cant cast an any bool type to an int" << '\n';
  79. }
  80.  
  81. auto& u = v[3].type();  // What is contained in this std:: any?
  82. if (u == typeid(std::string))                            // not a string
  83.   std::cout << "We have a string" << "\n";
  84. else if (u == typeid(char))                             // not a char
  85.   std::cout << "We have a char" << "\n";
  86. else if (u == typeid(const char*))                      // should match const char*
  87.   std::cout << "We have a const char*" << "\n";
  88. else
  89.   std::cout << "We have a problem! val" << "\n";
  90.  
  91. try
  92. {
  93.     std::cout << any_cast<const char*>(v[3]) << '\n';
  94. }
  95. catch(bad_any_cast& )
  96. {
  97.     std::cout << "error not a string" << '\n';
  98. }
  99.  
  100. // =================== structure with auto on return sets varaibles ============
  101. Identifier_t p1{"Tom", 20, "Moscow", 12.345f};
  102. auto [name, age, city, floatTag] = p1;
  103. std::cout << name << "(" << age << ") lives in " << city << std::endl;
  104.  
  105. // ================== any type as a map you can use any in containers ===========
  106. std::map<std::string, any> m;
  107. m["integer"] = 10;
  108. m["string"] = std::string("Hello World");
  109. m["float1"] = 1.0000012f;
  110. m["float2"] = 1.76f;
  111. m["double"] = 1.676;
  112.  
  113. // now look at each value in the map and display the type
  114. for (auto &[key, val] : m)
  115. {
  116.     if (val.type() == typeid(int))
  117.     {
  118.        try
  119.        {
  120.           std::cout << "int: " << any_cast<int>(val) << " " << key << "\n";
  121.        }
  122.        catch(bad_any_cast& )
  123.        {
  124.           std::cout << "error not a int" << '\n';
  125.        }  
  126.     }
  127.  
  128.     else if (val.type() == typeid(std::string))
  129.     {
  130.        try
  131.        {
  132.           std::cout << "string: " << any_cast<std::string>(val) << " " << key << "\n";
  133.        }
  134.        catch(bad_any_cast& )
  135.        {
  136.           std::cout << "error not a string" << '\n';
  137.        }
  138.     }  
  139.     else if (val.type() == typeid(float))
  140.     {
  141.         try
  142.         {
  143.             std::cout << "float: " << any_cast<float>(val) << " " << key << "\n";
  144.         }
  145.         catch(bad_any_cast& )
  146.         {
  147.           std::cout << "error not a float" << '\n';
  148.         }        
  149.     }
  150.     else if (val.type() == typeid(double))
  151.     {
  152.         try
  153.         {
  154.           std::cout << "double: " << any_cast<double>(val) << " " << key << "\n";
  155.         }
  156.         catch(bad_any_cast& )
  157.         {
  158.           std::cout << "error not a double" << '\n';
  159.         }
  160.  
  161.     }
  162.  
  163. }
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement