I typed this into a template function, just to see if it would work:
if (T==int)
and the intellisense didn't complain. Is this valid C++? What if I did:
std::cout << (int)int; // looks stupid doesn't it.
If both operands have the same type, then no further conversion is needed. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
You can call isdigit() on each character of the string, and if it's true for all characters you have an integer, otherwise it's some alphanumeric string. You can also call strtol to parse the string as an integer. The second argument returns a pointer to the first non-numeric character in the string.
In order to compare two strings, we can use String's strcmp() function. The strcmp() function is a C library function used to compare two strings in a lexicographical manner. The function returns 0 if both the strings are equal or the same. The input string has to be a char array of C-style string.
The size is typically about 32-bits or 4 bytes on a 16/ 32-bit compiler. Yet, it varies depending on what compiler we are using. There is no specification of the data types sizes according to the C standard, except the character.
Just to fit your requirement you should use typeid
operator. Then your expression would look like
if (typeid(T) == typeid(int)) {
...
}
Obvious sample to illustrate that this really works:
#include <typeinfo>
#include <iostream>
template <typename T>
class AClass {
public:
static bool compare() {
return (typeid(T) == typeid(int));
}
};
void main() {
std::cout << AClass<char>::compare() << std::endl;
std::cout << AClass<int>::compare() << std::endl;
}
So in stdout you'll probably get:
0
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With