Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Any type
C++17 added the std::any type to the standard library. It is designed to hold variables of any type and provides methods for type-safe inspection and access.
Exercise
Refactor the following code to use std::any.
 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
#include <typeindex>
#include <vector>
#include <string>
#include <iostream>
struct element
{
    void* elem;
    std::type_index type;
};
int main()
{
    std::vector<element> vec_of_any;
    vec_of_any.emplace_back(element{new int(1), std::type_index(typeid(int))});
    vec_of_any.emplace_back(element{new double(2.0), std::type_index(typeid(double))});
    vec_of_any.emplace_back(element{new std::vector<int>(2, 5), std::type_index(typeid(std::vector<int>))});
    vec_of_any.emplace_back(element{new std::string("Hello"), std::type_index(typeid(std::string))});
    for (const auto& elem : vec_of_any)
    {
        if (elem.type == std::type_index(typeid(int))) {
            std::cout << *(static_cast<int*>(elem.elem)) << std::endl;
        }
        if (elem.type == std::type_index(typeid(double))) {
            std::cout << *(static_cast<double*>(elem.elem)) << std::endl;
        }
        if (elem.type == std::type_index(typeid(std::vector<int>))) {
            const auto vec = static_cast<std::vector<int>*>(elem.elem);
            std::cout << "[";
            for(auto i = 0; i < vec->size(); ++i) {
                std::cout << vec->at(i);
                if (i < (vec->size()-1)) {
                    std:: cout << ", ";
                }
            }
            std::cout << "]\n";
        }
        if (elem.type == std::type_index(typeid(std::string))) {
            std::cout << *(static_cast<std::string*>(elem.elem)) << std::endl;
        }
    }
}
Press desired key combination and then press ENTER.
 Open Source Your Knowledge: become a Contributor and help others learn. Create New Content