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?

Recommended Answers

All 5 Replies

Think this can help you a bit.

I'm still quite confused. But I came across reflection of data types:
http://msdn.microsoft.com/en-us/library/y0114hz2.aspx

It seems easier to use, and I can print out the data types when I follow the code examples, however, how can I use them for decision making or looping? I've tried things like if( tempVar->GetType() == "System.Int32") or if(tempVar->GetType() == typeid(System.Int32)) and so on, but they don't seem to work.

The whole concept of Template is to exploit the fact that interface of the data-type passed is same. Consider std:;sort() it takes the vector of any container which have overloaded > operator.
That is, template are used to perform type-unspecific operations.
If you are needing a way to check if the type of the parameter is a int or a float, your designs have flaws and you should not be using Templates for this purpose.
Instead you should have been using a overloaded function.

Another thing you can consider is the Template specialization. Which can be understood here.

Use typeid(), RTTI is not a good idea in the first place. Avoid it.( Read Scott Mayers Effective C++)

Thank you. I'll take a look at it.

Thanks for the FAQ on templates and the part on specializing. That exactly suits my needs. Everything works fine now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.