Question

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);
} 
Was it helpful?

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]);

OTHER TIPS

In your example, T is char and gcc returns "c" for typeid(char).name(), as demonstrated by the following program:

#include <iostream>
#include <typeinfo>

int main() {
  std::cout << typeid(char).name() << std::endl;
  std::cout << typeid(short).name() << std::endl;
  std::cout << typeid(int).name() << std::endl;
  std::cout << typeid(long).name() << std::endl;
}

On my compiler, this prints out

c
s
i
l

Given that the name() strings are implementation-defined, this is compliant behaviour.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top