Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can I add optional types parameters in C++

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading