Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function that accepts every type of argument [duplicate]

Tags:

c++

variables

Let's say I want to create a function that couts the value I pass it, but I don't know whether it is an int, or a string, or….

So something like:

void print(info) {
   cout << info;
}

print(5);
print("text");
like image 967
ljsp Avatar asked Dec 11 '22 13:12

ljsp


1 Answers

You can do it with a function template:

template <typename T>
void print( const T& info)
{
   std::cout << info ;
}
like image 197
taocp Avatar answered Dec 14 '22 01:12

taocp