How to differentiate input types?

Asked

Viewed 111 times

2

I’m using the operator typeid to check conditions, where I do the following basic test:

int main()
{
    cout << (typeid(int) == typeid('abc')) << endl; //TRUE
    cout << (typeid(int) == typeid(100)) << endl; //TRUE
    cout << (typeid(int) == typeid(100.40)) << "\n" << endl; // FALSE

    cout << (typeid(float) == typeid('abc')) << endl; // FALSE
    cout << (typeid(float) == typeid(100)) << endl; // FALSE
    cout << (typeid(float) == typeid(100.40)) << "\n" << endl; // FALSE

    cout << (typeid(char) == typeid('a')) << endl; //TRUE
    cout << (typeid(char) == typeid('ab')) << endl; // FALSE
    cout << (typeid(char) == typeid(100)) << endl; // FALSE
    cout << (typeid(char) == typeid(100.40)) << endl; // FALSE

    return 0;
}

But I couldn’t understand the true and false for certain conditions, such as:

cout << (typeid(int) == typeid('abc')) << endl; //TRUE
cout << (typeid(float) == typeid(100.40)) << "\n" << endl; // FALSE
cout << (typeid(char) == typeid('ab')) << endl; // FALSE

Questions in doubt:

  • to) If a char is also the type int, how to differentiate a int of a char?
  • b) Why typeid(float) == typeid(100.40) is not true?
  • c) Why typeid(char) == typeid('ab') is not true?

3 answers

2


Do you know that this operator is to be used with polymorphic objects and the types used are not polymorphic? And that the goal of this operator is to get a runtime information and not something you know at compile time, so all this code is useless? And know that there is no guarantee that the operator returns the same type identifier for objects of the same type?

a) If a char is also the type int, how to differentiate a int of a char?

This is not true, the conclusion is wrong. char is char, and int is int.

b) Why typeid(float) == typeid(100.40) is not true?

Because 100.4 is a number like double. The literal for a value float would be 100.40f.

c) Why typeid(char) == typeid('ab') is not true?

Because the guy char can only have one character, so it was the die was converted to another type, possibly a int. This is behavior not specified by the language and the compiler can do as it sees fit.

Complementing according to the comments: if you already know what the type of variable or expression is then you have no reason to use this type of operator. It has nothing to do with the content entered, it has to do with the type used in the code, and this you always know in basic types like this. You just don’t know when the type is polymorphic, even in these cases the type is always within the possible hierarchy between them, otherwise it will always be false. All this code posted is curious, but unnecessary and even useless, can do the same thing without it.

  • The syntax is c++, but the idea here would be to understand the response of this function, so I inserted the c tag as well. Well, how do I stop at runtime to compare the primitive types? Add your answer that I apply the same as the correct one.

  • Why do you want to do this? I saw no need.

  • The intention is to make use of conditions in case the type inserted at runtime is different from the one I entered it returns an error, but it is only for learning even.

  • This is impossible to do. The type is set at compile time so it cannot be different at runtime. I don’t think you understand what this operator is for. So I stress, it’s only useful for polymorphic comparisons, which is not the case, this is the only chance of the type being different at runtime. Don’t try to do something that doesn’t need to be done and it makes no sense to do it.

  • Okay, I get it, so I have to use the famous switch to analyze the input from Cin? To check if it really is a number or a char? I’m trying to do something elegant for this, which suggests?

  • No, no need, if you declare the variable as char is a char, declare itself as a int is a int, It doesn’t change anything and you already know what kind it is when you’re coding, you don’t need any of this. The typing is already defined in your code, what you want to do makes no sense.

Show 1 more comment

2

a) If a char is also of type int, how to differentiate an int from a char?

The guy char is a different kind of int. You can only verify with the following:

cout << (typeid(int) == typeid(char)) << endl; //0

But the char is represented internally with a numerical value corresponding to the ASCII table. Therefore it is common to convert the value of a char for a whole.

The reason your case came true is that you built a string or char wrong. The compiler itself warns you of this when compiling your code:

...main.cpp|14|Warning: multi-character Character Constant [-Wmultichar]|

A string is bounded by " and can have several characters, already a char is delimited by ' and can only have one character. You have put several characters in a char what is wrong.

b) Why typeid(float) == typeid(100.40) is not true?

Because 100.40 is interpreted as double. You can also easily see this by changing the cout corresponding to:

cout << (typeid(double) == typeid(100.40)) << "\n" << endl; // 1

c) Why typeid(char) == typeid('ab') is not true?

For the same reason I explained in the first point.

Look at the examples I mentioned in Ideone

  • How do I test primitive types at runtime ? Add this the answer is I will apply the same as the correct one.

  • @THIAGODEBONIS But what are you trying to do exactly ? Only knowing that I can suggest something.

0

On the issues to) and c), to ISO/IEC 9899:1999 in the section §6.4.4.4.10 defines this behavior:

An integer Character Constant has type int. The value of an integer Character Constant containing a single Character that maps to a single-byte Execution Character is the numerical value of the representation of the Mapped Character Interpreted as an integer. The value of an integer Character Constant containing more than one Character (e.g.,'ab'), or containing a Character or escape Sequence that does not map to a single-byte Execution Character,is implementation-defined. If an integer Character Constant contains asingle Character or escape Quence, its value is the one that Results when an Object with type char Whose value is that of the single Character or escape Quence is converted to type int.

On the question b), 100.40 is of the double type. In order for it to float, the value must be defined as a literal of this type, setting it with the letter "f": 100.40f

Browser other questions tagged

You are not signed in. Login or sign up in order to post.