15

How can I in C++ make a function accept every Object, so I can give it numbers, String or other Objects. I am not very well in C++, I hope it's not a totally stupid question...

Edit: Ok, an example: if you want to try to wrap the std::cout streams into normal functions, that funtion should be able to accept everything - from Integers over Floats to complex Objects. I hope it's more clear now!

Codeversum
  • 317
  • 1
  • 3
  • 10

5 Answers5

22

You can overload your function for different types, i.e.

size_t func(int);
size_t func(std::string);

Alternatively and/or additionally, you can provide a function template, which is a way to tell the compiler how to generate your function for any particular type, for example

template<typename T>
size_t func(T const&) { return sizeof(T); }

You may use more advanced techniques such as SFINAE to effectively overload those template functions, i.e. to use different templates for different kind of types T (i.e. integral types, pointer, built-in types, pod, etc). The compiler will then pick the best-fitting func() (if any) for any function call it encounters and, if this is a template, generate an appropriate function. This requires no re-coding.

A completely different approach is to use a generic erasure type, such as boost::any, when the function will need to resolve the expected types at coding-time (as opposed to compile-time):

size_t func(boost::any const&x)
{
  auto i = boost::any_cast<const int*>(x);
  if(i) return func(*i);
  // etc for other types, but this must be done at coding time!
}
Walter
  • 44,150
  • 20
  • 113
  • 196
  • Since C++20, it's also possible to [infer parameter types using `auto`](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template). – Anderson Green Sep 01 '21 at 15:43
  • "coding-time"!? You mean "run-time", right? – Alexis Wilke Sep 26 '21 at 00:46
  • @AlexisWilke I actually meant coding time. The point is that while the program resolves the type at run-time, the code dealing with possible types must be coded for each possible time, which happens at coding time. – Walter Sep 27 '21 at 10:29
10

You can use templates for this purpose:

template <typename T>
void foo(T const & value)
{
    // value is of some type T, which can be any type at all.
}

What you can actually do with the value may be rather limited without knowing its type -- it depends on the goal of your function. (If someone attempts to call the function with an argument type that causes that function specialization to be ill-formed then your template function will fail to instantiate and it will be a compile-time error.)

cdhowie
  • 158,093
  • 24
  • 286
  • 300
3

I'm not sure what you're trying to accomplish, but you can pass a void pointer as a parameter.

void foo(void* bar);
PDizzle745
  • 356
  • 1
  • 7
1

If I understood you correctly you might wanna try using templates http://en.cppreference.com/w/cpp/language/function_template

0

You are probably looking for templates.
I suggest you read this.

Axalo
  • 2,953
  • 4
  • 25
  • 39