jeudi 31 décembre 2015

c++ multiple interfaces to the same functionality template

I have several methods that work over utf8 encoded strings that I usually keep in std::string.

In same cases though I just have const char* to the data, or the data is part of a bigger string I do not want to create a substring.

All the functionality boils down to one generic method say:

void foo(int a, int b, const char* beginStr, const char* endStr);

Now if I would like to avoid some ugliness I will create

void foo(int a, int b, const char* beginStr, const char* endStr);
void foo(int a, int b, const char* str);
void foo(int a, int b, const std::string% str);

and in some cases even:

void foo(int a, int b, const std::string::const_iterator& beginStr
                       const std::string::const_iterator& endStr);

This seems Ok, but as I mentioned I have several methods and it gets really annoying maintaining all these flavors.

What I am looking is some magic that could eliminate the need of multiplying every interface. Can some of the c++11 features help with solving this without performance impact - making it less chaty?

Aucun commentaire:

Enregistrer un commentaire