1

I'm trying to create a constructor that states what type of objects it's storing. Is there any simple way to accomplish this? I was looking at decltype.

template <typename T>
MyVector<T>::MyVector(int setCap)
{
    m_ptr = new T[setCap];
    m_size = 0;
    m_capacity = setCap;
    string typeSet = decltype(T);
    cout << "New MyVector of type " << typeSet << " created\n";
}
  • Are you looking for [`typeid`](http://en.cppreference.com/w/cpp/language/typeid)? The output of `typeid(...).name()` may or may not spell out the type name in a way you find useful, that's implementation dependent. – Praetorian Mar 13 '13 at 21:15
  • 2
    @thiefjack if you're using GCC you can demangle the name using the second answer to [this question](http://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname) (don't use the first, it's buggy) – Stephen Lin Mar 13 '13 at 21:18
  • possible duplicate of [How do I get the type of a variable?](http://stackoverflow.com/questions/11310898/how-do-i-get-the-type-of-a-variable) – David G Mar 14 '13 at 15:07

1 Answers1

2

C++ has typeid:

#include <typeinfo>

std::cout << "The type is " << typeid(T).name();
David G
  • 94,763
  • 41
  • 167
  • 253