Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // simple example of the type any being used
- //
- #include <iostream>
- #include <string>
- #include <vector>
- #include <map>
- #include <experimental/any>
- // although uint8_t holds the max age it must be u16
- struct Identifier_t {
- std::string name;
- uint16_t age;
- std::string city;
- float floatTag;
- };
- using namespace std::experimental;
- main()
- {
- // =========== any list (vector) =======================================
- std::vector<any> v { 7, 2.26, true, "hi! there" };
- // test the types first element integer ?
- auto& r = v[0].type(); // What is contained in this std:: any?
- if (r == typeid(int))
- std::cout << "We have a int" << "\n";
- else
- std::cout << "We have a problem!" << "\n";
- try
- {
- std::cout << "integer has a value of " << any_cast<int>(v[0]) << '\n';
- }
- catch(bad_any_cast& )
- {
- std::cout << "error not a integer" << '\n';
- }
- auto& t = v[1].type(); // What is contained in this std:: any?
- if (t == typeid(double))
- std::cout << "We have a double" << "\n";
- else
- std::cout << "We have a problem!" << "\n";
- try
- {
- std::cout << "double has a value of " << any_cast<double>(v[1]) << '\n';
- }
- catch(bad_any_cast& )
- {
- std::cout << "error not a double" << '\n';
- }
- auto& s = v[2].type(); // What is contained in this std:: any?
- if (s == typeid(bool))
- std::cout << "We have a bool" << "\n";
- else
- std::cout << "We have a problem!" << "\n";
- try
- {
- std::cout << "bool has a value of " << any_cast<bool>(v[2]) << '\n';
- }
- catch(bad_any_cast& )
- {
- std::cout << "error not a bool" << '\n';
- }
- try
- {
- std::cout << "bool to int has a value of " << any_cast<int>(v[2]) << '\n';
- }
- catch(bad_any_cast& )
- {
- std::cout << "error cant cast an any bool type to an int" << '\n';
- }
- auto& u = v[3].type(); // What is contained in this std:: any?
- if (u == typeid(std::string)) // not a string
- std::cout << "We have a string" << "\n";
- else if (u == typeid(char)) // not a char
- std::cout << "We have a char" << "\n";
- else if (u == typeid(const char*)) // should match const char*
- std::cout << "We have a const char*" << "\n";
- else
- std::cout << "We have a problem! val" << "\n";
- try
- {
- std::cout << any_cast<const char*>(v[3]) << '\n';
- }
- catch(bad_any_cast& )
- {
- std::cout << "error not a string" << '\n';
- }
- // =================== structure with auto on return sets varaibles ============
- Identifier_t p1{"Tom", 20, "Moscow", 12.345f};
- auto [name, age, city, floatTag] = p1;
- std::cout << name << "(" << age << ") lives in " << city << std::endl;
- // ================== any type as a map you can use any in containers ===========
- std::map<std::string, any> m;
- m["integer"] = 10;
- m["string"] = std::string("Hello World");
- m["float1"] = 1.0000012f;
- m["float2"] = 1.76f;
- m["double"] = 1.676;
- // now look at each value in the map and display the type
- for (auto &[key, val] : m)
- {
- if (val.type() == typeid(int))
- {
- try
- {
- std::cout << "int: " << any_cast<int>(val) << " " << key << "\n";
- }
- catch(bad_any_cast& )
- {
- std::cout << "error not a int" << '\n';
- }
- }
- else if (val.type() == typeid(std::string))
- {
- try
- {
- std::cout << "string: " << any_cast<std::string>(val) << " " << key << "\n";
- }
- catch(bad_any_cast& )
- {
- std::cout << "error not a string" << '\n';
- }
- }
- else if (val.type() == typeid(float))
- {
- try
- {
- std::cout << "float: " << any_cast<float>(val) << " " << key << "\n";
- }
- catch(bad_any_cast& )
- {
- std::cout << "error not a float" << '\n';
- }
- }
- else if (val.type() == typeid(double))
- {
- try
- {
- std::cout << "double: " << any_cast<double>(val) << " " << key << "\n";
- }
- catch(bad_any_cast& )
- {
- std::cout << "error not a double" << '\n';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement