Hi guys. I'm pretty new here. I was recently playing around with templates and found them to be really useful. I was wondering how I could check the data type of the variable that was passed into the function, and I heard about RTTI from my friends. I've looked around the forum, but I'm still very confused.
For example, I'm currently testing a simple code, but I'm not too sure where things are wrong.
#include <iostream>
#include <typeinfo>
using namespace std;
template <typename T = {int, char}>
void rttiTester( T testVar)
{
if( typeid(testVar) == typeid(int) )
cout << "Is integer";
else
cout << "Is character";
}
void main()
{
int test1;
char test2;
cout << "Enter integer: ";
cin >> test1;
cin.ignore(255, '\n');
cout << "Enter character: ";
cin >> test2;
cin.ignore(255, '\n');
rttiTester(test1);
rttiTester(test2);
}
I've been looking around google, and I can't really find any idiot proof hints or guides on how to use RTTI. I'm still quite a beginner, and so far I'm only familiar with C++ up to some basic OOP and pointers.
Some of the errors I faced were template <typename T = {int, char}>
where the compiler gave me an "error C2143: syntax error : missing '>' before '{'. The full list is here.
rtti.cpp(5) : error C2143: syntax error : missing '>' before '{'
rtti.cpp(5) : error C2059: syntax error : '>'
rtti.cpp(20) : error C2587: 'test1' : illegal use of local variable as default parameter
rtti.cpp(17) : see declaration of 'test1'
rtti.cpp(23) : error C2587: 'test2' : illegal use of local variable as default parameter
rtti.cpp(18) : see declaration of 'test2'
rtti.cpp(26) : error C2587: 'test1' : illegal use of local variable as default parameter
rtti.cpp(17) : see declaration of 'test1'
rtti.cpp(27) : error C2587: 'test2' : illegal use of local variable as default parameter
rtti.cpp(18) : see declaration of 'test2'
I'm currently using Microsoft Visual C++ 2008 Express Edition.
In general, I'm intend to use templates to create some sorting or initialising functions to use for my projects and assignments.
EDIT: Oh yes, I was previously using Microsoft Visual C++ 6.0, and recently changed to the express edition. I'm still not familiar with the interface. Can someone point out to me how I can enable RTTI?