Home > Net >  can I add optional types parameters in C
can I add optional types parameters in C

Time:03-12

the title says it all.

I have this function that couts the parameter:

void outlog(string par) {

   std::cout << par;

} 

but Par can only be a string. how can I make its type optional.

CodePudding user response:

C (and many other languages) makes it available to have generic functions. Here you can read more about it: Generics in C Here is the sample code for your function. Note: the object should be printable (if it is a class object, it should have the << operator overloaded):

template <typename T>
void outlog(T& input){
    std::cout << input;
}
  • Related