I am trying to find the data type of an element of a vector of any type.
I tried the following code:
#include <any>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<any> myList;
myList.push_back(std::string("Hello"));
myList.push_back(3);
myList.push_back(true);
std::any t1 = "Hello";
std::any t2 = 3;
std::any t3 = true;
std::cout<<t1.type().name()<<endl;
std::cout<<t2.type().name()<<endl;
std::cout<<t3.type().name()<<endl;
std::cout<<myList[0].type().name()<<endl;
std::cout<<myList[1].type().name()<<endl;
std::cout<<myList[2].type().name()<<endl;
return 0;
}
The output is as follows:
PKc
i
b
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
i
b
As you can see the int
and bool
are correctly identified, but the string type returns something else. Also, this happens with all the complex C++ data structures as well such as vectors and maps. Is there any way to detect the specific data type?
My ultimate aim is to implement a custom vector data type such as a list
in python. with basic push and get functions.