Search code examples
c++pointerscharvoidtypeid

C++ typeid.name() only returns char "c"


I'm not entirely sure what is going on here. I'm guessing because my input is a string and I'm cycling through it one character at a time it is always returning as type char.

I pretty sure a string is actually char*. The only way I can think to fix this is to include and check what type of character it is, but I'd like to avoid doing that. Is there an alternative method using typeid.name() to figure out what the char is?

I'm using gcc compiler

voidQueue outQueue;
string temp = "32ad1f-31f()d";

int i = 0;
while(temp[i] != '\0')
{
    outQueue.enqueue(temp[i]);
    i++;
}

template<typename T>
void voidQueue::enqueue(T data)
{
    T *dataAdded = new T;
    *dataAdded = data;
    string type(typeid(data).name());
    cout<< type;
    myQueue::enqueue((void *)dataAdded,type);
} 

Solution

  • i want it recognize that char('9') is actually an int

    You can use std::isdigit for this:

    #include <cctype>
    
    bool digit = std::isdigit(static_cast<unsigned char>(temp[i]);