0

hey guy this my code on file h and i need to create method that need to print the type.

    #include <iostream>
    #include <typeinfo>
    using namespace::std;

template <class T>
class Set {
    T* group;
    int size_group;
public:
    Set():group(NULL),size_group(0){};
    Set(T*,int);
    Set(const Set<T>&);
    ~Set();

    void PrintObj()const;
    bool isThere(const T&)const;
    void getType()const;

};


template <class T>
void Set<T>::getType() const{
    cout << "The type is: " << typeid(*this).name() << endl;
}

Main:

#include "Set.h"

int main() {
    int arr[] = {1,4,5,6,3,2};
    int arr2[] = {5,2,6};

    Set<int> j(arr,(sizeof(arr)/sizeof(arr[0]))),l(arr2,sizeof(arr2)/sizeof(arr2[0])),k;

    j.getType();
}

OUTPUT:

The type is: 3SetIiE

how i can print the type name? It kind of gibberish to me

  • 1
    Does this answer your question? [Unmangling the result of std::type\_info::name](https://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname) – underscore_d Jun 17 '20 at 10:30
  • From C++17, expressions like `(sizeof(arr)/sizeof(arr[0]))` should be replaced with `std::size(arr)`, or better yet, use an `std::array` instead of a plain/C style array. – underscore_d Jun 17 '20 at 10:32

1 Answers1

0

What you see is calle a mangled name. You can use boost::core::demangle to demangle it (https://www.boost.org/doc/libs/1_73_0/libs/core/doc/html/core/demangle.html).

std::cout << boost::core::demangle(typeid(*this).name()) << std::endl;
Sebastian Hoffmann
  • 2,815
  • 1
  • 12
  • 22
  • boost::core::demangle He has no realization so he does not recognize him – Roni Jack Vituli Jun 17 '20 at 11:04
  • @רוניג'קויטולי You'll have to use the Boost library (https://www.boost.org/). Boost is widespead so that this option usually works for alot of people. There is no way to demangle names with the standard library. – Sebastian Hoffmann Jun 17 '20 at 11:11