mardi 18 juillet 2017

Generic enum as parameter C++

I'm developing a project using C++11. I have a bunch of functions that do the same but we have them overloaded with different enums like this.

void init_trace(const my::namespace::enum_type1 event);
void init_trace(const my::namespace::enum_type2 event);
void init_trace(const my::namespace::enum_type3 event);

and in implementation I have

void init_trace(const my::namespace::enum_type1 event) {
     call_to_function(par1, event.to_string(), par3);
}

void init_trace(const my::namespace::enum_type2 event) {
     call_to_function(par1, event.to_string(), par3);
}

As you can see we are repeating the same code. I would like to know if I can pass that event enum as generic to have all in one single function. Is this better? How can I do that?

Thank you

Aucun commentaire:

Enregistrer un commentaire