Some way of making a function accept any data type?

Started by
11 comments, last by johnnyBravo 20 years, 2 months ago
Hi, im wondering if its possible to have a function accept any data type. eg function(anyDataType value) { } , that is without making lots of functions of the name with different datatypes. Thanks,
Advertisement
Assuming C++, you can make it a template function and parameterize the function argument time. e.g.:
template <typename T>
void function(T value) {
}

Or, if you want to travel to the land of extremely bad ideas you can do:
void function(...);
Or possibly, depending on the situation, use a void pointer.
I don''t know why you would actually want that, but...
i want to do this because i am storing integers, floats etc into a file, and iam making a class wrapper over the fstream
one of the most basic and yet important features of c++ is function overloading

you can define multiple functions with the same name as long as they difer in parameters, you could have

void PrintStuff(char ch){std::cout<<"You passed "<<ch<<" ,which is a character, to the PrintStuff function\n";}void PrintStuff(int i){std::cout<<"You passed "<<i<<" ,which is an integer, to the PrintStuff function\n";}void PrintStuff(float fl){std::cout<<"You passed "<<fl<<" ,which is a float, to the PrintStuff function\n";}


and in your main...

int main()
{
int bleh = 21;

PrintStuff(bleh); // since bleh is an integer, the compiler knows to use the definition of PrintStuff, that has an integer for a param
return 0;
}

[edited by - Ademan555 on February 29, 2004 3:57:53 AM]
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
ya, i didnt want to use overloading, as i might want to use custom made classes, imso far using void *, but havent tested it on classes yet
template <typename T>void PrintStuff(T val){   std::cout << "You passed "  << val << ", which is a " << typeid(T).name() << ", to the " << __FUNCTION__ << " function\n";}

__FUNCTION__ only works on ms compilers 7.0+ and I believe most versions of gcc
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
boost::any perhaps?

[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
quote:Original post by dalleboy
boost::any perhaps?

.. which eventually drives you into lots of if-then statements
as you need to use boost::any_cast.



[edited by - darookie on February 29, 2004 6:43:58 AM]

This topic is closed to new replies.

Advertisement