0

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.

Community
  • 1
  • 1
Govinda Totla
  • 576
  • 6
  • 19
  • 1
    Don't include bits/stdc++.h, it's just always wrong. If you see a tutorial using this, you know immediately that it's a bad tutorial (which may be the only valid use of that non-standard header) – Useless May 24 '20 at 11:07
  • 2
    `t1` holds a `const char*`, not a `std::string`. It makes sense that the type name is different. – Miles Budnek May 24 '20 at 11:08
  • `The "any" here refers to the "std::any".` Don't take the whole std namespace into your program with those god awful practices and you don't have to explain that to anyone. – ruohola May 24 '20 at 11:08
  • 2
    Why do you think the std::string typename is wrong? Have you tried demangling these names? – Useless May 24 '20 at 11:09
  • FYI `type().name()` should only be used for logging. [std::type_info::name](https://en.cppreference.com/w/cpp/types/type_info/name) `[…]Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.[…]` – t.niese May 24 '20 at 11:12
  • 1
    Taking c++ and trying to make it work like python seems like a bad way to learn the language. Languages are different for a reason, they have strengths and weaknesses. Learn the differences and how to utilize them instead of trying to make the language work the way you want it to. – super May 24 '20 at 11:21
  • `but the string type returns something else` no, it clearly returns an implementation defined character string for `std::string`. Did you check what `typeof(std::string).name()` returns? – KamilCuk May 24 '20 at 11:32
  • Change `std::any t1 = "Hello";` to `std::any t1 = std::string("Hello");`. In C++, a C-style string literal is a different type from a std::string. There is also `"Hello"s` which will make a std::string literal. – Eljay May 24 '20 at 12:12

0 Answers0