tags:

views:

82

answers:

5

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.
+4  A: 

No, this is not valid C++.

IntelliSense is not smart enough to find everything that is wrong with code; it would have to fully compile the code to do that, and compiling C++ is very slow (too slow to use for IntelliSense).

James McNellis
+1  A: 

No, you can't use if (T == int) and std::cout<<(int)int;

xueyumusic
+1  A: 

Your probably didn't even instantiate your template, that's why it compiled.

Grozz
A: 

Is this what you're trying to do?

if(typeid(T) == typeid(int))

and this?

cout << typeid(int).name();
PigBen
+1  A: 

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
Keynslug