How do I get a type(keyword) in a function?

Hello all!

How do I get a type(keyword) in a function?

Here is my program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
#include <typeinfo.h>

void test(??? datatype){ //How to do?
	printf("%s\n", typeid(datatype).name());
}

int main(){
	test(int);

	system("pause");
	return 0;
}
You can use templates to pass types
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <stdlib.h>
#include <typeinfo>

template<typename T>
void test(){
	printf("%s\n", typeid(T).name());
}

int main(){
	test<int>();
}

Templates.
http://www.cplusplus.com/doc/tutorial/templates/

Or, you could pass the typeid if you want to do it at runtime. ;)
Last edited on
Thank for your replies, Peter87 and L B!!!

Is that possible to pass a type as a parameter in a function?
 
test(int);

It is just like...
 
sizeof(int);
No it's not possible. sizeof is an operator and not a function.
No, that's what templates are for:

1
2
3
func<int>();
func<double>();
//etc 


sizeof isn't a function, it's like return, just a keyword.
sizeof int;
is the same as
sizeof(int);
Last edited on
Thank for your replies, Peter87 and firedraco !!!

Topic archived. No new replies allowed.